1

Eclipse が提供するさまざまなコンポーネントの使用方法を学ぶために、単純なフォーム アクティビティを作成しようとしています。ラジオボタンにたどり着くまではうまくいっていました。

このアプリケーションにより、ユーザーはフォーム全体に入力してから送信ボタンをクリックできます。送信ボタンがクリックされると、新しいインテントを作成し、すべてのフォーム データをインテントに渡し、一種の確認/概要ページとして機能する新しいアクティビティを開始します。

ユーザーがクリックしたラジオボタンを取得するにはどうすればよいですか? 全部で5つ持っています。以下は私の onCreate メソッドのコードです。

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.feedback_scroll);

    //References to XML
    name = (EditText)findViewById(R.id.nameEntryBox);
    county = (Spinner)findViewById(R.id.countySpinner);

    //Set array adapter
    county.setAdapter(fillCountySpinner());
    submitBtn = (Button)findViewById(R.id.submitFormBtn);

    atmo1 = (RadioButton)findViewById(R.id.arad1);
    atmo2 = (RadioButton)findViewById(R.id.arad2);
    atmo3 = (RadioButton)findViewById(R.id.arad3);
    atmo4 = (RadioButton)findViewById(R.id.arad4);
    atmo5 = (RadioButton)findViewById(R.id.arad5);

    ser1 = (RadioButton)findViewById(R.id.srad1);
    ser2 = (RadioButton)findViewById(R.id.srad2);
    ser3 = (RadioButton)findViewById(R.id.srad3);
    ser4 = (RadioButton)findViewById(R.id.srad4);
    ser5 = (RadioButton)findViewById(R.id.srad5);

    rating = (RatingBar)findViewById(R.id.ratingBar1);

    //Add a listener to the submit button - Anonymous inner class
    submitBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Create a new Intent
            Intent i = new Intent(v.getContext(), FeedbackResults.class);

            choice = (String)county.getSelectedItem();
            //Add extra parameters

            //Name
            i.putExtra("name", name.getText());
            //County
            i.putExtra("county", choice);
            //Dob
            //Atmosphere
            i.putExtra("atmosphere", atmos);
            //Service
            i.putExtra("service", serv);
            //Rating
            i.putExtra("rating", rating.getRating());

            //Start new Activity that will display a review of the customers feedback
            startActivity(i);

            //Toast message
            Toast.makeText(v.getContext(), "Thank you for your feedback!", Toast.LENGTH_SHORT).show();

        }
    });
}

このサイトでの私の質問に関連する別のトピックを見ていました。彼らは、switch ステートメントを使用すると言いました。私はそれでロジックを理解し、これを行うメソッドを書きましたが、ビュー変数をそれに渡す方法がわかりません..このビュー変数は何に関連していますか?? これは私が書いた方法です。

    public void onRadioButtonClicked1(View v){

    switch(v.getId()){
    case R.id.arad1:
        atmos = "1";
        break;
    case R.id.arad2:
        atmos = "2";
        break;
    case R.id.arad3:
        atmos = "3";
         break;
    case R.id.arad4:
        atmos = "4";
        break;
    case R.id.arad5:
        atmos = "5";
        break;
    }

}

どんなフィードバックでも大歓迎です!

4

1 に答える 1

3

ラジオ ボタンの各グループは、グループ内にある必要があります。これは、XML ファイルで既に行っていると思います。

それを仮定すると、次のようにして、チェックされたラジオボタンの ID を取得します。

int id = ((RadioGroup)findViewById( R.id.radio_group )).getCheckedRadioButtonId();

のどこでもそれを行うことができActivityます。または、を使用している場合は、その中Fragmentに a を入れるだけですgetView()

int id = ((RadioGroup)getView().findViewById( R.id.radio_group )).getCheckedRadioButtonId();

だから私はあなたを変更しますonClick

public void onClick(View v) {
    // Create a new Intent
    Intent i = new Intent(v.getContext(), FeedbackResults.class);

    choice = (String)county.getSelectedItem();
    int id = ((RadioGroup)findViewById( R.id.radio_group )).getCheckedRadioButtonId();
    atmos = getAtmos( id );
    ...
}

private int getAtmos( int id ) {
    switch( id ) {
        case R.id.arad1:
            atmos = "1";
            break;
        case R.id.arad2:
            atmos = "2";
            break;
        case R.id.arad3:
            atmos = "3";
             break;
        case R.id.arad4:
            atmos = "4";
        break;
        case R.id.arad5:
            atmos = "5";
            break;
    }

    return atmos;
}
于 2012-10-30T21:50:31.297 に答える