开发者

Android Custom Dialog not Showing

开发者 https://www.devze.com 2023-03-16 19:54 出处:网络
I want to show a simple custom dialog. For starters I simply wanted to add a text view and see if the dialog show.

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 开发者_JS百科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


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消