[置顶] 显示/隐藏登录密码
利用复选框勾选---显示/隐藏登录密码(例如 电脑无线密码查看)
【MainActivity.java】
package com.example.test; import android.os.Bundle; import android.app.Activity; import android.text.InputType; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; public class MainActivity extends Activity { private CheckBox c1; private EditText e1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); c1 = (CheckBox) findViewById(R.id.checkBox1); e1 = (EditText) findViewById(R.id.editText1); e1.getText().toString(); c1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { e1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); } else { e1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); } } }); } }
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="19dp" android:layout_marginTop="160dp" android:text="显示密码" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/checkBox1" android:layout_alignLeft="@+id/checkBox1" android:layout_marginBottom="47dp" android:ems="10" android:inputType="textPassword" /> </RelativeLayout>