简体   繁体   English

如何从EditText视图中提取文本-Android

[英]How to extract text from an EditText view - Android

So I guess It was a simple as using a getText() to retrieve the information :) 所以我想这很简单,因为使用getText()来检索信息:)

Here is how it ended: 它是如何结束的:

public void getToast(View v) {
    EditText et = (EditText) findViewById(R.id.userText);
    String toastText = et.getText().toString();
    if (toastText == "" || toastText == null) {
        Toast.makeText(this, "This is a nice toast!", Toast.LENGTH_SHORT).show();   
    }
    else {
    Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
    }
}

I have created an EditText View on the main layout file. 我已经在主布局文件上创建了一个EditText视图。 This is referenced to with the userText identifier. 这是通过userText标识符引用的。 As it is an EditText field, the user is able to modify the text within it at any moment; 由于它是一个EditText字段,因此用户可以随时修改其中的文本。 what I am trying to accomplish is to retrieve the text the user has entered at the moment they tap on the button identified as getToast , and then display it as a Toast. 我要完成的工作是,在用户点击标识为getToast的按钮时立即检索用户输入的文本,然后将其显示为Toast。

I am using the Resources class at the moment (my first guess?) to retrieve the String stored under toastText but this is useless, as it is extracting the text stored in the main.xml - which is empty, as I declared no "android:text" attribute for that view, instead I used an "android:hint" to tell the user to enter the text. 我目前正在使用Resources类(我的第一个猜测?)来检索存储在toastText下的String,但这是无用的,因为它正在提取存储在main.xml中的文本-这是空的,因为我声明没有“ android :text“属性用于该视图,相反,我使用了” android:hint“来告诉用户输入文本。

I had read about intents, but that kind of makes sense if I was to send the String to another activity, not within the same one. 我已经阅读了有关意图的内容,但是如果我要将String发送到另一活动而不是在同一活动中,那是有意义的。 I had thought of it as a simple task, but it is consuming more time that I had hope :P 我原本以为这是一个简单的任务,但它消耗了我希望更多的时间:P

BTW: BTW:

The getToast method is defined as an "android:OnClickMethod" for a button created on the XML. 对于在XML上创建的按钮, getToast方法定义为“ android:OnClickMethod”。 It will work with any other string of text. 它将与其他任何文本字符串一起使用。

Any ideas? 有任何想法吗?

package com.testlabs.one;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class initialui extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
}

public void getToast(View v) {
    Resources myResources = getResources();
    String toastText = myResources.getString(R.string.toast); 
    if (toastText == "" || toastText == null) {
        Toast.makeText(this, "This is a nice toast!", Toast.LENGTH_SHORT).show();   
    }
    else {
    Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
    }
}
}

You simply call the function getText() on the TextView. 您只需在TextView上调用函数getText()

Example: 例:

public void getToast( View v )
{
    String toastText = ( (EditText)v ).getText();
    if ( toastText.length() == 0 ) {
        toastText = getResources().getString( R.string.toast );
    }
    Toast.makeText( this, toastText, Toast.LENGTH_SHORT ).show();
}

This code will display a toast with the text in your EditText when it is available and the default text in the toast resource when nothing was entered. 如果可用,此代码将显示带有您的EditText中文本的toast和未输入任何内容时toast资源中的默认文本。

If you just want to send a string from one activity to the other via an Intent 如果您只想通过Intent将字符串从一个活动发送到另一个活动

Intent intent = new Intent();
intent.putExtra("key","stringValue");
startActivity(intent);

then in your other activity 然后在你的其他活动中

Intent intent = getIntent();
String value = intent.getStringExtra("key","defaultstring");

I think this: 我认为这:

EditText et = (EditText) findViewById(R.id.userText);
String toastText = et.getText().toString();

Should be this: 应该是这样的:

String toastText  = findViewById(R.id.userText).toString();

If not, I would like to know why you needed to use the intermediate phase and not cast it straight to a java String object 如果不是,我想知道为什么您需要使用中间阶段而不将其直接转换为java String对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在循环中从 EditText 中提取文本并将其连接起来以在单个 TextView 中查看? - How to extract text from EditText in a loop and concatenate it to view it in a single TextView? 如何从 Android 中的 EditText 中提取 HTML 样式的文本? - How to extract HTML styled text from an EditText in Android? 如何通过在Android中单击按钮将文本从EditText复制到另一个EditText? - How To Copy a Text from a EditText to another EditText by button clicking in Android? 如何从 android 中的 edittext 中获取选定的文本? - How to get selected text from edittext in android? Android-如何定义要在EditText或Text View中显示的可绘制图像集 - Android - How to define sets of drawable images to be shown in edittext or text view 如何从动态创建的视图中的EditText获取文本 - How to get text from EditText from within a dynamically created view EditText-如何在Android EditText中设置默认文本 - EditText - How to set default text in android edittext 从EditText提取文本并在TextView中打印 - Extract Text From EditText and Print in TextView 如何在Android中将EditText中的文本另存为表格文本文件? - How to save text from an EditText as a table text file in Android? Android 从edittext复制文本 - Android copy text from edittext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM