开发者

How to catch selected value from Android Menu Radio Values

开发者 https://www.devze.com 2023-01-05 03:16 出处:网络
I\'ve created a 开发者_开发百科radio button menu list .. final CharSequence[] items = { \"3 sec\", \"5 sec\", \"7 sec\" };

I've created a 开发者_开发百科radio button menu list ..

    final CharSequence[] items = { "3 sec", "5 sec", "7 sec" };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Change Wallpaper Every..");
    builder.setSingleChoiceItems(items, -1,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item],
                            Toast.LENGTH_SHORT).show();
                }
            }); 

    AlertDialog alert = builder.create();
    builder.show();

what I want to do is to catch the selected value from the above Char Sequence and to put to a variable .


The selected item is the one with the index item, the corresponding String is items[item].

A typical pattern to catch a selection is like this:

public class SomeDialog {

   final CharSequence[] items = { "3 sec", "5 sec", "7 sec" };
   int choice = -1; 

   //...

   public void someMethod() {
     //...
     builder.setSingleChoiceItems(items, -1,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item],
                            Toast.LENGTH_SHORT).show();
                    choice = item;  // <-- now we store the selection to a class member
                }
            }); 
      // ... 
   }

   public String getSelection() {
     return items[choice];  // <-- this allows another class to read the current selection
                            //     often called after the dialog has been closed
   }
}

EDIT

An alternative would be making the Dialog (or part of the Dialog) observable. The Dialog would fire an event whenever the selction changed and anyone interested in the selected value would listen to this event source.


I use factories for this kind of things:

public interface ChoiceListener {
    void onPositiveResult(String choice);
}

public static AlertDialog getWallpaperDialog(Context ctx,
        final String[] options, int choice, final ChoiceListener listener) {
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    // set title, buttons, etc.
    builder.setSingleChoiceItems(options, choice, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            listener.onPositiveResult(options[which]);
            dialog.dismiss();
        }
    });
    return builder.create();
}

Suggestions: store string arrays in resources (e.g. strings.xml) & stick to indexes instead of strings for preferences.


I've tried with If ... else

First, I declare a private variable with a default value

private static int myChoice = 3000; //3000 is Millisecond = 3 sec 

Then, I've added a condition after Toast.makeText( )

                final CharSequence[] items = { "3 sec", "5 sec", "7 sec", "10 sec" }
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item],
                            Toast.LENGTH_SHORT).show();
                    if (items[item] == "3 sec") {
                        myChoice = 3000;
                    } else if (items[item] == "5 sec") {
                        myChoice = 5000;
                    } else if (items[item] == "7 sec") {
                        myChoice = 7000;
                    } else if (items[item] == "10 sec") {
                        myChoice = 10000;
                    }
                }

I got the value in this way.

0

精彩评论

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