简体   繁体   English

Android切换按钮在密码字段中无法正常使用

[英]Android Toggle Button not working properly for Password Field

When I Click on the Toggle Button, it changes the Password field to Normal looking Text, But when I click on it Again, It doesnt change the Text field to Password Type back. 当我单击“切换”按钮时,它将“密码”字段更改为“普通外观”文本,但是当我再次单击它时,它不会将“文本”字段更改为“密码类型”。 Why is that so ? 为什么会这样 ?

Here is my code, 这是我的代码,

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);
setContentView(R.layout.text);
chkcmd = (Button) findViewById(R.id.but3);
passtog = (ToggleButton) findViewById(R.id.tb1);
input = (EditText) findViewById(R.id.et1);
display = (TextView) findViewById(R.id.tv2);
passtog.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View V) {
// TODO Auto-generated method stub

if(passtog.isChecked())
{
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
}

else if(!passtog.isChecked())
{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});

Since the Support Library v24.2.0. 自支持库v24.2.0起。 you can achivie this very easy 你可以很容易做到

What you need to do is just: 您需要做的只是:

  1. Add the design library to your dependecies 将设计库添加到您的依存关系

     dependencies { compile "com.android.support:design:25.1.0" } 
  2. Use TextInputEditText in conjunction with TextInputLayout TextInputEditTextTextInputLayout结合使用

     <android.support.design.widget.TextInputLayout android:id="@+id/etPasswordLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true"> <android.support.design.widget.TextInputEditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password_hint" android:inputType="textPassword"/> </android.support.design.widget.TextInputLayout> 

passwordToggleEnabled attribute will make the password toggle appear passwordToggleEnabled属性将使密码切换出现

  1. In your root layout don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto" 在您的根目录布局中,不要忘记添加xmlns:app="http://schemas.android.com/apk/res-auto"

  2. You can customize your password toggle by using: 您可以使用以下方法自定义密码切换:

app:passwordToggleDrawable - Drawable to use as the password input visibility toggle icon. app:passwordToggleDrawable可绘制以用作密码输入可见性切换图标。
app:passwordToggleTint - Icon to use for the password input visibility toggle. app:passwordToggleTint用于密码输入可见性切换的图标。
app:passwordToggleTintMode - Blending mode used to apply the background tint. app:passwordToggleTintMode用于应用背景色的混合模式。

More details in TextInputLayout documentation . 有关更多详细信息,请参见TextInputLayout文档

在此处输入图片说明

Besides implementing the ClickListener you should use the CheckChangedListener as below: 除了实现ClickListener之外,还应该使用CheckChangedListener,如下所示:

 passtog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // Save the state here if(isChecked) { input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_NUMBER_VARIATION_PASSWORD); } else { input.setInputType(InputType.TYPE_CLASS_TEXT); } } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM