来自EditText字段的Android getText
我有一个从EditText
字段获取文本的问题,以意图将其插入到电子邮件composer php。 我已经在布局文件(@ + id / vnosEmaila)中声明了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>
button调用onClick方法“sestaviEmail”,我已经声明:
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来展示它,因为它更快,但是每次我尝试从字段中获取文本,我都会得到“false”。 我发现的所有其他问题都有代码在方法中声明Button而不是在布局中,也许这是问题的一部分?
如何从EditText
获取文本的示例代码。
EditText text = (EditText)findViewById(R.id.vnosEmaila); String value = text.getText().toString();
尝试这个。
EditText text = (EditText)findViewById(R.id.edittext1); String str = text.getText().toString().trim();
尝试这个 –
EditText myEditText = (EditText) findViewById(R.id.vnosEmaila); String text = myEditText.getText().toString();
试试这将解决你的问题….
EditText etxt = (EditText)findviewbyid(R.id.etxt); String str_value = etxt.getText().toString();
用这个:
setContentView(R.layout.yourlayout); //after setting yor layout do the following EditText email = (EdiText) findViewById(R.id.vnosEmaila); String val = email.getText().toString; // Use the toString method to convert the return value to a String. //Your Toast with String val; Toast toast = Toast.makeText(EmailGumb.this, val, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();
谢谢
EditText txt = (EditText)findviewbyid(R.id.txt); Editable str = txt.getText().toString(); Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG); toast.show();