【问题标题】:Cannot resolve method getText for a RadioGroup无法解析 RadioGroup 的方法 getText
【发布时间】:2015-10-15 13:17:22
【问题描述】:

我正在尝试让用户填写表格,然后这些字段会自动填充到电子邮件中。我能够将 EditText 转换为字符串以及微调器,但我对如何使用 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[]{"recipient@example.com"});
    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>

【问题讨论】:

标签: java android android-layout android-edittext android-radiogroup


【解决方案1】:

试试这个

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

【讨论】:

  • 我会在哪里添加这个?
  • 在您的提交方法中
【解决方案2】:

试试

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 2015-06-10
    • 2019-08-28
    • 2019-10-10
    • 2019-06-13
    相关资源
    最近更新 更多