1

  1. GOAL 1:当点击按钮时,如果没有选中任何单选按钮,它将通过 Toast 警告用户;如果选中了一个单选按钮,它将把用户带到新的活动中(或者对你做 smt up)。

第一的

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

对高级毫无意义,但可能对像我这样的新手有帮助:)

4

3 回答 3

2
  1. 查看Android 开发网站上的Form stuff 教程。您可以为所有 RadioButtons 提供 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()方法。

  2. 如其他答案之一所示:将 int 值作为附加值传递给用于启动第二个活动的 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");
    
于 2011-12-26T05:30:05.840 回答
1

把你所有的RadioButtonRadioGroup课堂水平。在里面初始化它们onCreate()

现在在里面onClick()获取检查的IDRadioButton并进行比较,如下所示:

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

}
于 2011-12-26T05:28:56.800 回答
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

}

于 2011-12-26T05:22:27.847 回答