开发者

Adding RadioButtons to ContextMenu

开发者 https://www.devze.com 2023-01-14 16:00 出处:网络
I would like to add radio buttons to my context menu, but I\'m not sure how. This is how it is created:

I would like to add radio buttons to my context menu, but I'm not sure how. This is how it is created:

@Override  
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  

        super.onCreateContextMenu(menu, v, menuInfo);  
        menu.setHeaderTitle("Selection Options");  
        menu.add(0, v.getId(), 0, "Remo开发者_StackOverflow中文版ve");  
    }  


from your code:

menu.add(0, v.getId(), 0, "Remove"); 

v is a View that can be a RadioButton or any other type of Views.

if you are not using xml to define RadioButton. you should set its ID in your application.

v.setId();

Or you can define ids.xml in res/values.

samples/ApiDemos/src/com/example/android/apis/RadioGroup1.java samples/ApiDemp/res/values/ids.xml

Menu & Context Menu developers guide:

http://developer.android.com/guide/topics/ui/menus.html

if you scroll the above page you can find RadioButton sample in context menu.

Quote form the above page:

Checkable menu items

A menu can be useful as an interface for turning options on and off, using a checkbox for stand-alone options, or radio buttons for groups of mutually exclusive options. Figure 2 shows a submenu with items that are checkable with radio buttons.

Note: Menu items in the Icon Menu (from the Options Menu) cannot display a checkbox or radio button. If you choose to make items in the Icon Menu checkable, you must manually indicate the checked state by swapping the icon and/or text each time the state changes.

You can define the checkable behavior for individual menu items using the android:checkable attribute in the element, or for an entire group with the android:checkableBehavior attribute in the element. For example, all items in this menu group are checkable with a radio button:

> <?xml version="1.0" encoding="utf-8"?>
> <menu
> xmlns:android="http://schemas.android.com/apk/res/android">
>     <group android:checkableBehavior="single">
>         <item android:id="@+id/red"
>               android:title="@string/red" />
>         <item android:id="@+id/blue"
>               android:title="@string/blue" />
>     </group> </menu> The android:checkableBehavior attribute

accepts either:

single Only one item from the group can be checked (radio buttons) all All items can be checked (checkboxes) none No items are checkable You can apply a default checked state to an item using the android:checked attribute in the element and change it in code with the setChecked() method.

When a checkable item is selected, the system calls your respective item-selected callback method (such as onOptionsItemSelected()). It is here that you must set the state of the checkbox, because a checkbox or radio button does not change its state automatically. You can query the current state of the item (as it was before the user selected it) with isChecked() and then set the checked state with setChecked(). For example:

> @Override public boolean
> onOptionsItemSelected(MenuItem item) {
> switch (item.getItemId()) {   case
> R.id.vibrate:   case
> R.id.dont_vibrate:
>     if (item.isChecked()) item.setChecked(false);
>     else item.setChecked(true);
>     return true;   default:
>     return super.onOptionsItemSelected(item);   }
> } 

If you don't set the checked state

this way, then the visible state of the item (the checkbox or radio button) will not change when the user selects it. When you do set the state, the Activity preserves the checked state of the item so that when the user opens the menu later, the checked state that you set is visible.

Note: Checkable menu items are intended to be used only on a per-session basis and not saved after the application is destroyed. If you have application settings that you would like to save for the user, you should store the data using Shared Preferences.


RadioButton rBtn1 = new RadioButton(this);
RadioButton rBtn2 = new RadioButton(this);
rBtn1.setText("radio button 1");
rBtn2.setText("radio button 2");
//Add all your RadioButtons the same way.

RadioGroup group = new RadioGroup(this);
group.addView(rBtn1);
group.addView(rBtn2);

menu.add(0, group.getId(), 0, "whatever");

I didn't try it but I hope it works:

0

精彩评论

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