To display different dialogs, you have couple options.

1.  Continue to use dialogs managed by the activity, but pass
different ids in showDialog(int), and in your onCreateDialog(int id),
check the parameter id and return the appropriate dialog.  In your
current code, you're passing 0 in both bt1 and but2.  Pass different
ids.  Also in your onCreateDialog, you're dismissing the dialog
immediately, which doesn't make sense.  You should be returning the
dialog instance you just created.

2.  Create a new dialog every time an event happens.  In your case,
create() and show() a new dialog instance in bt1 or bt2 onClickListener
() event.

On Sep 5, 12:46 am, 陈增顺 <[email protected]> wrote:
> Hi,  I am trying to reuse a dialog to show different message in different
> cases.
>   But it seems that Android will cache the content, when the dialog was
> created.
>
>   Here is my codes, the dialog just show the content set by the the first
> clicked button.
> ------------------------------------------------------------------------------------------------------------------------------
>   package com.mytest;
>
> import android.app.Activity;
> import android.app.AlertDialog;
> import android.app.Dialog;
> import android.content.DialogInterface;
> import android.os.Bundle;
> import android.view.View;
> import android.view.View.OnClickListener;
> import android.widget.Button;
>
> public class myTest extends Activity {
> public static String errTitle = "";
> public static String errMessage = "";
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>         Button bt1 = (Button)findViewById(R.id.Button01);
>         Button bt2 = (Button)findViewById(R.id.Button02);
>         bt1.setOnClickListener(new OnClickListener(){
>          public void onClick(View v){
>          errTitle = "H1";
>          errMessage = "M1";
>          showDialog(0);
>          }
>         });
>         bt2.setOnClickListener(new OnClickListener(){
>          public void onClick(View v){
>          errTitle = "H2";
>          errMessage = "M2";
>          showDialog(0);
>          }
>         });
>     }
>
> protected Dialog onCreateDialog(int id) {
> AlertDialog dialog = null;
> switch (id){
> case 0:
> dialog =  new AlertDialog.Builder(this)
>         .setTitle(errTitle)
>         .setMessage(errMessage)
>         .setPositiveButton(
> "Confirm",
> new DialogInterface.OnClickListener() {
> public void onClick(DialogInterface dialog,
> int whichButton) {
> dialog.dismiss();
> return;
>
> }
> }).create();
> }
> return dialog;
> }
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to