首页 > 解决方案 > 来自片段的自定义对话框片段

问题描述

我已经创建了一个约束布局,我正在从一个片段中调用一个对话框片段。但是当我点击按钮时,它所做的只是淡化屏幕,但我看不到任何对话框或布局。

这是我的对话框片段代码:

package com.example.atry.MakeComplaint

import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment

import com.example.atry.R


class PopupDialog : DialogFragment() {



    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {



        val builder = AlertDialog.Builder(activity)


        return builder.create()

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val view = inflater.inflate(R.layout.existing_complaint_popup, container, false)

        val yes = view.findViewById(R.id.YES) as Button
        val no = view.findViewById(R.id.NO) as Button

        // set onclicklistener
        yes.setOnClickListener(View.OnClickListener {
            // I want the dialog to close at this point
            dismiss()
            backFragment()

        })

        // set onclicklistener
        no.setOnClickListener(View.OnClickListener {
            // I want the dialog to close at this point
            dismiss()
            descriptionFragment()

        })

        return view
    }




    private fun backFragment() {
        val manager = (context as AppCompatActivity).supportFragmentManager
        manager.popBackStackImmediate()


    }

    private fun descriptionFragment() {
        val dFragment= Category_Description()
        val lFragment = LocationFragment()
        val manager = (context as AppCompatActivity).supportFragmentManager
        val transaction =  manager.beginTransaction()
        transaction.replace(
            R.id.location_screen,
            dFragment
        ) // give your fragment container id in first parameter
        transaction.show(dFragment)
        transaction.hide(lFragment)
        transaction.isAddToBackStackAllowed
        transaction.addToBackStack(lFragment.fragmentManager.toString())  // if written, this transaction will be added to backstack
        transaction.commit()
    }







}

这是我从中调用对话框的片段中的代码:

 //opening up the checkExisting popup

    private fun existPopup(){
        val fm = activity!!.supportFragmentManager
        val dialog = PopupDialog() // creating new object
        dialog.show(fm, "dialog")
    }

这是我的布局:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
            android:id="@+id/textViewComplaint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/complainDescription"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/textViewComplaint"
            >



        <TextView
                android:id="@+id/headingComplain"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="10dp"
                android:layout_weight="6"
                android:fontFamily="@font/pathway_gothic_one"
                android:gravity="center"
                android:padding="10dp"
                android:text="Is this the complaint you're looking for?"
                android:textColor="@color/white"
                android:textSize="30sp"
                />


    </LinearLayout>

    <RelativeLayout
            android:id="@+id/fazool"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentBottom="true"
            android:background="@color/grey"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/textViewComplaint"
            android:background="@drawable/bottom_nav_border"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textViewComplaint"
            app:layout_constraintBottom_toBottomOf="parent"
            >
        <ScrollView
                android:layout_height="wrap_content"
                android:layout_width = "match_parent">


            <include layout="@layout/existing_complaint_popup_inner"/>
        </ScrollView>

    </FrameLayout>



</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidkotlinandroid-dialogfragment

解决方案


那是我的自定义通知对话框代码。我认为这很容易和清楚。我不知道你为什么用片段自定义对话框,但你可以使用我的方式。

 val mDialogView = LayoutInflater.from(this).inflate(R.layout.permission_for_notification, null)
        //AlertDialogBuilder
        val mBuilder = AlertDialog
            .Builder(this)
            .setView(mDialogView)
        //show dialog
        val  mAlertDialog = mBuilder.show()

 mAlertDialog.window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)


        mDialogView.notification_accept_button.setOnClickListener {

            mAlertDialog.dismiss()
        }

        //cancel button click of custom layout
        mDialogView.notification_reject_button.setOnClickListener {

            mAlertDialog.dismiss()
        }

推荐阅读