Android Dialog - confused

2019-06-11 10:41发布

I've been trying to get a handle on Dialogs. I read the android dev site info and a dozen bolg's. It seems there are several ways of doing it and I'm able to at least get a dialog with 3 buttons (not using a custom dialog with custom layout).

If I set up the positive/negative/neutral actions with something like finish(), cancel() etc, it works with those calls.

But, what I want to do is have the buttons do something more, if only display a text using a string defined in the Maincode (not a Toast). Eventually, I want to enter some numbers in a dialog and return them in a string. Yes, I can do that from another activity screen but, prefer not to as I like the compact size of a dialog.

Even cheating and returning an integer to the Maincode to do some switch/case stuff would be okay but, I seem not able to even return an integer.

I understand that I'll need to do a customized alert dialog to do input stuff and the following is my attempt at a start by just trying to return a string or integer - it doesn't seem to be getting me there!

This code presents the dialog with 3 buttons. This is just one of the try's I made (integer return stuff deleted)...

What can I do to return an integer to the Maincode from a dialog button? There are no code errors or warnings, it just doesn't work as I hoped...

    public class Maincode extends Activity {
public static String rtndMessage = "Push Button";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TextView text = (TextView) findViewById(
            R.id.TextView01);
    final Button doIt = (Button) findViewById(R.id.Button01);

    //  -----------------------------------------------------
    doIt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // do something
            Dialog1();          // call Dialog1
        }
    }); // end -----------------------------------------------
// do the rest of the code (ie, display the result of doIt)
text.setText(rtndMessage);  // set text w/result
}//end onCreate ----------------------------------------------

public void Dialog1() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Dialog Test");
builder.setIcon(R.drawable.redtest);
// chain the following together
  builder.setMessage("Send text: Yes, No, Cancel")
    // the positive action ----------------------------------
    .setPositiveButton("Yes Action", new
        DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){            
            Maincode.rtndMessage = "sent Yes back";             
        }
    })
    // The negative action ----------------------------------
    .setNegativeButton("No Action", new
            DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
            Maincode.rtndMessage = "sent No back";
        }
    })
    // The negative action ------------------------------
    .setNeutralButton("Cancel", new
            DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
            Maincode.rtndMessage = "sent N/A back";
            dialog.cancel();    // just return to activity
        }           
});    

builder.show(); // show the dialog }//end Dialog }//end activity

3条回答
冷血范
2楼-- · 2019-06-11 11:32

Android 1.6+ provides an easy to use set of dialog functions for your Activity.

When you make the dialogs, just do whatever you have to do in the onClick() methods of the dialog's buttons. You can see an example I have of this below, in the DIALOG_CONFIRM_DELETE switch where I delete a record for a local database and requery a cursor.

I'd take a look at the "Creating Dialogs" section in the dev guide on http://d.android.com It shows you how to use the dialog functions. http://developer.android.com/guide/topics/ui/dialogs.html

@Override
protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    switch (id) {
        case DIALOG_CONFIRM_DELETE:
            builder
                .setTitle("Confirm")
                .setMessage("Are you sure you want to delete that access point?")
                .setCancelable(true)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mDbAdapter.delete(SELECTED_AP_ID);
                        mCursor.requery();
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
            return builder.create();
        case DIALOG_EXPORTING:
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("Exporting...");
            progressDialog.setCancelable(false);
            progressDialog.show();
            return progressDialog;
        case DIALOG_GPS_DISABLED:
            builder
                .setTitle("GPS signal not Found")
                .setMessage("GPS is not enabled, and accuracy may be effected.")
                .setCancelable(false)
                .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        startService();
                        dialog.dismiss();
                    }
                });
            return builder.create();
        default:
            return null;
    }
}
查看更多
走好不送
3楼-- · 2019-06-11 11:38

This is VERY late, but hopefully it will help others in future. What I did was created a custom dialog that implemented OnClickListener:

public class LoginDialog extends Dialog implements OnClickListener{

    public LoginDialog(Context context) {
    super(context);
    this.context = context;
    // TODO Auto-generated constructor stub

    }

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog);
    setTitle("Enter Login Details");
    setCancelable(true);

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    bLogin = (Button) findViewById(R.id.bDialogLogin);
    bCancel = (Button) findViewById(R.id.bDialogCancel);

    bLogin.setOnClickListener(this);
    bCancel.setOnClickListener(this);


   }

    public void onClick(View v) {

    switch (v.getId()) {
    case R.id.bDialogLogin:
        //Check credentials.  If they pass:
        cancel();

        //if they don't pass, throw an alert or something
      break;

    case R.id.bDialogCancel:
        dismiss();
      break;
    }

    }
}

Then in my main code I implemented an onCancelListener (dialogs have onCancel and onDismiss listeners all prepared for you).

//This calls my alert dialog.  Place this in the onClick of a button or something
loginDialog = new LoginDialog(this);
loginDialog.show();


loginDialog.setOnCancelListener(new OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            // TODO Auto-generated method stub

            //This code is run whenever the cancel(); function in the dialog is called.

        }
    });

Hope this helps!

查看更多
迷人小祖宗
4楼-- · 2019-06-11 11:47

You can implement the dialog as separate Activity and get any behavior you need. Just apply standard android Theme.Dialog theme to the activity to make it look like dialog. Also you can create theme of your own pointing Theme.Dialog as parent.

查看更多
登录 后发表回答