Пользовательский диалог Android не отображается - PullRequest
0 голосов
/ 03 июля 2011

Я хочу показать простой пользовательский диалог.Для начала я просто хотел добавить текстовое представление и посмотреть, отображается ли диалоговое окно.

Это мой 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 Ответ

1 голос
/ 03 июля 2011

Вы можете попробовать этот подход. Создайте класс Custom Dialog (это пример класса, вы можете использовать то, что вы хотите):

/** 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();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...