0

如何获取所选单选按钮的文本(我已自定义)

我在一个组中有一组单选按钮,如下所示

  • 早餐
  • 午餐
  • 晚餐

在这些中只有一个会被选中,因为它们是单选按钮组

图像

如何获取所选单选按钮的文本并将它们作为意图传递给下一个活动

有任何想法吗 ?


我知道如何通过从中获取值来将意图传递给下一个活动,Edittext但不能这样做radio button group

我尝试过 的是我如何从编辑文本中获取值::

EditText City;     
Search=(Button) findViewById(R.id.SEARCH_BUTTON_ID);
            Search.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Intent searchIntent=new Intent(SearchPage.this,AndroidTabRestaurantDescSearchListView.class);
                    searchIntent.putExtra("REST",City.getText().toString());
                    startActivity(searchIntent);
                }
            });

正如我在单选按钮组中提到的那样,我怎样才能实现这一点!

4

2 回答 2

1

每组单选按钮都应该在一个组中,我假设您已经在 XML 文件中这样做了。

假设,那么您通过执行以下操作获得选中的单选按钮的 ID:

int id = ((RadioGroup)findViewById( R.id.radio_group )).getCheckedRadioButtonId();

您可以在 Activity 中的任何位置执行此操作,或者如果您使用的是 Fragment,则只需在其中放入一个 getView() :

int id = ((RadioGroup)getView().findViewById( R.id.radio_group )).getCheckedRadioButtonId();
So I would change your onClick:

public void onClick(View v) {
    // Create a new Intent
    Intent i = new Intent(v.getContext(), FeedbackResults.class);

    choice = (String)county.getSelectedItem();
    int id = ((RadioGroup)findViewById( R.id.radio_group )).getCheckedRadioButtonId();
    atmos = getAtmos( id );
    ...
}

private int getAtmos( int id ) {
    switch( id ) {
        case R.id.arad1:
            atmos = "1";
            break;
        case R.id.arad2:
            atmos = "2";
            break;
        case R.id.arad3:
            atmos = "3";
             break;
        case R.id.arad4:
            atmos = "4";
        break;
        case R.id.arad5:
            atmos = "5";
            break;
    }

    return atmos;
}
于 2013-10-11T04:10:57.637 回答
1
// try this
**xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="5dp"
              android:orientation="vertical"
              android:gravity="center" >
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        <RadioGroup
                android:id="@+id/rdg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

            <RadioButton
                    android:id="@+id/breakFast"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:button="@null"
                    android:text="BreakFast"/>

            <RadioButton
                    android:id="@+id/lunch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:button="@null"
                    android:text="Lunch"/>

            <RadioButton
                    android:id="@+id/dinner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:button="@null"
                    android:text="Dinner"/>

        </RadioGroup>
    </LinearLayout>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/SEARCH_BUTTON_ID"
            android:layout_marginTop="10dp"
            android:text="Search"/>
</LinearLayout>

**Activity**
 private RadioGroup rdg;
    private RadioButton breakFast;
    private RadioButton lunch;
    private RadioButton dinner;
    private String selectedType="";
    private Button Search;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rdg = (RadioGroup) findViewById(R.id.rdg);
        breakFast = (RadioButton) findViewById(R.id.breakFast);
        lunch = (RadioButton) findViewById(R.id.lunch);
        dinner = (RadioButton) findViewById(R.id.dinner);
        Search=(Button) findViewById(R.id.SEARCH_BUTTON_ID);
        breakFast.setSelected(true);
        rdg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==R.id.breakFast){
                    selectedType = breakFast.getText().toString();
                }else if(i==R.id.lunch){
                    selectedType = lunch.getText().toString();
                }else{
                    selectedType = dinner.getText().toString();
                }
            }
        });


        Search.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent searchIntent=new Intent(SearchPage.this,AndroidTabRestaurantDescSearchListView.class);
                searchIntent.putExtra("REST",selectedType);
                startActivity(searchIntent);
            }
        });
    }
于 2013-10-11T04:21:09.753 回答