Android 如何设置 TextView 的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10399210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set text of TextView ?
提问by Arshad Ali
I am facing a problem of setting the text to TextView
in android my code is :
我面临TextView
在 android中设置文本的问题,我的代码是:
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
final TextView text = (TextView) findViewById(R.id.textView1);
final EditText input = (EditText) findViewById(R.id.editText1);
final String string = input.getText();
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
text.setText(string);
}
});
}
}
if I write
如果我写
final Editable string = input.getText();
then it works.....!!!!
然后它起作用了......!!!!
Now I want to send data of EditText
to next Activity
like this :
现在我想像这样发送EditText
到 next 的数据Activity
:
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
final TextView text = (TextView) findViewById(R.id.textView1);
final EditText input = (EditText) findViewById(R.id.editText1);
final Editable string = input.getText();
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Main.this, Second.class);
intent.putExtra("thetext", string);
startActivity(intent);
}
});
}
}
and in Second.java
class I get StringExtra
in this way:
在Second.java
课堂上我StringExtra
是这样的:
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView text = (TextView) findViewById(R.id.textView2);
String string = getIntent().getExtras().getString("thetext", "not found");
text.setText(string); /// Here the text is not shown but the default message "not found" is set to `TextView`
}
}
Please give me way to proceed in development.
请给我继续开发的方法。
采纳答案by Brayden
I think the problem is you're actually putting an "Editable" in the intent, not a String. Although close, they're not the same thing. If you toString() your Editable to get a String object and put that in the intent, you should be able to get it back out with getString like you're doing.
我认为问题在于您实际上是在意图中放置了“可编辑”,而不是字符串。虽然很接近,但它们不是一回事。如果你 toString() 你的 Editable 来获取一个 String 对象并将其放入意图中,你应该能够像你正在做的那样使用 getString 将它取回。
回答by skywall
The problem should be, you're sending Editable
, not String
. Try this:
问题应该是,您发送的是Editable
,而不是String
。尝试这个:
final String string = input.getText().toString();
回答by Brayden
private TextView mTextView;
private String mString;
mTextView = (TextView) findViewById(R.id.tv);
mString = "hello everyone ! how r u?";
mTextView.setText(mString);
回答by Sam
Try something like this:
尝试这样的事情:
public class Main extends Activity {
EditText input;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.textView1);
input = (EditText) findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Main.this, Second.class);
intent.putExtra("thetext", input.getText().toString());
startActivity(intent);
}
});
}
}
(Hint: The easiest way to post code is to paste your code, select it, and use crtl+k to indent/format it.)
(提示:发布代码的最简单方法是粘贴您的代码,选择它,然后使用 crtl+k 对其进行缩进/格式化。)
回答by Drake Clarris
According to the android docs, the name of the string put into extras must include a package prefix... i.e. som.arshay.dev.thetext
Secondly, getExtras() returns a bundle, which is not what you added. You need
getStringExtra( name )
根据android docs,放入extras 的字符串的名称必须包含包前缀...即som.arshay.dev.thetext 其次,getExtras() 返回一个包,这不是您添加的包。你需要
getStringExtra( name )
回答by Aerrow
In this line use
在这一行中使用
final String string = input.getText().toString();
instead of
代替
final String string = input.getText();