Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android自定义对话框_Android_Dialog - Fatal编程技术网

Android自定义对话框

Android自定义对话框,android,dialog,Android,Dialog,我正在尝试按照创建一个自定义对话框,但每次尝试显示该对话框时,它都会崩溃。这是我的密码: Context mContext = getApplicationContext(); Dialog dialog = new Dialog(mContext); dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Custom Dialog"); dialog.show(); 下面是我的布局XML: <?xml vers

我正在尝试按照创建一个自定义对话框,但每次尝试显示该对话框时,它都会崩溃。这是我的密码:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
dialog.show();
下面是我的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:id="@+id/btnConfirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add text"
        android:layout_below="@+id/txtNewText"
        android:layout_alignParentLeft="true">
    </Button>
    <EditText
        android:id="@+id/txtNewText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">
    </EditText>
</RelativeLayout>

考虑以下模式:

private static final int MY_DIALOG= 0;

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
        case MY_DIALOG:
            dialog= getInstanceMyDialog();
            break;
        default:
            dialog = null;
    }
    return dialog;
}

private Dialog getInstanceMyDialog() {
    final Dialog d= new Dialog(this); //<=====THIS
    d.setContentView(R.layout.custom_dialog);
    d.setTitle("Custom Dialog");
    return d;
}
private static final int MY_DIALOG=0;
受保护的对话框onCreateDialog(int id){
对话;
开关(id){
“我的案例”对话框:
dialog=getInstanceMyDialog();
打破
违约:
dialog=null;
}
返回对话框;
}
专用对话框getInstanceMyDialog(){
最终对话d=新对话(此);//这对我来说很有效:

实例化对话框时,请使用此选项,而不是getApplicationContext():

Dialog dialog = new Dialog(this);

Kotlin在android中创建自定义对话框的方法:

Dialog(activity!!, R.style.LoadingIndicatorDialogStyle)
        .apply {
            // requestWindowFeature(Window.FEATURE_NO_TITLE)
            setCancelable(true)
            setContentView(R.layout.define_your_custom_view_id_here)

            //access your custom view buttons/editText like below.z
            val createBt = findViewById<TextView>(R.id.clipboard_create_project)
            val cancelBt = findViewById<TextView>(R.id.clipboard_cancel_project)
            val clipboard_et = findViewById<TextView>(R.id.clipboard_et)
            val manualOption =
                findViewById<TextView>(R.id.clipboard_manual_add_project_option)

            //if you want to perform any operation on the button do like this

            createBt.setOnClickListener {
                //handle your button click here
                val enteredData = clipboard_et.text.toString()
                if (enteredData.isEmpty()) {
                    Utils.toast("Enter project details")
                } else {
                    navigateToAddProject(enteredData, true)
                    dismiss()
                }
            }

            cancelBt.setOnClickListener {
                dismiss()
            }
            manualOption.setOnClickListener {
                navigateToAddProject("", false)
                dismiss()
            }
            show()
        }
对话框(活动!!,R.style.LoadingIndicator或AlogStyle)
.申请{
//requestWindowFeature(窗口。功能\u无\u标题)
可设置可取消(真)
setContentView(R.layout.define_your_custom_view_id_here)
//访问自定义视图按钮/editText,如下所示.z
val createBt=findviewbyd(R.id.clipboard\u create\u项目)
val cancelBt=findviewbyd(R.id.clipboard\u cancel\u项目)
val clipboard\u et=findviewbyd(R.id.clipboard\u et)
val手动选项=
findViewById(R.id.剪贴板\手动\添加\项目\选项)
//如果要对按钮执行任何操作,请执行以下操作
createBt.setOnClickListener{
//处理你的按钮点击这里
val enteredData=剪贴板_et.text.toString()
if(enteredData.isEmpty()){
Utils.toast(“输入项目详细信息”)
}否则{
navigateToAddProject(输入数据,真)
解雇
}
}
cancelBt.setOnClickListener{
解雇
}
manualOption.setOnClickListener{
navigateToAddProject(“,false)
解雇
}
show()
}
在style.xml中创建LoadingIndicatorDialogStyle

<style name="LoadingIndicatorDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:statusBarColor">@color/black_transperant</item>
<item name="android:layout_gravity">center</item>
<item name="android:background">@android:color/transparent</item>
<!--<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>-->

真的
@android:彩色/透明
@空的
真的
@彩色/黑色透明剂
居中
@android:彩色/透明

您能提供错误日志吗?@ElecO您提供的链接使用此模式创建自定义对话框。最大的区别是教程调用showDialog(MY_dialog),而您发布的代码使用dialog.show()。我的工作代码是:为什么会发生这种情况?有时我想在单击按钮后显示一个对话框。如果我使用“this”而不是“getApplicationContext()”不起作用,因为“this”指的是“OnClickListener”。我必须做一些难看的变通来使用“this”…不,你不能只写类名。这个类应该是这个