0

我正在構建用戶反饋表單。該表格由多個RadioGroups組成。 每個RadioGroup裏面有5 RadioButtons從Checked Radio按鈕檢索值Android

目前這是我檢查每個組的值的方式。然而,這樣做似乎很漫長而尷尬,有人可能會建議一個更有效的方法來更好地整理代碼嗎?

這裏是我的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; 
    } 
} 

以上工作正常,沒有錯誤,但我只想知道如何改善它! 非常感謝。

回答

2

然而,它似乎很長篇大論和尷尬去做這樣

RadioGroups只是長篇大論......你可以用更少的代碼:

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

getCheckedRadioButtonId()是)但是,這假定每個RadioButton的文本是「1」,「2」等,並且findViewById()比開關語句慢...


或者您可以使用OnCheckedChangedListener,因此您無需致電getCheckedRadioButtonId()。但是每當用戶點擊一個新的RadioButton,就會調用這個函數,如果你只需要「最終答案」,那麼它會慢幾毫秒。

+0

非常好,我現在將修改我的代碼!謝謝。 – Javacadabra 2013-02-22 21:36:57

1

既然你的ID和你所生成的字符串是合乎邏輯的("1"對於arad1等),你可以使用this answer來獲得作爲字符串的ID。當然,如果你的Buttons已經使用這個數字作爲文本,那麼使用它更容易。

所以這樣的事情可能工作:

String s = getResources().getResourceEntryName(id); 
atmos = s.substring (s.length()-1);