Android getText from EditText field(来自 EditText 字段的 Android getText)
问题描述
我在从 EditText
字段获取文本以将其插入到电子邮件编辑器中时遇到问题.我已经在布局文件 (@+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>
按钮调用 onClick 方法sestaviEmail",我已经声明了它:
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 展示它,因为它更快,但每次我尝试从字段中获取文本时,我都会得到假".我发现的所有其他问题的代码都在方法中而不是在布局中声明了 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()
这篇关于来自 EditText 字段的 Android getText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:来自 EditText 字段的 Android getText


- android 4中的android RadioButton问题 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01