在视图存根中的按钮上设置 setOnClickListener
Setting setOnClickListener on a Button in a View Stub
我正在为我的登录屏幕创建一个视图存根,但是我有一个问题。当我尝试在 viewstub_login.kt
文件中的登录按钮上设置 setOnClickListener
时,按钮操作不会执行。我需要更改 Kotlin 文件的扩展名,还是需要将 setOnClickListener
放入 onStart()
函数中?谢谢!
viewstub_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#BF090909">
<androidx.cardview.widget.CardView
android:id="@+id/loginCardView"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:cardBackgroundColor="@color/main_purple"
app:cardCornerRadius="8dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/logoLoginImageView"
android:layout_width="90dp"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:contentDescription="@string/login_logo_image"
android:scaleType="fitXY"
android:src="@drawable/skedaddle_services_"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/emailTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:textColorHint="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logoLoginImageView">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/emailInputTextView"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/textview_underlinr"
android:drawableStart="@drawable/outline_mail_white_24"
android:drawablePadding="5dp"
android:hint="@string/e_mail"
android:textColor="@color/white"
android:textColorHint="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/passwordTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:hint="@string/password"
android:textColorHint="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/emailTextInputLayout"
app:passwordToggleEnabled="true"
app:passwordToggleTint="@color/white">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/passwordTextInputView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/textview_underlinr"
android:drawableStart="@drawable/outline_lock_white_24"
android:drawablePadding="5dp"
android:hint="@string/password"
android:inputType="textPassword"
android:textColor="@color/white"
android:textColorHint="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/forgotPasswordButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:background="@android:color/transparent"
android:text="@string/forgot_password"
android:textAlignment="textEnd"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="12sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordTextInputLayout" />
<Button
android:id="@+id/cancelButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:background="@color/black"
android:text="@android:string/cancel"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/secondLoginButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/forgotPasswordButton" />
<Button
android:id="@+id/secondLoginButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:layout_marginStart="32dp"
android:background="@color/main_teal"
android:text="@string/log_in"
android:textAllCaps="false"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="@+id/cancelButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/cancelButton"
app:layout_constraintTop_toTopOf="@+id/cancelButton"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
viewstub_login.kt
class LoginViewStub : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.viewstub_login)
val emailTextView = findViewById<TextInputEditText>(R.id.emailInputTextView)
val passwordTextView = findViewById<TextInputEditText>(R.id.passwordTextInputView)
val forgetPasswordButton = findViewById<Button>(R.id.forgotPasswordButton)
val loginButton = findViewById<Button>(R.id.secondLoginButton)
val cancelButton = findViewById<Button>(R.id.cancelButton)
val emailText = emailTextView.text
val passwordText = passwordTextView.text
loginButton.setOnClickListener {
if (emailText.isEmpty(emailTextView.text) || passwordText.isEmpty(passwordTextView.text)) {
val dialogBuilder = AlertDialog.Builder(this)
dialogBuilder.setMessage("Please fill out all fields.").setCancelable(false).setPositiveButton("Okay", DialogInterface.OnClickListener {
dialog, id -> finish()
}).setNegativeButton("Cancel", DialogInterface.OnClickListener {
dialog, id -> dialog.cancel()
})
val alert = dialogBuilder.create()
alert.show()
}
}
cancelButton.setOnClickListener {
}
forgetPasswordButton.setOnClickListener {
}
}
}
private fun Boolean.isEmpty(str: CharSequence?): Boolean {
return str == null || str.length == 0
}
点击侦听器是正确的,但在块内部,您的代码不正确
因为 emailText 和 passwordText 不为空,正如您在上面定义的那样,因此 if 条件返回 false 并且您的代码只是通过它
我正在为我的登录屏幕创建一个视图存根,但是我有一个问题。当我尝试在 viewstub_login.kt
文件中的登录按钮上设置 setOnClickListener
时,按钮操作不会执行。我需要更改 Kotlin 文件的扩展名,还是需要将 setOnClickListener
放入 onStart()
函数中?谢谢!
viewstub_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#BF090909">
<androidx.cardview.widget.CardView
android:id="@+id/loginCardView"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:cardBackgroundColor="@color/main_purple"
app:cardCornerRadius="8dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/logoLoginImageView"
android:layout_width="90dp"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:contentDescription="@string/login_logo_image"
android:scaleType="fitXY"
android:src="@drawable/skedaddle_services_"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/emailTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:textColorHint="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logoLoginImageView">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/emailInputTextView"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/textview_underlinr"
android:drawableStart="@drawable/outline_mail_white_24"
android:drawablePadding="5dp"
android:hint="@string/e_mail"
android:textColor="@color/white"
android:textColorHint="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/passwordTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:hint="@string/password"
android:textColorHint="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/emailTextInputLayout"
app:passwordToggleEnabled="true"
app:passwordToggleTint="@color/white">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/passwordTextInputView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/textview_underlinr"
android:drawableStart="@drawable/outline_lock_white_24"
android:drawablePadding="5dp"
android:hint="@string/password"
android:inputType="textPassword"
android:textColor="@color/white"
android:textColorHint="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/forgotPasswordButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:background="@android:color/transparent"
android:text="@string/forgot_password"
android:textAlignment="textEnd"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="12sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordTextInputLayout" />
<Button
android:id="@+id/cancelButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:background="@color/black"
android:text="@android:string/cancel"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/secondLoginButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/forgotPasswordButton" />
<Button
android:id="@+id/secondLoginButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:layout_marginStart="32dp"
android:background="@color/main_teal"
android:text="@string/log_in"
android:textAllCaps="false"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="@+id/cancelButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/cancelButton"
app:layout_constraintTop_toTopOf="@+id/cancelButton"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
viewstub_login.kt
class LoginViewStub : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.viewstub_login)
val emailTextView = findViewById<TextInputEditText>(R.id.emailInputTextView)
val passwordTextView = findViewById<TextInputEditText>(R.id.passwordTextInputView)
val forgetPasswordButton = findViewById<Button>(R.id.forgotPasswordButton)
val loginButton = findViewById<Button>(R.id.secondLoginButton)
val cancelButton = findViewById<Button>(R.id.cancelButton)
val emailText = emailTextView.text
val passwordText = passwordTextView.text
loginButton.setOnClickListener {
if (emailText.isEmpty(emailTextView.text) || passwordText.isEmpty(passwordTextView.text)) {
val dialogBuilder = AlertDialog.Builder(this)
dialogBuilder.setMessage("Please fill out all fields.").setCancelable(false).setPositiveButton("Okay", DialogInterface.OnClickListener {
dialog, id -> finish()
}).setNegativeButton("Cancel", DialogInterface.OnClickListener {
dialog, id -> dialog.cancel()
})
val alert = dialogBuilder.create()
alert.show()
}
}
cancelButton.setOnClickListener {
}
forgetPasswordButton.setOnClickListener {
}
}
}
private fun Boolean.isEmpty(str: CharSequence?): Boolean {
return str == null || str.length == 0
}
点击侦听器是正确的,但在块内部,您的代码不正确 因为 emailText 和 passwordText 不为空,正如您在上面定义的那样,因此 if 条件返回 false 并且您的代码只是通过它