为什么我不能将文本设置为 Android TextView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3275467/
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
Why can't I set text to an Android TextView?
提问by Christoph
I'm having a problem with setting text to a TextView:
我在将文本设置为 TextView 时遇到问题:
TextView android:editable = "true".
In my .java it seems like that this should work:
在我的 .java 中,这似乎应该有效:
text = (EditText) findViewById(R.id.this_is_the_id_of_textview);
text.setText("TEST");
But it doesn't. Can anyone tell me what's wrong here?
但事实并非如此。谁能告诉我这里有什么问题?
采纳答案by Noman
In your java class, set the "EditText
" Type to "TextView
". Because you have declared a TextView
in the layout.xml
file
在您的 Java 类中,将“ EditText
”类型设置为“ TextView
”。因为你已经TextView
在layout.xml
文件中声明了 a
回答by Arnab C.
The code should instead be something like this:
代码应该是这样的:
TextView text = (TextView) findViewById(R.id.this_is_the_id_of_textview);
text.setText("test");
回答by patel135
To settext in any of in activity than use this below code... Follow these step one by one:
要在任何活动中设置文本,而不是使用下面的代码……请一步一步地执行以下操作:
1.Declare
1.申报
private TextView event_post;
2.Bind this
2.绑定这个
event_post = (TextView) findViewById(R.id.text_post);
3.SetText in
3.SetText中
event_post.setText(event_post_count);
回答by Bunny
Try Like this :
试试这样:
TextView text=(TextView)findViewById(R.id.textviewID);
text.setText("Text");
Instead of this:
取而代之的是:
text = (EditText) findViewById(R.id.this_is_the_id_of_textview);
text.setText("TEST");
回答by Hamedz
Or you can do this way :
或者你可以这样做:
((TextView)findViewById(R.id.this_is_the_id_of_textview)).setText("Test");
回答by Scott Bentley
I was having a similar problem, however my program would crash when I tried to set the text. I was attempting to set the text value from within a class that extends AsyncTask, and that was what was causing the problem.
我遇到了类似的问题,但是当我尝试设置文本时,我的程序会崩溃。我试图从扩展 AsyncTask 的类中设置文本值,这就是导致问题的原因。
To resolve it, I moved my setText to the onPostExecute method
为了解决它,我将我的 setText 移动到 onPostExecute 方法
protected void onPostExecute(Void result) {
super.onPostExecute(result);
TextView text = (TextView) findViewById(R.id.errorsToday);
text.setText("new string value");
}
回答by CodeToLife
You set the text to a field that was rendered in another instance of your activity residing in cash,say when it was landscape oriented. Then it was recreated due to some reason. You need to set the text in an unusual way by saving the string to SharedPreferences and force relaunch the activity using recreate(), say for mOutputText:
您将文本设置为在您的活动的另一个实例中呈现的字段,该实例以现金形式存在,例如当它面向横向时。后来由于某种原因重新创建了它。您需要通过将字符串保存到 SharedPreferences 并使用 recreate() 强制重新启动活动来以不寻常的方式设置文本,例如 mOutputText:
@Override
protected void onResume() {
super.onResume();
String sLcl=mPreferences.getString("response","");
if(!sLcl.isEmpty()){
mOutputText.setText(sLcl);
mPreferences.edit().putString("response","").commit();
}
}
private void changeText() {
mPreferences.edit().putString("response",responseText).commit();
recreate();
}
@Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAPTURE_MEDIA_RESULT_CODE:
if (null == data || null == data.getData()) {
showMessage("Sorry. You didn't save any video");
} else {
videoUri = data.getData();
mProgress = new ProgressDialog(this);
mProgress.setMessage("Uploading onYoutube ...");
authorizeIt();// SAY YOU CALL THE changeText() at the end of this method
}
break;
}
}
回答by Rajaji subramanian
In your XML, you had used Textview, But in Java Code you had used EditText instead of TextView. If you change it into TextView you can set Text to to your TextView Object.
在您的 XML 中,您使用了 Textview,但在 Java 代码中,您使用了 EditText 而不是 TextView。如果您将其更改为 TextView,则可以将 Text 设置为您的 TextView 对象。
text = (TextView) findViewById(R.id.this_is_the_id_of_textview);
text.setText("TEST");
hope it will work.
希望它会起作用。