在View Stub中为Button设置setOnClickListener



我正在为我的登录屏幕创建一个视图存根,但是我有一个问题。当我试图在我的viewstub_login.kt文件中设置登录按钮上的setOnClickListener时,按钮动作不会通过。我需要改变我的Kotlin文件的扩展名,或者我需要把我的setOnClickListeneronStart()功能?谢谢你!

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
}

click listener是正确的,但在块内,您的代码是不正确的因为emailText和passwordText不是空的,正如你在上面定义的,因此if条件返回false,你的代码只是越过它

相关内容

  • 没有找到相关文章

最新更新