android – 将单选按钮数据发送到下一个活动

android – 将单选按钮数据发送到下一个活动,第1张

概述我试图让用户输入他们的名字,然后点击三个单选按钮中的一个,然后点击提交按钮.在下一个活动中,它将显示他们的名字和他们选择的单选按钮.我已经设法能够发送名称,但我不知道如何发送单选按钮选择.有人可以帮忙吗? 这是我在主活动布局.xml中的内容 <EditText android:id="@+id/operatorName" android:layout_width="wrap_con 我试图让用户输入他们的名字,然后点击三个单选按钮中的一个,然后点击提交按钮.在下一个活动中,它将显示他们的名字和他们选择的单选按钮.我已经设法能够发送名称,但我不知道如何发送单选按钮选择.有人可以帮忙吗?

这是我在主活动布局.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 – 将单选按钮数据发送到下一个活动所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1127226.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存