android - 获取单选按钮值 Android

标签 android forms android-intent radio-button

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

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

如何检索用户单击了哪个单选按钮?我一共有五个。 下面是我的 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 变量传递给它..这个 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;
    }

}

非常感谢任何反馈!

最佳答案

每组单选按钮应该在一个组中,我假设您已经在 XML 文件中这样做了。

假设如此,那么您可以通过以下方式获得选中的单选按钮的 ID:

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

您可以在 Activity 的任何位置执行此操作,或者如果您使用的是 Fragment,那么您只需要放置一个 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;
}

关于android - 获取单选按钮值 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13148034/

相关文章:

android - Android 2.3.3 中的 Gradle 错误

forms - Bootstrap : Vertically center label in form-group

php - 根据表单中的行数将项目添加到购物车

android - 将联系人 .VCF 导入电话簿 android

android - 搜索 Intent 不起作用

java - 如何检查当前时间是否在android中的预设时间范围内

android - HttpContext 不持有 cookie

java - 在没有字段绑定(bind)的情况下使用表单错误?

Android:如何使用单个实例从非 Activity 类启动 Activity

java - 服务和类之间的通信