从选定的单选按钮获取值并将其传递给新活动

时间:2013-10-11 04:07:12

标签: android android-intent

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

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

  • 早餐
  • 午餐
  • 晚餐

其中只有一个会被选中因为它们是单选按钮组

img

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

任何想法?


我知道如何通过从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);
                }
            });

我如何实现这一点,就像我在radiobutton集团中提到的那样!

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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);
            }
        });
    }