是否可以通过单击按钮来显示插入在密码字段中的单词,然后再次单击该单词时将密码隐藏为点?
非常感谢您的帮助。
答案 0 :(得分:1)
尝试此代码:
<?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"
}
}
}
}