[英]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.