EditText密码:明文和密文
密文:
1 public class MainActivity extends Activity { 2 3 private EditText password = null; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 this.password = (EditText) super.findViewById(R.id.pwdEdittext); 10 //设置为密文 11 MainActivity.this.password.setTransformationMethod(PasswordTransformationMethod.getInstance()); 12 13 } 14 15 @Override 16 public boolean onCreateOptionsMenu(Menu menu) { 17 // Inflate the menu; this adds items to the action bar if it is present. 18 getMenuInflater().inflate(R.menu.main, menu); 19 return true; 20 } 21 }
明文密文切换(Checkbox切换):
public class MainActivity extends Activity { private EditText password = null; private CheckBox show = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.password = (EditText) super.findViewById(R.id.pwdEdittext); MainActivity.this.password.setTransformationMethod(PasswordTransformationMethod.getInstance()); this.show = (CheckBox) super.findViewById(R.id.display_checkBox); this.show.setOnClickListener(new OnclickListenerlmp()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void register(View view){ Intent intent = new Intent(); intent.setClass(this, RegisterActivity.class); this.startActivity(intent); } private class OnclickListenerlmp implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub if(MainActivity.this.show.isChecked()){ //设置为明文显示 MainActivity.this.password.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); } else{ //设置为密文显示 MainActivity.this.password.setTransformationMethod(PasswordTransformationMethod.getInstance()); } } } }
Checkbox二选一
public class RegisterActivity extends Activity { private CheckBox choose1 = null; private CheckBox choose2 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); this.choose1 = (CheckBox) super.findViewById(R.id.sex_checkBox1); this.choose2 = (CheckBox) super.findViewById(R.id.sex_checkBox2); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.register, menu); return true; } public void choose(View view){ if(RegisterActivity.this.choose1.isChecked()){ choose2.setChecked(false); } else{ choose1.setChecked(false); } } }
时间: 2024-10-01 07:48:03