Android自定义对话框未显示

时间:2011-07-03 11:39:45

标签: android customdialog

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

这是我的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:以防万一没有任何错误或警告。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

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

/** 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();