我正在制作用户反馈表单。表单由许多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;
}
}
以上工作正常,没有错误,但我只是想知道如何改进它! 非常感谢。
答案 0 :(得分:2)
然而,这样做似乎很长很啰嗦和尴尬
RadioGroups只是啰嗦......你可以用以下代码编写更少的代码:
TextView checked = (TextView) findViewById(amosgroup.getCheckedRadioButtonId());
atmos = checked.getText().toString();
(getCheckedRadioButtonId()
本身就是啰嗦!)但是这假设每个RadioButton的文本是“1”,“2”等,而findViewById()
比switch语句慢......
或者您可以使用OnCheckedChangedListener
,因此您无需拨打getCheckedRadioButtonId()
。但是每次用户点击一个新的RadioButton时都会调用它,如果你只需要“最终答案”,那就慢了几毫秒。
答案 1 :(得分:1)
因为你的id和你生成的字符串是合乎逻辑的("1"
等arad1
),你可以使用this answer将id作为字符串。当然,如果您的按钮已经使用数字作为文本,则更容易使用它。
这样的事情可能有用:
String s = getResources().getResourceEntryName(id);
atmos = s.substring (s.length()-1);