Android动态显示隐藏密码输入框的内容
16lz
2021-01-23
推荐安卓开发神器(里面有各种UI特效和android代码库实例)
通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码,示例如下:
private Button mBtnPassword; private EditText mEtPassword; private boolean mbDisplayFlg = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mEtPassword = (EditText)findViewById(R.id.password); mBtnPassword = (Button)findViewById(R.id.btnPassword); mBtnPassword.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d("AndroidTest", "mbDisplayFlg = " + mbDisplayFlg); if (!mbDisplayFlg) { // display password text, for example "123456" mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); } else { // hide password, display "." mEtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); } mbDisplayFlg = !mbDisplayFlg; mEtPassword.postInvalidate(); } }); }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btnPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:password="true" android:textSize="18sp" android:text="123456"> </EditText> </LinearLayout>
更多相关文章
- android 播放音乐示例
- Android 自定义View及其在布局文件中的使用示例(二)
- android生命周期神器--Lifecycle
- Android 开机自启动示例程序
- Android使用SQLite数据库的示例
- Android中对Group的各种操作示例代码
- 【Android】抽屉控件 SlidingDrawer 的简单示例
- Android EditText 密码输入框可见性设置
- Android日语输入法Simeji使用示例