我的应用程序有一个包含三个 Fragment 作为页面的 ViewPager Activity ,每个页面都有一些按钮。 所以我的问题是,当单击 fragment 页面内的这些按钮时是否可以弹出消息或对话框。 谢谢。
最佳答案
在您的 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>
关于android - 如何在 ViewPager 的 fragment 页面内单击按钮时弹出窗口?使用 Kotlin ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59563186/