获取分辨率及密码的显示与隐藏
过完春节耍嗨了,一看代码感觉都陌生了,于是动手写点代码找哈感觉,进去正题:获取手机分辨率,这个就两行代码非常简单:
public class MainActivity extends Activity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.cc);
//获取手机分辨率
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
String opt = "手机分辨率为:" + displayMetrics.widthPixels + "x"
+ displayMetrics.heightPixels;
mTextView.setText(opt);
}
}
密码的显示与隐藏,这个很多登陆都会用到,点击CheckBox打钩就显示密码,再点击不打钩就隐藏掉密码,看是看以下代码,也是比较简单的几行代码:
ublic class MainActivity extends Activity {
private EditText editText;
private CheckBox box;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edt);
box = (CheckBox) findViewById(R.id.cbx);
box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(box.isChecked()){
//显示密码
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else {
// 隐藏密码
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
}
文档来源:51CTO技术博客https://blog.51cto.com/u_14397532/3035630
页:
[1]