使用RadioButton将一项活动更改为另一项活动 [英] Changing one activity to another using RadioButton
本文介绍了使用RadioButton将一项活动更改为另一项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个活动更改为另一活动.它是第一次工作,但是下一次却不行,我该如何解决?
I want to change one activity to another activity. It worked at the first time, But not at the next, How do I fix that?
public class Activity1 extends Activity {
RadioGroup radioGroup;
RadioButton Rd1, Rd2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity1);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
Rd1=(RadioButton)findViewById(R.id.radioButton);
Rd2=(RadioButton)findViewById(R.id.radioButton2);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(Rd1.isChecked())
{
Intent intent = new Intent(getApplicationContext(), Acitivity.class);
startActivity(intent);
}
else {
if (Rd2.isChecked()) {
Intent intent1 = new Intent(getApplicationContext(), Acivity1.class);
startActivity(intent1);
}
}
}
});
}
}
推荐答案
我注意到您想从Activity1转到Activity.class,但是您不能这样做.将Activity.class更改为现有活动,例如:Activity2.class.而且您需要简化您的侦听器:类似这样的内容:
I noticed that you wanted to go from Activity1 to Activity.class, you cannot do that. Change Activity.class to existing activity, ex: Activity2.class. And you need to simplify your listener: to something like this:
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Intent intent = null;
if (Rd1.isChecked()) {
intent = new Intent(Activity1.this, Activity2.class);
} else if(Rd2.isChecked()) {
intent = new Intent(Activity1.this, Activity3.class);
}
startActivity(intent);
}
这篇关于使用RadioButton将一项活动更改为另一项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文