0

사용자 피드백 양식을 작성 중입니다. 양식은 RadioGroups 숫자로 구성됩니다. 각 RadioGroup 안에 5 RadioButtons이 있습니다.Checked Radio 버튼에서 값 가져 오기 Android

현재 각 그룹의 값을 확인하는 방법입니다. 그러나 이런 식으로하는 것은 매우 오랜 시간이 걸리고 어색한 것처럼 보입니다. 누군가 코드를 더 정리할 수있는 더 효율적인 방법을 제안 할 수 있습니까? 나는 각각 다른 그룹에 대해 기입이 방법이이 호출에

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

     } 
    }); 
} 

:

<?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> 

여기 내 활동입니다 :

여기 내 XML 파일입니다.

// 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 그냥 긴 호흡하는이 방법을 수행 할 어색한 것 같다 : getCheckedRadioButtonId()은 (

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

그러나 이것은 각 RadioButton의 텍스트가 "1", "2"등으로 가정하고 findViewById()은 switch 문보다 느립니다.


OnCheckedChangedListener을 사용하면 getCheckedRadioButtonId()으로 전화하지 않아도됩니다. 그러나 사용자가 새 RadioButton을 클릭 할 때마다 호출됩니다. "최종 답변"만 필요하면 몇 밀리 초가 더 걸립니다.

+0

우수, 지금 코드를 수정합니다! 고맙습니다. – Javacadabra

1

귀하의 ID와 결과 문자열이 논리적이므로 (arad1 등의 경우 "1") this answer을 사용하여 id를 문자열로 가져올 수 있습니다. 물론, 버튼이 이미 텍스트로 숫자를 사용하고 있다면, 대신 그 숫자를 사용하는 것이 더 쉽습니다.

그래서 이런 식으로 뭔가를 작동 할 수 있습니다 :

String s = getResources().getResourceEntryName(id); 
atmos = s.substring (s.length()-1); 
관련 문제