1

  1. 目標1:ボタンをクリックしたときに、ラジオボタンがチェックされていない場合、トーストによってユーザーに警告します。ラジオボタンがチェックされている場合、ユーザーは新しいアクティビティに移動します(またはあなたに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 dev サイトのForm stuff チュートリアルをご覧ください。OnClickListener をすべての RadioButton に提供し、選択されたもの (存在する場合) を追跡できます。

    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. 他の回答の1つに示されているように、2番目のアクティビティを起動するために使用するインテントに追加としてint値を渡します。

    // 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()で、checked の id を取得し、次のRadioButtonように比較します。

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

2 番目のアクティビティ onCreate() 内で、次のエクストラを読み取ります。

{
int Index1 = getIntent().getExtras().getInt("Index1");

//do stuff here

}

于 2011-12-26T05:22:27.847 に答える