Android 自定义对话框未显示

发布于 2024-11-18 09:32:51 字数 951 浏览 4 评论 0原文

我想显示一个简单的自定义对话框。对于初学者来说,我只想添加一个文本视图并查看对话框是否显示。

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tvPreview" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:text="@string/Instructions"></TextView>
</LinearLayout>

这是我的 onCreateDialog 功能代码:

@Override
protected Dialog onCreateDialog(int id) {
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.predialog);
    dialog.setTitle("Tests of my Dialog");
    return dialog;
}

当用户(我)按下菜单项时,我使用以下代码:

public void DiagTests(){
    showDialog(0);
}

发生的情况是屏幕模糊,但对话框不显示。

有人知道我做错了什么吗?

PD:以防万一没有任何错误或警告。

感谢您的帮助

I want to show a simple custom dialog. For starters I simply wanted to add a text view and see if the dialog show.

This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tvPreview" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:text="@string/Instructions"></TextView>
</LinearLayout>

This is my code for the onCreateDialog Function:

@Override
protected Dialog onCreateDialog(int id) {
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.predialog);
    dialog.setTitle("Tests of my Dialog");
    return dialog;
}

When the user (me) presses a menu item the I use this code:

public void DiagTests(){
    showDialog(0);
}

What happens is that the screen obscures but the dialog doesn't show.

Does anyone have any idea of what I'm doing wrong?

PD: Just in case there are no errors or warnings of any kind.

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

药祭#氼 2024-11-25 09:32:51

你可以尝试一下这个方法。创建一个自定义对话框类(这是一个类的示例,您可以使用您想要的):

/** Class Must extends with Dialog */
/** Implement onClickListener to dismiss dialog when OK Button is pressed */
public class DialogWithSelect extends Dialog implements OnClickListener {

private String _text;
Button okButton;
Button cancelButton;
/**
 * ProgressDialog that will be shown during the loading process
 */
private              ProgressDialog            myDialog;

public DialogWithSelect getDialog() {
    return this;
}

public String getText() {
    return this._text;
}

public DialogWithSelect(Context context) {
    super(context);
     myDialog = new ProgressDialog(this.getContext());
     myDialog.setMessage("Exporting file...");
    /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    /** Design the dialog in main.xml file */
    setContentView(R.layout.dialog_with_select_box);

     final Spinner hubSpinner = (Spinner) findViewById(R.id.spinnerSelectFormat);
     ArrayAdapter adapter = ArrayAdapter.createFromResource( this.getContext(), R.array.spinner , android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     hubSpinner.setAdapter(adapter);


    okButton = (Button) findViewById(R.id.okButton);
    cancelButton = (Button) findViewById(R.id.cancelButton);

      okButton.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v) {
            //whatever  
            }

        });



    cancelButton.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            //Get the text from the texString to paint on the Canvas
            getDialog().hide();
        }

    }
);


}

在要使用它的类上定义对话框:

final DialogWithSelect dialog = new DialogWithSelect(getContext());
dialog.setTitle(R.string.dialogSelectBoxText);

并在单击事件中启动它:

dialog.show();

You could try this approach. Create a Custom Dialog class (this is an example of a class, you can use what you want):

/** Class Must extends with Dialog */
/** Implement onClickListener to dismiss dialog when OK Button is pressed */
public class DialogWithSelect extends Dialog implements OnClickListener {

private String _text;
Button okButton;
Button cancelButton;
/**
 * ProgressDialog that will be shown during the loading process
 */
private              ProgressDialog            myDialog;

public DialogWithSelect getDialog() {
    return this;
}

public String getText() {
    return this._text;
}

public DialogWithSelect(Context context) {
    super(context);
     myDialog = new ProgressDialog(this.getContext());
     myDialog.setMessage("Exporting file...");
    /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    /** Design the dialog in main.xml file */
    setContentView(R.layout.dialog_with_select_box);

     final Spinner hubSpinner = (Spinner) findViewById(R.id.spinnerSelectFormat);
     ArrayAdapter adapter = ArrayAdapter.createFromResource( this.getContext(), R.array.spinner , android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     hubSpinner.setAdapter(adapter);


    okButton = (Button) findViewById(R.id.okButton);
    cancelButton = (Button) findViewById(R.id.cancelButton);

      okButton.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v) {
            //whatever  
            }

        });



    cancelButton.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            //Get the text from the texString to paint on the Canvas
            getDialog().hide();
        }

    }
);


}

Define the dialog on the class where it is going to be used:

final DialogWithSelect dialog = new DialogWithSelect(getContext());
dialog.setTitle(R.string.dialogSelectBoxText);

And launch it in the click event:

dialog.show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文