Android筆記-5-EditText密碼和Checkbox二選一

OuZeBo發表於2015-08-18

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);
    }
  }
}

 

       

相關文章