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