开发者

Displaying custom dialog

开发者 https://www.devze.com 2023-01-16 20:26 出处:网络
Alright, so I would like to have a custom dialog, but I cannot figure out for the life of me how to make it appear when the function is called.

Alright, so I would like to have a custom dialog, but I cannot figure out for the life of me how to make it appear when the function is called.

public void addHomework() {
    final Dialog alert = new Dialog(this);  

    alert.setTitle("Add Homework");

    alert.setContentView(R.开发者_开发技巧layout.homework_item_entry);

    Button add_button = (Button) findViewById(R.id.add_homework_button);
    Button cancel_button = (Button) findViewById(R.id.cancel_homework_button);

    add_button.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(ClassHomeworkList.this, "Adding homework", Toast.LENGTH_SHORT).show();
        }
    });

    cancel_button.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            alert.dismiss();
        }
    });

    alert.show();
}

What could I do?


I know this is an old thread, but even after reading the Android docs it also was not obvious to me how to display a custom dialog using the standard Dialog class. Basically you can call:

this.showDialog(MANAGE_PASSWORD); // MANAGE_PASSWORD static final int

from your activity. Then instantiate the custom dialog in the onCreateDialog method:

   protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case MANAGE_PASSWORD:
            dialog= getInstancePasswordDialog();
            break;
        case DIALOG_ABOUT:
            // do the work to define the About Dialog
            dialog= getInstanceAlertDialog(); // called "the first time"
            break;
        default:
            dialog = null;
        }
        return dialog;
    }

The code to instantiate the dialog is in getInstancePasswordDialog(). Here is the code sample.


I think you have the problem that your two buttons cannot be found by their ID's like this (as you are trying to find them in your main activity, but they are in the layout for the dialog)

Button add_button = (Button) findViewById(R.id.add_homework_button);
Button cancel_button = (Button) findViewById(R.id.cancel_homework_button);

But instead need to do:

Button add_button = (Button) alert.findViewById(R.id.add_homework_button);
Button cancel_button = (Button) alert.findViewById(R.id.cancel_homework_button);


Have you read the following document: http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog ?

You should override your Activity's onCreateDialog(int) method as described there and then use showDialog(int)


LayoutInflater factory = LayoutInflater.from(this);
View view = factory.inflate(R.layout.dialog, null);

//the id is your root layout
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
alert.setContentView(layout);
0

精彩评论

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

关注公众号