問題描述
我在從 EditText
字段獲取文本以將其插入到電子郵件編輯器中時(shí)遇到問題.我已經(jīng)在布局文件 (@+id/vnosEmaila) 中聲明了 EditText
字段:
I have a problem with getting text from EditText
field to insert it in Email composer with intent. I've declared EditText
field in layout file (@+id/vnosEmaila):
<?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>
按鈕調(diào)用 onClick 方法sestaviEmail",我已經(jīng)聲明了它:
The button calls onClick method "sestaviEmail" and I have declared it:
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();
}
我只是用 Toast 展示它,因?yàn)樗欤看挝覈L試從字段中獲取文本時(shí),我都會(huì)得到假".我發(fā)現(xiàn)的所有其他問題的代碼都在方法中而不是在布局中聲明了 Button,也許這是問題的一部分?
I'm just showing it with Toast because it's faster but everytime I try to get text from field I get "false". All other questions that I've found had code which declared Button in methods and not in layout, maybe this is a part of the problem?
推薦答案
如何從 EditText
獲取文本的示例代碼.
Sample code for How to get text from EditText
.
Android Java 語法
EditText text = (EditText)findViewById(R.id.vnosEmaila);
String value = text.getText().toString();
Kotlin 語法
val text = findViewById<View>(R.id.vnosEmaila) as EditText
val value = text.text.toString()
這篇關(guān)于來自 EditText 字段的 Android getText的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!