为什么我不能将文本设置为Android TextView?

时间:2010-07-18 12:19:05

标签: android text textview

我在将文本设置为TextView时遇到问题:

TextView android:editable = "true".

在我的.java中,似乎这应该有效:

 text = (EditText) findViewById(R.id.this_is_the_id_of_textview);
text.setText("TEST");

但事实并非如此。谁能告诉我这里有什么问题?

8 个答案:

答案 0 :(得分:35)

代码应该是这样的:

TextView text = (TextView) findViewById(R.id.this_is_the_id_of_textview);
text.setText("test");

答案 1 :(得分:17)

在您的java类中,将“EditText”类型设置为“TextView”。因为您已在TextView文件中声明了layout.xml

答案 2 :(得分:5)

要在任何活动中设置文本而不是使用下面的代码...     按照以下步骤逐一进行:

1.Declare

    private TextView event_post;

2.绑定

    event_post = (TextView) findViewById(R.id.text_post);

3.SetText in

    event_post.setText(event_post_count);

答案 3 :(得分:4)

尝试这样:

TextView text=(TextView)findViewById(R.id.textviewID);
text.setText("Text");

而不是:

text = (EditText) findViewById(R.id.this_is_the_id_of_textview);
text.setText("TEST");

答案 4 :(得分:1)

或者你可以这样做:

((TextView)findViewById(R.id.this_is_the_id_of_textview)).setText("Test");

答案 5 :(得分:1)

我遇到了类似的问题,但是当我尝试设置文本时,我的程序会崩溃。我试图在扩展AsyncTask的类中设置文本值,这就是导致问题的原因。

要解决它,我将setText移动到onPostExecute方法

protected void onPostExecute(Void result) {
    super.onPostExecute(result);

    TextView text = (TextView) findViewById(R.id.errorsToday);
    text.setText("new string value");
}

答案 6 :(得分:0)

在XML中,您使用过Textview,但在Java Code中,您使用的是EditText而不是TextView。如果将其更改为TextView,则可以将Text设置为TextView对象。

text = (TextView) findViewById(R.id.this_is_the_id_of_textview);
text.setText("TEST");

希望它能奏效。

答案 7 :(得分:0)

您将文本设置为在活动的另一个实例中以现金形式呈现的字段,例如,它是横向的。然后由于某种原因重新创建了它。您需要通过将字符串保存到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;
    }
}
相关问题