-1

我想读取单选按钮上的 ID 并使用我将在另一个活动中开始的 Intent,我的目标是读取单选按钮上的 ID 并在下一个事件 XML 中显示它

xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Male" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Amphibian" />
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Submit" />

</RelativeLayout>

主要活动:

package com.work.radbtn;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {
Button btn;
RadioGroup radGrp;
RadioButton radBtn1, radBtn2, radBtn3;
String lang;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button1);
        radGrp = (RadioGroup) findViewById(R.id.radioGroup1);
        radBtn1 = (RadioButton) findViewById(R.id.radio0);
        radBtn2 = (RadioButton) findViewById(R.id.radio1);
        radBtn3 = (RadioButton) findViewById(R.id.radio2);
        /*radBtn1.setOnClickListener(radBtnOnClickListener);
        radBtn2.setOnClickListener(radBtnOnClickListener);
        radBtn3.setOnClickListener(radBtnOnClickListener);*/


        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (radBtn1.isSelected())
                    lang = lang + radBtn1.getId();
                else if (radBtn2.isSelected())
                    lang = lang + radBtn2.getId();
                else if (radBtn3.isSelected())
                    lang = lang + radBtn3.getId();

                else {

                Intent i = new Intent();
                i.setClass(MainActivity.this, MainActivity1.class);
                String temp = lang.toString();
                i.putExtra("me", temp);

                startActivity(i);
                }
            }
        });
    }



}

第二个 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity1" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="184dp"
    android:text="TextView" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="36dp"
    android:text="Button" />

主要活动1

    package com.work.radbtn;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity1 extends Activity {

TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity1);
        txt = (TextView) findViewById(R.id.textView1);

        Intent i = getIntent();
        Bundle bundle = i.getExtras();
        String temp = bundle.getString("me");
        txt.setText(temp);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity1, menu);
        return true;
    }

}
4

1 回答 1

0

创建单选按钮数组时,将每个按钮的 ID 存储在数组中,如下所示(在forwith 循环变量中i):

for (int i=0; i<NumberOfButtons; i++)
    // Create button i
    radioButtonId[i] = radioButton[i].getId();

然后您可以像这样获取所选按钮的数组索引:

private int getSelected() {
    int selectedId = radioGroup.getCheckedRadioButtonId();
    int selected = -1;
    for (int i=0; i<radioButtonId.length; i++) {
        if ( selectedId == radioButtonId[i] ) {
            selected = i;
            break;
        }
    }
    return selected;
}
于 2013-09-06T11:27:05.423 回答