0

我正在建立一个用户反馈表。该表格由多个RadioGroups. 每个RadioGroup里面有 5 个RadioButtons

目前这是我检查每个组的值的方式。但是,这样做似乎很冗长且尴尬,有人可以提出一种更有效的方法来整理代码吗?

这是我的 XML 文件:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- Activity Title -->


        <TextView
            android:id="@+id/feedbackTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="@string/feedbackTitletxt"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <!-- Description box -->


        <TextView
            android:id="@+id/feedbackInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="@string/feedbackInfotxt" />

        <!-- Name -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/nameLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/nameLabeltxt" />


            <EditText
                android:id="@+id/nameEntryBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:ems="10"
                android:inputType="textPersonName" />

        </LinearLayout>

        <!-- County -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/countyLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/countyLabeltxt" />


            <Spinner
                android:id="@+id/countySpinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_weight="1" />

        </LinearLayout>

        <!-- Date of Birth -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >


            <TextView
                android:id="@+id/dobLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:text="@string/dobLabeltxt" />


            <DatePicker
                android:id="@+id/datePicker"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp" />

        </LinearLayout>

        <!-- Atmosphere -->


        <TextView
            android:id="@+id/atmosphereLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="@string/atmosphereLabeltxt" />


        <RadioGroup
            android:id="@+id/atmospheregroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/arad1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:text="@string/radVal1" />

            <RadioButton
                android:id="@+id/arad2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="@string/radVal2" />

            <RadioButton
                android:id="@+id/arad3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="@string/radVal3" />

            <RadioButton
                android:id="@+id/arad4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="@string/radVal4" />

            <RadioButton
                android:id="@+id/arad5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="15dp"
                android:layout_weight="1"
                android:text="@string/radVal5" />
        </RadioGroup>

        <!-- Service -->


        <TextView
            android:id="@+id/serviceLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="@string/serviceLabeltxt" />



            <RadioGroup
                android:id="@+id/sevicegroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal" >


                <RadioButton
                    android:id="@+id/srad1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="1"
                    android:text="@string/radVal1" />

                <RadioButton
                    android:id="@+id/srad2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="1"
                    android:text="@string/radVal2" />

                <RadioButton
                    android:id="@+id/srad3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="1"
                    android:text="@string/radVal3" />

                <RadioButton
                    android:id="@+id/srad4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="1"
                    android:text="@string/radVal4" />

                <RadioButton
                    android:id="@+id/srad5"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="1"
                    android:text="@string/radVal5" />
            </RadioGroup>

        <!-- Rating Bar -->


        <TextView
            android:id="@+id/overallLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="@string/overallLabeltxt" />


        <RatingBar
            android:id="@+id/ratingBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp" />

        <!-- Submit Button -->

        <Button
            android:id="@+id/submitFormBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/submitFormBtntxt" />
    </LinearLayout>

</ScrollView>

这是我的活动:

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

    // References to XML - Reference required to every component on the
    // form.
    name = (EditText) findViewById(R.id.nameEntryBox);
    county = (Spinner) findViewById(R.id.countySpinner);
    // Set array adapter
    county.setAdapter(fillCountySpinner());
    date = (DatePicker) findViewById(R.id.datePicker);
    atmosGroup = (RadioGroup) findViewById(R.id.atmospheregroup);
    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);
    servGroup = (RadioGroup) findViewById(R.id.sevicegroup);
    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);
    submitBtn = (Button) findViewById(R.id.submitFormBtn);

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

            // Add extra parameters using putExtra() method. extras are
            // essentially a Bundle of additional information we want to
            // pass to a new activity.
            // It is similar to a map in the sense that is uses key value
            // pairs.

            // Name - Get name from EditText box and convert to string.
            i.putExtra("name", name.getText().toString());
            // County - getSelectedItem() will return data of currently
            // selected item. Convert this to a String.
            i.putExtra("county", county.getSelectedItem().toString());

            // DOB - dd/mm/yyyy format.
            i.putExtra("day", date.getDayOfMonth());
            i.putExtra("month", date.getMonth());
            i.putExtra("year", date.getYear());

            // Atmosphere - assign the checked buttons id to id variable and
            // pass to method which will assign "atmos" a value based on
            // radio button selected.
            id = atmosGroup.getCheckedRadioButtonId();
            getAtmosphere(id);
            i.putExtra("atmosphere", atmos);

            // Service - assign the checked buttons id to id variable and
            // pass to method which will assign "serv" a value based on
            // radio button selected.
            id = servGroup.getCheckedRadioButtonId();
            getService(id);
            i.putExtra("service", serv);

            // Rating - As the rating is a Float and we cannot display a
            // Float in a TextView we must convert to a string.
            i.putExtra("rating", Float.toString(rating.getRating()));

            // Start new Activity that will display a review of the
            // customers feedback. Pass the intent to the method. This
            // intent contains all of the data in it's extras array.
            startActivity(i);
            finish();

            // Toast message - Create a Toast Object to inform user that
            // feedback was submitted.
            Toast.makeText(v.getContext(), "Thank you for your feedback!",
                    Toast.LENGTH_LONG).show();

        }
    });
}

在这个电话中,我为每个不同的组写了这个方法。

// Called when the user presses the submit button. The id of the radioButton
// that was checked is passed to the method.
public void getAtmosphere(int id) {

    // Switch statement will check if the ID matches any of the cases.
    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;
    }
}

以上工作正常,没有错误,但我只想知道如何改进它!非常感谢。

4

2 回答 2

2

然而,这样做似乎很冗长和尴尬

RadioGroups 只是啰嗦...您可以编写更少的代码:

TextView checked = (TextView) findViewById(amosgroup.getCheckedRadioButtonId());
atmos = checked.getText().toString();

getCheckedRadioButtonId()本身就很啰嗦!)但这假设每个 RadioButton 的文本是“1”、“2”等,并且findViewById()比 switch 语句慢......


或者您可以使用 anOnCheckedChangedListener这样您就不必调用getCheckedRadioButtonId(). 但是每次用户点击一个新的 RadioButton 时都会调用这个,如果你只需要“最终答案”,它会慢几毫秒。

于 2013-02-22T21:34:39.390 回答
1

好吧,由于您的 id 和您制作的结果字符串是合乎逻辑的("1"forarad1等),您可以使用此答案将 id 作为字符串获取。当然,如果您的按钮已经使用数字作为文本,那么使用它会更容易。

所以这样的事情可能会起作用:

String s =  getResources().getResourceEntryName(id);
atmos = s.substring (s.length()-1);
于 2013-02-22T21:37:01.970 回答