android - 来自片段的自定义对话框片段
问题描述
我已经创建了一个约束布局,我正在从一个片段中调用一个对话框片段。但是当我点击按钮时,它所做的只是淡化屏幕,但我看不到任何对话框或布局。
这是我的对话框片段代码:
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()
}
推荐阅读
- bash - 如何用awk替换所有出现的符号
- swift - swift中UIBUTTON中渐变层的问题
- resampling - 应用 10 到 15 分钟值的 Pandas 重新采样
- python - Ngboost 算法可以自动处理缺失值吗?
- python - 如何在 numpy.where 条件后获取特定的数组位置?
- r - varnames 上的重复函数
- python - 为什么汤里没有表格数据标签?
- linux - 删除大量用户的 Bash 脚本未运行
- octobercms - Octobercms 在后端显示画廊的标题和图像
- c++ - 现代编译器内联 Cpp 文件和 PImpl Idiom Overhead