android - 如何在 ViewPager 的 fragment 页面内单击按钮时弹出窗口?使用 Kotlin

标签 android kotlin android-viewpager android-popupwindow

我的应用程序有一个包含三个 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/

相关文章:

android - AppCompat 工具栏字体大小在纵向与横向中不一致

android-studio - 运行时 JUnit 测试中 Kotlin 类出现 NoClassDefFoundError

android - 如何解决软键盘显示/隐藏工作缓慢?

android - 图像存在,不为空,但我得到一个 NullPointerException

kotlin - Mapbox 安卓 : Adjust Opacity of background layer

android - Viewpager 中的 map v2 SupportMapFragment

android - 销毁以前的 ViewPager 项目以防止向后滚动

android - 包含 ToggleButton 时 AutoCompleteTextView 项目不可单击

android - 硬件加速 Activity - 如何获得 OpenGL 纹理大小限制?

android - 在屏幕中央对齐控件