Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 将单选按钮数据发送到下一个活动大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让用户输入他们的名字,然后点击三个单选按钮中的一个,然后点击提交按钮.在下一个活动中,它将显示他们的名字和他们选择的单选按钮.我已经设法能够发送名称,但我不知道如何发送单选按钮选择.有人可以帮忙吗?

这是我在主活动布局.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文件中有这个:

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);
    }

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

解决方法

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

编辑:要在下一个活动中收到数据,请使用

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

}

大佬总结

以上是大佬教程为你收集整理的android – 将单选按钮数据发送到下一个活动全部内容,希望文章能够帮你解决android – 将单选按钮数据发送到下一个活动所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。