Android Studio开发小常识

1、Android Studio如何快速生成get,set,tostring,构造函数?

方式一:Code-->Generate

方式二:通过快捷键Alt+Insert

2、在Eclipse中如果我们想让一段代码格式变得整齐,可以:选中这行代码→右键单击→Source→Format或者Ctrl+Shift+F就可以

在Android Studio中如果我们想让一段代码格式变得整齐,可以:选中这行代码→Code→Reformed Code或者Ctrl+Alt+L就可以

如下图:

格式化.jpg

3、

1.实现按钮的不可见

XML:android:visibility="invisible"

Java代码:view.setVisibility(View.INVISIBLE);

2.实现按钮的隐藏

XML:android:visibility="gone"

Java代码:view.setVisibility(View.GONE);

4、android判断EditText输入的数字、中文还是字母方法:

String txt = edInput.getText().toString();
 
   Pattern p = Pattern.compile( "[0-9]*" );
    Matcher m = p.matcher(txt);
    if (m.matches() ){
    Toast.makeText(Main. this , "输入的是数字" , Toast.LENGTH_SHORT).show();
    }
    p=Pattern.compile( "[a-zA-Z]" );
    m=p.matcher(txt);
    if (m.matches()){
    Toast.makeText(Main. this , "输入的是字母" , Toast.LENGTH_SHORT).show();
    }
    p=Pattern.compile( "[\u4e00-\u9fa5]" );
    m=p.matcher(txt);
    if (m.matches()){
    Toast.makeText(Main. this , "输入的是汉字" , Toast.LENGTH_SHORT).show();
    }
 
5、

1 如何将字串 String 转换成整数 int ? 

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或 
i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue(); 

注: 字串转成 Double, Float, Long 的方法大同小异. 

2 如何将整数 int 转换成字串 String ? 

A. 有叁种方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i); 

3.) String s = "" + i; 

3 如何将整数 double 转换成字串 int? 

double d = 12.0; 

int i = (int)d;

注: Double, Float, Long 转成字串的方法大同小异.

Double.toString(d);

Float.toString(f);

Long.toString(n);

  6、
collapseColumns – 设置隐藏那些列,列ID从0开始,多个列的话用”,”分隔
stretchColumns – 设置自动伸展那些列,列ID从0开始,多个列的话用”,”分隔 
shrinkColumns -设置自动收缩那些列,列ID从0开始,多个列的话用”,”分隔
可以用”*”来表示所有列,同一列可以同时设置为shrinkable和stretchable。
  7、
/**
 * 确认字符串是否为email格式
*
 * @param strEmail
*/
public static boolean isEmail(String strEmail) {
    String strPattern = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    Pattern p = Pattern.compile(strPattern);
    Matcher m = p.matcher(strEmail);
    return m.matches();
}

/**
 * 确认字符串是否为手机格式
*
 * @param strMobiles
*/
public static boolean isMobile(String strMobiles) {
    Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
    Matcher m = p.matcher(strMobiles);
    return m.matches();
}

猜你喜欢

转载自ch-kexin.iteye.com/blog/2281747