Java 如何让应用程序显示对话框(AndroidStudio)?
“我的活动”启动时应显示一个对话框 这是我的java类“PrivacyDialog”: 这是在“我的活动”中打开对话框的函数Java 如何让应用程序显示对话框(AndroidStudio)?,java,android,android-studio,dialog,Java,Android,Android Studio,Dialog,“我的活动”启动时应显示一个对话框 这是我的java类“PrivacyDialog”: 这是在“我的活动”中打开对话框的函数 public void openDialog () { PrivacyDialog privacyDialog = new PrivacyDialog(); privacyDialog.show(getSupportFragmentManager(),"PrivacyDialog"); } 我的问题: 每次执行openDi
public void openDialog () {
PrivacyDialog privacyDialog = new PrivacyDialog();
privacyDialog.show(getSupportFragmentManager(),"PrivacyDialog");
}
我的问题:
每次执行openDialog函数时,应用程序都会崩溃。您在自定义对话框中创建警报对话框,这是错误的。您应该在充气机内部提供自定义布局
class MyCustomDialogFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Do all the stuff to initialize your custom view
return inflater.inflate(R.layout.dialog_fragment, container, false)
}
}
解雇
dialogFragment.dismiss();
附言:
因为DialogFragment是一个实际的片段,所以您只需要创建一个片段事务并调用
val dialog: MyCustomDialogFragment = MyCustomDialogFragment()
val fm: FragmentManager = supportFragmentManager
dialog.show(fm, "fragment_dialog")
Xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在自定义对话框中创建警报对话框是错误的。您应该在充气机内部提供自定义布局
class MyCustomDialogFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Do all the stuff to initialize your custom view
return inflater.inflate(R.layout.dialog_fragment, container, false)
}
}
解雇
dialogFragment.dismiss();
附言:
因为DialogFragment是一个实际的片段,所以您只需要创建一个片段事务并调用
val dialog: MyCustomDialogFragment = MyCustomDialogFragment()
val fm: FragmentManager = supportFragmentManager
dialog.show(fm, "fragment_dialog")
Xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
谢谢您的帮助!我按照您的建议更改了代码,但是.show()函数仍然存在问题。AndroidStudio在内容(ft,“对话框”)下方加上红色下划线。好的,现在再试一次。我在我的项目中测试了它,它可以导入androidx.fragment.app.FragmentManager。在ovrride OnViewCreated中定义ui操作感谢您的支持!我的问题是我没有在override OnViewCreate中定义ui操作谢谢你的帮助!我按照您的建议更改了代码,但是.show()函数仍然存在问题。AndroidStudio在内容(ft,“对话框”)下方加上红色下划线。好的,现在再试一次。我在我的项目中测试了它,它可以导入androidx.fragment.app.FragmentManager。在ovrride OnViewCreated中定义ui操作感谢您的支持!我的问题是,我没有在override onViewCreate中定义ui操作