1

我正在尝试制作一个简单的表单活动,以了解如何使用 Eclipse 提供的各种组件。在我到达单选按钮之前,我做得很好。

该应用程序允许用户填写整个表格,然后单击发送按钮。单击发送按钮时,我创建一个新意图,将所有表单数据传递到该意图并启动一个新活动以充当一种确认/摘要页面。

如何检索用户单击了哪个单选按钮?我一共有五个。下面是我的 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 语句。我了解它的逻辑并在方法中编写了执行此操作的方法,但我不知道如何将 View 变量传递给它..这个视图变量与什么有关?这是我写的方法。

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

您可以在 a 中的任何位置执行此操作Activity,或者如果您正在使用 a Fragment,则只需将 agetView()放入其中:

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 回答