如何在 ViewPager 的片段页面内单击按钮弹出窗口?使用科特林
How to make a popup on a button click inside a fragment page of ViewPager ? Using Kotlin
我的应用程序有一个 activity,ViewPager 由三个 Fragment 作为页面,每个页面都有一些按钮。
所以我的问题是,单击片段页面内的这些按钮时是否可以弹出消息或对话框。
谢谢。
您希望在您的片段中显示一个对话框:
fun showDialog() {
val dialogBuilder = AlertDialog.Builder(context)
dialogBuilder.setMessage("The message here")
dialogBuilder.setPositiveButton("Done",
DialogInterface.OnClickListener { dialog, whichButton -> })
val b = dialogBuilder.create()
b.show()
}
按钮的 OnClick 侦听器:
btn.setOnClickListener{ showDialog() }
如果您希望有一个自定义对话框,您可以执行以下操作:
private lateinit var alertDialog: AlertDialog
fun showCustomDialog() {
val inflater: LayoutInflater = this.getLayoutInflater()
val dialogView: View = inflater.inflate(R.layout.dialog_custom_view, null)
val header_txt = dialogView.findViewById<TextView>(R.id.header)
header_txt.text = "Header Message"
val details_txt = dialogView.findViewById<TextView>(R.id.details)
val custom_button: Button = dialogView.findViewById(R.id.customBtn)
custom_button.setOnClickListener {
//perform custom action
}
val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(context!!)
dialogBuilder.setOnDismissListener(object : DialogInterface.OnDismissListener {
override fun onDismiss(arg0: DialogInterface) {
}
})
dialogBuilder.setView(dialogView)
alertDialog = dialogBuilder.create();
alertDialog.window!!.getAttributes().windowAnimations = R.style.PauseDialogAnimation
alertDialog.show()
}
dialog_custom_view.xml 布局可以是您想要的任何内容,例如
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/content"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/header"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:ellipsize="end"
android:textColor="@color/black"
android:textSize="17sp"
android:text="Header"
android:maxLines="3"
android:textAlignment="center"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:textAllCaps="false"/>
<TextView
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:text="Details here set multiple lines if you want"
android:paddingEnd="8dp"
android:paddingStart="8dp"/>
<Button
android:id="@+id/customBtn"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginBottom="15dp"
android:text="Custom Btn" />
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
我的应用程序有一个 activity,ViewPager 由三个 Fragment 作为页面,每个页面都有一些按钮。 所以我的问题是,单击片段页面内的这些按钮时是否可以弹出消息或对话框。 谢谢。
您希望在您的片段中显示一个对话框:
fun showDialog() {
val dialogBuilder = AlertDialog.Builder(context)
dialogBuilder.setMessage("The message here")
dialogBuilder.setPositiveButton("Done",
DialogInterface.OnClickListener { dialog, whichButton -> })
val b = dialogBuilder.create()
b.show()
}
按钮的 OnClick 侦听器:
btn.setOnClickListener{ showDialog() }
如果您希望有一个自定义对话框,您可以执行以下操作:
private lateinit var alertDialog: AlertDialog
fun showCustomDialog() {
val inflater: LayoutInflater = this.getLayoutInflater()
val dialogView: View = inflater.inflate(R.layout.dialog_custom_view, null)
val header_txt = dialogView.findViewById<TextView>(R.id.header)
header_txt.text = "Header Message"
val details_txt = dialogView.findViewById<TextView>(R.id.details)
val custom_button: Button = dialogView.findViewById(R.id.customBtn)
custom_button.setOnClickListener {
//perform custom action
}
val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(context!!)
dialogBuilder.setOnDismissListener(object : DialogInterface.OnDismissListener {
override fun onDismiss(arg0: DialogInterface) {
}
})
dialogBuilder.setView(dialogView)
alertDialog = dialogBuilder.create();
alertDialog.window!!.getAttributes().windowAnimations = R.style.PauseDialogAnimation
alertDialog.show()
}
dialog_custom_view.xml 布局可以是您想要的任何内容,例如
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/content"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/header"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:ellipsize="end"
android:textColor="@color/black"
android:textSize="17sp"
android:text="Header"
android:maxLines="3"
android:textAlignment="center"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:textAllCaps="false"/>
<TextView
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:text="Details here set multiple lines if you want"
android:paddingEnd="8dp"
android:paddingStart="8dp"/>
<Button
android:id="@+id/customBtn"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginBottom="15dp"
android:text="Custom Btn" />
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>