简体   繁体   English

如何通过单击按钮在密码字段中显示和隐藏单词

[英]how to show and hide the word in a password field by clicking on a button

Is it possible to show the word inserted in a password field by clicking on a button and then when I click it again hide the password as dots? 是否可以通过单击按钮显示插入密码字段中的单词,然后当我再次单击它时将密码隐藏为点?

I'll really appreciate your help. 我真的很感谢你的帮助。

Try this code: 试试这段代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_marginTop="100sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <EditText
        android:id="@+id/pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="200sp"
        android:hint="Password"
        android:inputType="textPassword" />
    <Button
        android:id="@+id/showHideBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show"/>
    </LinearLayout>

</LinearLayout>

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    showHideBtn.setOnClickListener {
        if(showHideBtn.text.toString().equals("Show")){
            pwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            showHideBtn.text = "Hide"
        } else{
            pwd.transformationMethod = PasswordTransformationMethod.getInstance()
            showHideBtn.text = "Show"
        }
    }
}
}

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

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