当我单击按钮时,如何查看输入类型为"password"的edittext视图的值,当我释放按钮时,文本显示将返回到不可读的格式?就像当你点击密码字段旁边的"眼睛"时微软在Windows 8中所做的那样.
谢谢
Pra*_*ani 58
我找到了设计支持库用户的最佳解决方案之一:
使用最新的Support Library v24.2.0非常容易实现.
你需要做的只是:
将设计库添加到依赖项中
dependencies {
compile "com.android.support:design:24.2.0"
}
Run Code Online (Sandbox Code Playgroud)使用TextInputEditText会同TextInputLayout
<android.support.design.widget.TextInputLayout
android:id="@+id/etPasswordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
android:layout_marginBottom="@dimen/login_spacing_bottom">
<android.support.design.widget.TextInputEditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/fragment_login_password_hint"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)该passwordToggleEnabled属性将完成工作!
在您的根布局中不要忘记添加 xmlns:app="http://schemas.android.com/apk/res-auto"
您可以使用以下方法自定义密码切换:
app:passwordToggleDrawable - Drawable用作密码输入可见性切换图标.
app:passwordToggleTint - 用于密码输入可见性切换的图标.
app:passwordToggleTintMode - 用于应用背景色调的混合模式.
TextInputLayout文档中的更多详细信息.
Mur*_*ain 30
yourButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
editText.setInputType(InputType.TYPE_CLASS_TEXT);
break;
case MotionEvent.ACTION_UP:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
break;
}
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
我喜欢当前的答案!只是为了补充,如果你有一个CheckBox(根据我的说法)使用它是一种很好的做法,OnCheckedListener如下所示:
CheckBox box = (CheckBox)findViewById(R.id.dialog_checkBox_showPassword);
EditText password = (EditText) findViewById(R.id.dialog_editText_password);
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if(checked){
password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}
else{
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17625 次 |
| 最近记录: |