第一
public class hanh1_2 extends Activity{
public static int ButID;
@Override
其次,设置按钮操作:
final Button ok2 = (Button) findViewById(R.id.ok2);
ok2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Set int of ButID = checkedradiobuttonID
//If ButID = -1 --> there isn't bt checked
int ButID = tieng.getCheckedRadioButtonId();
if (ButID == -1){
Toast.makeText(hanh1_2.this, "Check a butt pls", Toast.LENGTH_SHORT).show();
}
else {
Intent intent2 = new Intent(hanh1_2.this,hanh1_3.class);
startActivity(intent2);
}
}
});
没有意义的高级,但可能对像我这样的新手有用:)。
答案 0 :(得分:2)
查看Android开发网站上的Form stuff tutorial。您可以为所有RadioButton提供OnClickListener并跟踪所选的一个(如果有的话)。
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
或者,您可以使用RadioGroup的getCheckedRadioButtonId()
方法。
如其他答案之一所示:将int值作为额外内容传递给用于启动第二个Activity的Intent:
// In first activity
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putInt("selected_index", selectedIndex);
startActivity(i);
// In second activity
int selectedIndex = getIntent().getExtras().getInt("selected_index");
答案 1 :(得分:1)
将所有RadioButton
和RadioGroup
带到班级。
在onCreate()
现在在onClick()
内获取已检查RadioButton
的ID并进行比较,如下所示:
public void onClick(View v) {
int checked = tieng.getCheckedRadioButtonId(); // tieng is your RadioGroup
switch(checked)
{
case R.id.tieng1:
Toast.makeText(hanh1_2.this, "First is selected", Toast.LENGTH_SHORT).show();
break;
case R.id.tieng1:
Toast.makeText(hanh1_2.this, "Second is selected", Toast.LENGTH_SHORT).show();
break;
case R.id.tieng1:
Toast.makeText(hanh1_2.this, "Third is selected", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(hanh1_2.this, "pleas check any button", Toast.LENGTH_SHORT).show();
break;
}
}
答案 2 :(得分:0)
加上意图:
else {
Intent intent2 = new Intent(hanh1_2.this,hanh1_3.class);
intent2.putInt(Index1, index1);
startActivity(intent2);
}
现在在第二个活动onCreate()里面读了这个额外的东西:
{
int Index1 = getIntent().getExtras().getInt("Index1");
//do stuff here
}