如何使用自定义 setView 为 AlertDialog 设置 OnClickListener?
How to setOnClickListener for AlertDialog with custom setView?
我正在显示来自 Activity 的 AlertDialog OnClickListener:
fav_btn.setOnClickListener {
if(token.equals("false")){
val builder = AlertDialog.Builder(this)
val optionDialog = AlertDialog.Builder(this).create()
builder.setView(R.layout.alert_reg)
builder.setPositiveButton(android.R.string.yes) { dialog, which ->
val intent = Intent(this, SignupActivity::class.java)
startActivity(intent)
}
builder.setNeutralButton(android.R.string.no) { dialog, which ->
optionDialog.dismiss()
}
builder.show()
}
else{
if (isFavourite)deleteFavorite(eventId)
else postFavImage(eventId)
}
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<LinearLayout
android:background="@drawable/round_corner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Assalamualaikum Qalbies"
android:textSize="19dp"
android:fontFamily="@font/poppins_bold"
android:gravity="center"
android:layout_marginTop="@dimen/_20sdp"
android:textColor="#AC6AA6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/_20sdp"
android:paddingTop="@dimen/_8sdp"
android:paddingRight="@dimen/_20sdp"
android:text="Text"
android:textSize="14dp"
android:fontFamily="@font/poppins"
android:gravity="center"
android:layout_marginBottom="@dimen/_5sdp"
android:textColor="#1AB7B8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/_20sdp"
android:paddingTop="@dimen/_8sdp"
android:paddingRight="@dimen/_20sdp"
android:text="To continue, please"
android:textSize="14dp"
android:fontFamily="@font/poppins"
android:gravity="center"
android:layout_marginBottom="@dimen/_5sdp"
android:textColor="#4C4F4F"/>
<LinearLayout
android:background="@drawable/round_corner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/cancel_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/_20sdp"
android:paddingTop="@dimen/_8sdp"
android:paddingRight="@dimen/_20sdp"
android:text="Cancel"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/rounded_edittext"
android:backgroundTint="#F3F3F3"
android:fontFamily="@font/poppins_bold"
android:gravity="center"
android:layout_marginBottom="@dimen/_5sdp"
android:textColor="#7D7D7D"/>
<TextView
android:id="@+id/confirmation_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:backgroundTint="#1AB7B8"
android:layout_gravity="center_horizontal"
android:paddingLeft="@dimen/_20sdp"
android:paddingTop="@dimen/_8sdp"
android:clickable="true"
android:paddingRight="@dimen/_20sdp"
android:text="Sign-Up"
android:textSize="14dp"
android:fontFamily="@font/poppins_bold"
android:gravity="center"
android:layout_marginBottom="@dimen/_5sdp"
android:textColor="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
到目前为止一切正常,但我想设置文本视图“cancel_reg”和“confirmation_reg”而不是“setPositiveButton”和“setNeutralButton”
我试图在 activity 中这样做:
fav_btn.setOnClickListener {
if(token.equals("false")){
val builder = AlertDialog.Builder(this)
val optionDialog = AlertDialog.Builder(this).create()
builder.setView(R.layout.alert_reg)
val goreg = findViewById<TextView>(R.id.confirmation_reg)
val cancel = findViewById<TextView>(R.id.cancel_reg)
goreg?.setOnClickListener {
val intent = Intent(this, SignupActivity::class.java)
startActivity(intent)
}
cancel?.setOnClickListener {
val intent = Intent(this, SignupActivity::class.java)
optionDialog.dismiss()
}
builder.show()
}
else {
if (isFavourite)deleteFavorite(eventId)
else postFavImage(eventId)
}
}
我试图从视图中删除“setPositiveButton”和“setNeutralButton”并用文本视图“confirmation_reg”和“cancel_reg”替换它们,但我无法单击它们
我做错了什么吗?
尝试在 xml 中将 clickable 属性设置为 true。
您需要在 AlertDialog
的 View
上调用 findViewById
目前正在您的 Activity
或 Fragment
上调用它。要解决此问题,请更新您的对话框创建代码,如下所示
val builder = AlertDialog.Builder(requireContext())
// Inflate the XML with layout inflator and keep a reference to find TextView's
val dialogView = layoutInflater.inflate(R.layout.alert_reg, null)
builder.setView(dialogView)
// Find TextView's using the dialogView
val goreg = dialogView.findViewById<TextView>(R.id.confirmation_reg)
val cancel = dialogView.findViewById<TextView>(R.id.cancel_reg)
goreg?.setOnClickListener {
// Do something
}
cancel?.setOnClickListener {
// Do something
}
builder.show()
我正在显示来自 Activity 的 AlertDialog OnClickListener:
fav_btn.setOnClickListener {
if(token.equals("false")){
val builder = AlertDialog.Builder(this)
val optionDialog = AlertDialog.Builder(this).create()
builder.setView(R.layout.alert_reg)
builder.setPositiveButton(android.R.string.yes) { dialog, which ->
val intent = Intent(this, SignupActivity::class.java)
startActivity(intent)
}
builder.setNeutralButton(android.R.string.no) { dialog, which ->
optionDialog.dismiss()
}
builder.show()
}
else{
if (isFavourite)deleteFavorite(eventId)
else postFavImage(eventId)
}
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<LinearLayout
android:background="@drawable/round_corner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Assalamualaikum Qalbies"
android:textSize="19dp"
android:fontFamily="@font/poppins_bold"
android:gravity="center"
android:layout_marginTop="@dimen/_20sdp"
android:textColor="#AC6AA6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/_20sdp"
android:paddingTop="@dimen/_8sdp"
android:paddingRight="@dimen/_20sdp"
android:text="Text"
android:textSize="14dp"
android:fontFamily="@font/poppins"
android:gravity="center"
android:layout_marginBottom="@dimen/_5sdp"
android:textColor="#1AB7B8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/_20sdp"
android:paddingTop="@dimen/_8sdp"
android:paddingRight="@dimen/_20sdp"
android:text="To continue, please"
android:textSize="14dp"
android:fontFamily="@font/poppins"
android:gravity="center"
android:layout_marginBottom="@dimen/_5sdp"
android:textColor="#4C4F4F"/>
<LinearLayout
android:background="@drawable/round_corner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/cancel_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/_20sdp"
android:paddingTop="@dimen/_8sdp"
android:paddingRight="@dimen/_20sdp"
android:text="Cancel"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/rounded_edittext"
android:backgroundTint="#F3F3F3"
android:fontFamily="@font/poppins_bold"
android:gravity="center"
android:layout_marginBottom="@dimen/_5sdp"
android:textColor="#7D7D7D"/>
<TextView
android:id="@+id/confirmation_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:backgroundTint="#1AB7B8"
android:layout_gravity="center_horizontal"
android:paddingLeft="@dimen/_20sdp"
android:paddingTop="@dimen/_8sdp"
android:clickable="true"
android:paddingRight="@dimen/_20sdp"
android:text="Sign-Up"
android:textSize="14dp"
android:fontFamily="@font/poppins_bold"
android:gravity="center"
android:layout_marginBottom="@dimen/_5sdp"
android:textColor="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
到目前为止一切正常,但我想设置文本视图“cancel_reg”和“confirmation_reg”而不是“setPositiveButton”和“setNeutralButton”
我试图在 activity 中这样做:
fav_btn.setOnClickListener {
if(token.equals("false")){
val builder = AlertDialog.Builder(this)
val optionDialog = AlertDialog.Builder(this).create()
builder.setView(R.layout.alert_reg)
val goreg = findViewById<TextView>(R.id.confirmation_reg)
val cancel = findViewById<TextView>(R.id.cancel_reg)
goreg?.setOnClickListener {
val intent = Intent(this, SignupActivity::class.java)
startActivity(intent)
}
cancel?.setOnClickListener {
val intent = Intent(this, SignupActivity::class.java)
optionDialog.dismiss()
}
builder.show()
}
else {
if (isFavourite)deleteFavorite(eventId)
else postFavImage(eventId)
}
}
我试图从视图中删除“setPositiveButton”和“setNeutralButton”并用文本视图“confirmation_reg”和“cancel_reg”替换它们,但我无法单击它们
我做错了什么吗?
尝试在 xml 中将 clickable 属性设置为 true。
您需要在 AlertDialog
的 View
上调用 findViewById
目前正在您的 Activity
或 Fragment
上调用它。要解决此问题,请更新您的对话框创建代码,如下所示
val builder = AlertDialog.Builder(requireContext())
// Inflate the XML with layout inflator and keep a reference to find TextView's
val dialogView = layoutInflater.inflate(R.layout.alert_reg, null)
builder.setView(dialogView)
// Find TextView's using the dialogView
val goreg = dialogView.findViewById<TextView>(R.id.confirmation_reg)
val cancel = dialogView.findViewById<TextView>(R.id.cancel_reg)
goreg?.setOnClickListener {
// Do something
}
cancel?.setOnClickListener {
// Do something
}
builder.show()