获取单选按钮值的Andr​​oid [英] Get Radio Button Value Android

查看:196
本文介绍了获取单选按钮值的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单形式的活动,以学习如何使用Eclipse所提供的各种组件。我在做伟大的,直到我到了单选按钮。

I am trying to make a simple form activity in order to learn how to use the various components supplied by Eclipse. I was doing great until I got to the radio buttons.

该应用程序允许用户填写整个表单,然后点击发送按钮。当发送按钮被点击我创建了一个新的意图,通过所有形式的数据进入的意图,并开始了一项新的活动,作为一种确认/摘要页。

The application allows a user to fill out an entire form and then click the send button. When the send button is clicked I create a new intent, pass all of the form data into the intent and start up a new activity to act as a sort of confirmation/summary page.

我如何去检索该单选按钮用户点击?我有五个总。
下面是从我的onCreate方法的code。

How do I go about retrieving which radiobutton the user has clicked? I have five in total. Below is the code from my onCreate method.

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语句。我理解它的逻辑和方法来做到这一点写的,但我不知道如何通过查看变量it..What是这种观点的变量有关?这是我写的方法。

I was looking at another topic relating to my question on this site. They said to use a switch statement. I understand the logic with it and wrote in the methods to do this, but I don't know how to pass the View variable to it..What is this view variable relating to?? THis is the method I wrote.

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

}

任何反馈是非常AP preciated!

Any feedback is very much appreciated!

推荐答案

每个单选按钮组应在一组,我想你在XML文件中这样做了。

Each group of radio buttons should be in a group, I assume you did this already in your XML file.

假设,那么你做得到检查单选按钮的ID:

Assuming that, then you get the ID of the checked radio button by doing:

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

您可以做,在一个活动的任何位置,或者如果您使用的是片段,那么你只需要把 getView()在里面:

You can do that anywhere in an Activity, or if you're using a Fragment, then you just need to put a getView() in it:

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

这篇关于获取单选按钮值的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆