Tôi gặp sự cố khi nhận văn bản từ trường EditText
để chèn văn bản vào trình soạn thảo Email với mục đích. Tôi đã tuyên bố EditText
trường trong tệp bố trí (@ + id/vnosEmaila):Android getText từ trường EditText
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/navodiloEmail"
android:text="@string/navodiloEmail"
android:textSize="15dip"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/vnosEmaila"
android:layout_below="@id/navodiloEmail"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/navodiloZadeva"
android:text="@string/navodiloZadeva"
android:layout_below="@id/vnosEmaila"
android:textSize="15dip"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/vnosZadeve"
android:layout_below="@id/navodiloZadeva"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/navodiloBody"
android:text="@string/navodiloBody"
android:layout_below="@id/vnosZadeve"
android:textSize="15dip"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/vnosBody"
android:layout_below="@id/navodiloBody"/>
<Button
android:id="@+id/klicIntentEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sestaviEmail"
android:onClick="sestaviEmail"
android:layout_below="@id/vnosBody"/>
</RelativeLayout>
Nút gọi phương thức onClick "sestaviEmail" và tôi đã tuyên bố:
public void sestaviEmail (View view){
CharSequence test = getText(R.id.vnosEmaila);
Toast toast = Toast.makeText(EmailGumb.this, test, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
Tôi chỉ thấy nó với bánh mì nướng vì nó nhanh hơn nhưng mỗi khi tôi cố gắng để có được văn bản từ trường tôi nhận được "sai". Tất cả các câu hỏi khác mà tôi đã tìm thấy có mã mà tuyên bố Button trong phương pháp và không phải trong bố trí, có lẽ đây là một phần của vấn đề?
có thể trùng lặp của [Lấy giá trị của trường Chỉnh sửa văn bản] (http://stackoverflow.com/questions/4531396/get-value-of-a-edit-text-field) – Mafro34