1

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

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

谢谢你的帮助

4

1 回答 1

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();
于 2011-07-03T12:15:32.197 回答