Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Dialog - confused
    primarykey
    data
    text
    <p>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).</p> <p>If I set up the positive/negative/neutral actions with something like finish(), cancel() etc, it works with those calls.</p> <p>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.</p> <p>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.</p> <p>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!</p> <p>This code presents the dialog with 3 buttons. This is just one of the try's I made (integer return stuff deleted)... </p> <p><strong>What can I do to return an integer to the Maincode from a dialog button?</strong> There are no code errors or warnings, it just doesn't work as I hoped...</p> <pre><code> 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 } }); </code></pre> <p>builder.show(); // show the dialog }//end Dialog }//end activity</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload