-1

我想讓用戶填寫表單,然後這些字段會自動填充到電子郵件中。我能夠將EditText轉換爲字符串以及微調器,但我很困惑如何使用RadioGroup來完成。我想要選中的按鈕中的文本顯示在電子郵件中。我知道getText不起作用,但是會發生什麼。謝謝無法解析RadioGroup的方法getText

JAVA

//This happens when you press the submit button at the bottom of the form. 
    public void submit(View button) { 
    // Do click handling here 
    //What happens here is the input from each field is taken in and converted into 
    //Strings and placed into their correct variables. 

    final EditText FirstNameField = (EditText) findViewById(R.id.EditTextFirstName); 
    fName = FirstNameField.getText().toString(); 

    final EditText LastNameField = (EditText) findViewById(R.id.EditTextLastName); 
    lName = LastNameField.getText().toString(); 

    final EditText emailField = (EditText) findViewById(R.id.EditTextEmail); 
    email = emailField.getText().toString(); 

    final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody); 
    feedback = feedbackField.getText().toString(); 

    final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType); 
    feedbackType = feedbackSpinner.getSelectedItem().toString(); 

    final RadioGroup ApproveorReject = (RadioGroup) findViewById(R.id.radiochoice); 
    fAnswer = ApproveorReject.getText().toString(); 


    //calls the sendEmail() from below to pull up a list of apps installed 
    //on the phone that can handle emailing. 
    sendEmail(); 
} 


//This bit of code pulls up a list of apps that can handle emailing. 
private void sendEmail() { 

    //When the user chooses an app of the list that pops up, these lines below 
    //will auto-populate the fields of the email with the input from above. 
    //The user can take one last look at the email before hitting that send button 
    Intent i = new Intent(Intent.ACTION_SEND); 
    i.setType("message/rfc822"); 
    i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
    i.putExtra(Intent.EXTRA_SUBJECT, feedbackType); 
    i.putExtra(Intent.EXTRA_TEXT, lName + ", " + fName + "\n" + email + "\n" + feedback + fAnswer); 
    startActivity(Intent.createChooser(i, "Send mail...")); 


} 

}

XML

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:scrollbars="vertical"> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/TextViewTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:text="@string/feedbacktitle" 
     android:textSize="10pt"> 
    </TextView> 

    <!--First Name--> 
    <EditText 
     android:id="@+id/EditTextFirstName" 
     android:layout_height="wrap_content" 
     android:hint="@string/feedbackfirst" 
     android:inputType="textPersonName" 
     android:layout_width="fill_parent" 
     android:layout_below="@+id/TextViewTitle"> 
    </EditText> 

    <!--Last Name--> 
    <EditText 
     android:id="@+id/EditTextLastName" 
     android:layout_height="wrap_content" 
     android:hint="@string/feedbacklast" 
     android:inputType="textPersonName" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/EditTextFirstName"> 
    </EditText> 

    <!-- Email --> 
    <EditText 
     android:id="@+id/EditTextEmail" 
     android:layout_height="wrap_content" 
     android:hint="@string/feedbackemail" 
     android:inputType="textEmailAddress" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/EditTextLastName"> 
    </EditText> 

    <Spinner 
     android:id="@+id/SpinnerFeedbackType" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:entries="@array/Types_of_Errors" 
     android:layout_below="@+id/EditTextEmail"> 
    </Spinner> 

    <EditText 
     android:id="@+id/EditTextFeedbackBody" 
     android:layout_height="wrap_content" 
     android:hint="@string/feedbackbody" 
     android:inputType="textMultiLine" 
     android:lines="5" 
     android:layout_width="fill_parent" 
     android:layout_below="@+id/SpinnerFeedbackType"> 

    </EditText> 

    <RadioGroup 
     android:id="@+id/radiochoice" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/EditTextFeedbackBody" 
     android:orientation="horizontal"> 

     <RadioButton 
     android:id="@+id/AcceptRadio" 
     android:layout_width="wrap_content" 
     android:layout_marginStart="50dp" 
     android:layout_height="wrap_content" 
     android:text="@string/acceptbutton" /> 

     <RadioButton 
      android:id="@+id/RejectRadio" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/rejectbutton" 
      android:layout_marginStart="115dp" /> 
    </RadioGroup> 

    <EditText 
     android:id="@+id/RejectResponse" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="@string/rejectreason" 
     android:layout_marginTop="410dp" 
     android:layout_alignParentEnd="true" 
     android:layout_marginRight="20dp" 
     android:inputType="text"/> 

    <Button 
     android:id="@+id/ButtonSend" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/sendform" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="590dp" 
     android:onClick="submit"/> 

</RelativeLayout> 

+0

是指這樣的:http://stackoverflow.com/questions/6440259/how-to-get-the-selected-index-of-a-radiogroup-in-android – Calvin

回答

0

試試這個

RadioButton btn=(RadioButton)findViewById(ApproveorReject.getCheckedRadioButtonId()); 
String selectedText=btn.getText().toString(); 
+0

哪裏,我會添加此? –

+0

在您的提交方法 –

0

嘗試

ApproveorReject.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
{ 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 

     RadioButton rb=(RadioButton)findViewById(checkedId); 
     String param = rb.getText(); 
    } 
});