来自 EditText 字段的 Android getText [英] Android getText from EditText field

查看:22
本文介绍了来自 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆