gpt4 book ai didi

android - fragment 中的自定义对话框 fragment

转载 作者:行者123 更新时间:2023-12-02 13:44:25 26 4
gpt4 key购买 nike

我创建了约束布局,并从片段中调用对话框片段。但是,当我单击按钮时,它所做的只是使屏幕褪色,但是看不到任何对话框或布局。

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

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>

最佳答案

多数民众赞成在我的自定义通知对话框代码。我认为那很容易清楚。我不知道为什么要使用片段分割自定义对话框,但是可以使用我的方式。

 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()
}

关于android - fragment 中的自定义对话框 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60275450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com