Android基礎--登陸介面,密碼的隱藏和顯示
阿新 • • 發佈:2019-01-26
在開發中,有的需要密碼的隱藏和顯示,這裡直接程式碼展示,很簡單:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id ="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="輸入密碼"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height ="wrap_content"
android:text="顯示密碼" />
</LinearLayout>
public class MainActivity extends Activity {
private EditText editText;
private CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText =(EditText) findViewById(R.id.editText1);
checkBox=(CheckBox) findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
//如果選中,顯示密碼
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else{
//否則隱藏密碼
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
}