我正在使用科特林。
我有一個存盤的密碼(val password = 1111)。我有一個接受“numberPassword”輸入的 editText(密碼)。我希望用戶輸入代碼,如果匹配我會做一些事情。
我努力了:
if (passcode.equals(1111)) { //do something }
if (passcode = password) { //do something }
if (passcode.text.toString() == password) { //do something }
if (passcode == "1111") { //do something }
...和許多其他版本在 int 和 string 之間來回切換
這應該非常簡單,因為用戶不是對就是錯。我猜它與 string/int 的東西有關。謝謝你的幫助!
剛試過:
<EditText
android:id="@ id/passcode"
android:layout_width="120dp"
android:layout_height="75dp"
android:layout_gravity="center_horizontal"
android:textAlignment="center"
android:hint="@string/zeros"
android:drawableStart="@drawable/ic_key"
android:inputType="numberPassword"
android:maxLength="4"
android:maxLines="1"
android:textColor="@color/black"
android:textColorHint="@color/light_gray"
android:textCursorDrawable="@null"
android:textSize="35sp"
android:singleLine="true"
android:autofillHints="true" />
private var passcode: EditText? = null private var password: Int = 1111
view.submitButton?.setOnClickListener {
if (passcode?.text.toString() == password.toString()){
Toast.makeText(this@SplashScreen, "Valid", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this@SplashScreen, "Invalid", Toast.LENGTH_LONG).show()
}
}
結果:在edittext中輸入1111無效
uj5u.com熱心網友回復:
您的密碼變數未初始化,因為它仍然為空。
像這樣設定:
private var passcode : EditText? = findViewById<EditText>(R.id.passcode)
uj5u.com熱心網友回復:
您正在將整個小部件與可變密碼進行比較。您需要的是將密碼變數與passcode.text.toString()
以下內容進行比較:
if (passcode.text.toString() == password.toString()){
Toast.makeText(this@MainActivity, "Correct Passcode!", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this@MainActivity, "Wrong Passcode!", Toast.LENGTH_SHORT).show()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/533801.html
標籤:安卓科特林验证密码等于
