将单选按钮数据发送到下一个活动 [英] Sending radio button data to next activity

查看:78
本文介绍了将单选按钮数据发送到下一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图允许用户输入其名称,然后单击三个单选按钮之一,然后单击提交"按钮.在下一个活动中,它将显示其名称和所选的单选按钮.我设法能够发送名称,但是我不确定如何发送单选按钮选择.有人可以帮忙吗?

I'm trying to allow the user to enter their Name, and click on one of three radio buttons, and click on a submit button. And on the next activity, it will display their name and the radio button they selected. I've managed to be able to send the name, but I'm not sure how to send the radio button selection. Can someone help?

这是我在主要活动布局.xml中的内容

This is what I have in the main activity layout .xml

<EditText
    android:id="@+id/operatorName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:ems="10"
    android:hint="@string/operator_name" />

<RadioGroup
    android:id="@+id/radioShifts"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="10dp" >
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio1"
        android:checked="true"
        android:onClick="onRadioButtonClicked" />
    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio2"
        android:onClick="onRadioButtonClicked" />
    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio3"
        android:onClick="onRadioButtonClicked" />
</RadioGroup>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="28dp"
    android:text="@string/button1"
    android:onClick="onButton1" />

我在main_activity .java文件中有这个文件:

And I have this in the main_activity .java file:

    public final static String OP_NAME = "com.cyapps.downtimer.OPNAME";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();
        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.radioButton1:
                if (checked)
                    break;
            case R.id.radioButton2:
                if (checked)
                    break;
            case R.id.radioButton3:
                if (checked)
                    break;
        }
/*        Intent intent = new Intent(this, WinderDTActivity.class);
        EditText button = (EditText) findViewById(RadioGroup.getCheckedRadioButtonId());
        String radioChosen = button.getText().toString();
        intent.putExtra(RADIO_CHOSEN, radioChosen);*/
    }

    public void onButton1(View view) {
        Intent intent = new Intent(this, WinderDTActivity.class);
        EditText editText = (EditText) findViewById(R.id.operatorName);
        String opName = editText.getText().toString();
        intent.putExtra(OP_NAME, opName);
        startActivity(intent);
    }

/* */中的代码是我应该做的..但是我不确定.有人帮忙吗?我会很感激的.

The code in /* */ is what I think I should do.. But I'm not sure. Someone help please? I'll really appreciate it..

推荐答案

String str; // store the text corresponding to  the RadioButton which is clicked 

   switch(view.getId()) {
                case R.id.radioButton1:
                    if (checked)
                     str = "button1Text";
                        break;
                case R.id.radioButton2:
                    if (checked) str = "button2Text";
                        break;
                case R.id.radioButton3:
                    if (checked) str = "button3Text";
                        break;
         }

            Intent intent = new Intent(this, WinderDTActivity.class);
            intent.putExtra("radioChosen", str); // pass "str" to the next Activity

要在下一个活动中接收数据,请使用

EDIT : To recieve the data in the next activity, use

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String message= extras.getString("radioChosen");

}

这篇关于将单选按钮数据发送到下一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆