กดปุ่มเพื่อสลับระหว่าง แสดง หรือ ซ่อน password
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center" android:orientation="horizontal"> <EditText android:id="@+id/pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" android:minWidth="200sp" /> <Button android:id="@+id/showHideBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" /> </LinearLayout> </LinearLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.phaisarn.ktapplication import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.text.method.HideReturnsTransformationMethod import android.text.method.PasswordTransformationMethod import kotlinx.android.synthetic.main.activity_main.* 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" } } } } |