【问题标题】:Android getting and sending RadioButton id to other activitiesAndroid获取RadioButton id并将其发送到其他活动
【发布时间】:2014-01-05 17:39:07
【问题描述】:

我正在创建一个简单的应用程序,其中有很多单选按钮,但我没有在其中使用文本,只添加背景图片。所以在我完成设计工作后,我需要做所有的机械工作。我的问题是我可以找到如何发送单选按钮文本的信息,但没有关于可能的图像发送的信息。我发现可以发送 ID,但我看到人们使用它作为 int 变量。我的单选按钮 ID 类似于 jobRB1、jobRB2、jobRB3、....,有些只是诸如 goodCar、badCar 之类的词。最令人困惑的部分。所以我的问题是如何将单选按钮 ID 读取为字符串,然后在将其发送到其他活动后再次设置为背景。

我只做研究,所以我现在除了要显示的 xml 文件之外没有任何代码。

一些代码(我希望它会有所帮助):

private RadioGroup firstRadioGroup;
private RadioGroup secondRadioGroup;
private RadioGroup thirdRadioGroup;
private RadioButton rb1,rb2,rb3,rb4,rb5;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_jobs_page);
    firstRadioGroup = (RadioGroup) findViewById(R.id.jobrbgroup1);
    secondRadioGroup = (RadioGroup) findViewById(R.id.jobrbgroup2);
    thirdRadioGroup = (RadioGroup) findViewById(R.id.jobrbgroup3);
    rb1 = (RadioButton) findViewById(R.id.jobRB1); rb2 = (RadioButton) findViewById(R.id.jobRB2);
    rb3 = (RadioButton) findViewById(R.id.jobRB3); rb4 = (RadioButton) findViewById(R.id.jobRB4);
    rb5 = (RadioButton) findViewById(R.id.jobRB5);

public void resetRadioButtons (View view){
    firstRadioGroup.clearCheck();
    secondRadioGroup.clearCheck();
    thirdRadioGroup.clearCheck();
}

public void openReligionsPage (View view){


    if(rb1.isChecked()){
        Intent intent = new Intent (this, DisplayReligionsPage.class);
        startActivity(intent);
    }
    else if(rb2.isChecked()){
        Intent intent = new Intent (this, DisplayReligionsPage.class);
        startActivity(intent);
    }
    else if(rb3.isChecked()){
        Intent intent = new Intent (this, DisplayReligionsPage.class);
        startActivity(intent);
    }
    else if(rb4.isChecked()){
        Intent intent = new Intent (this, DisplayReligionsPage.class);
        startActivity(intent);
    }
    else if(rb5.isChecked()){
        Intent intent = new Intent (this, DisplayReligionsPage.class);
        startActivity(intent);
    }

【问题讨论】:

  • 贴一些代码,让我们看看你到目前为止做了什么。

标签: android android-activity radio-button


【解决方案1】:

您可以将按钮设为公开和静态,然后从您的其他活动中直接访问它们。

public static RadioButton rb1,rb2,rb3,rb4,rb5;

第二个活动中的示例

MainActivity.rb1.setBackground("");

【讨论】:

  • 你能解释一下吗?我想在新 ReligionActivity 的新按钮上设置在 JobActivity 的 rb1 上设置的图像。我怎样才能做到这一点?以及如何根据 RadioButton 的选择进行设置?
  • 您在 JobActivity 或 ReligionActivity 上方发布的 Activity 是哪个?那么你要做的是从 JobActivity 中取出 rb1 并在 ReligionActivity 中复制它?还是假设 rb1 在 JobActivity 中,是否要从 ReligionActivity 更改 rb1 的背景?
  • 在你的 ReligionActivityreligionrb1.setBackground(JobActivity.rb1.getBackground()) 中做这样的事情;
  • 在工作活动中,我说选择 rb1,然后单击按钮后,我想用一个背景类似于 rb1 的按钮来活动 Religion
  • 尝试了您的建议,还添加了@TargetApi(Build.VERSION_CODES.JELLY_BEAN),但它不起作用。我在 4.1.2 Android 上对其进行了测试,但它崩溃了。
猜你喜欢
  • 2018-08-26
  • 2016-04-08
  • 2020-11-20
  • 2017-08-16
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 2020-08-18
相关资源
最近更新 更多