我在从EditText
字段获取文本以将其插入电子邮件编辑器中时遇到问题。我在布局文件中声明了EditText
字段(@ + 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>
按钮调用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显示它,因为它更快但每次我尝试从字段中获取文本时我都会得到“假”。我发现的所有其他问题都有代码,这些代码在方法中而不是布局中声明了Button,这可能是问题的一部分吗?
答案 0 :(得分:81)
如何从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()
答案 1 :(得分:10)
试试这个。
EditText text = (EditText)findViewById(R.id.edittext1);
String str = text.getText().toString().trim();
答案 2 :(得分:4)
试试这个 -
EditText myEditText = (EditText) findViewById(R.id.vnosEmaila);
String text = myEditText.getText().toString();
答案 3 :(得分:1)
尝试这将解决您的问题....
EditText etxt = (EditText)findviewbyid(R.id.etxt);
String str_value = etxt.getText().toString();
答案 4 :(得分:0)
您可以通过应用以下代码简单地获取editText中的文本:
EditText editText=(EditText)findViewById(R.id.vnosZadeve);
String text=editText.getText().toString();
然后你可以烘烤字符串文字!
快乐的编码!
答案 5 :(得分:-1)
使用此:
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();
由于
答案 6 :(得分:-3)
EditText txt = (EditText)findviewbyid(R.id.txt);
Editable str = txt.getText().toString();
Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG);
toast.show();