Android:TextView 未更新

Android: TextView not updating

我有一个 EditText 和一个 TextView,我想在每次迭代中更新我的 TextView(ConsoleWindow 在循环中运行;它从处理程序调用,因此在 UIthread 上运行)。

问题是我的 TextView 只在第一轮更新,然后它在剩余的运行时保留第一个条目(尽管 dataString 在每一轮中都是不同的):

private void ConsoleWindow(String dataString) {

            LinearLayout layout = new LinearLayout(getApplicationContext());

            if (first2) { //first2 is true when application is launched
                // ONLY SET LAYOUT AND EDITTEXT IN FIRST RUN TO SAVE CAPACITY

                // LINEAR LAYOUT
                setContentView(layout);
                layout.setOrientation(LinearLayout.VERTICAL);
                layout.setBackgroundColor(Color.parseColor("#000000")); // black

                // EDITTEXT
                EditText et = new EditText(getApplicationContext());
                et.setHint("Enter Command");
                layout.addView(et);
                first2 = false;
            }


            // TEXTVIEW
            TextView tv = new TextView(getApplicationContext());
            tv.setText(dataString); // KEEPS THE SAME UNTIL THE 1ST ROUND
            layout.addView(tv);
}

我已经尝试了 tv.invalidate() 和 tv.postInvalidate(),但是没有效果。有人可以帮我吗?

first2false 时,您只是创建一个新的 LinearLayout layout,然后不膨胀 layout ,你是直接将 TextView tv 添加到 layout。这就是 Textview 不可见的原因。

private void ConsoleWindow(String dataString) {
    LinearLayout layout;
    TextView tv;
    EditText et;

    if (first2) {
        layout = new LinearLayout(getApplicationContext());

        setContentView(layout);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundColor(Color.parseColor("#000000"));

        // EDITTEXT
        et = new EditText(getApplicationContext());
        et.setHint("Enter Command");
        layout.addView(et);

        tv = new TextView(getApplicationContext());
        layout.addView(tv);

        first2 = false;
    }
    if(tv != null) {
        tv.setText(dataString);
    }
}

将 tv 设为全局变量。

 private TextView tv; 

在此之后,在您的 "onCreate()" 方法中:

 tv = new TextView(getApplicationContext());

然后:

private void ConsoleWindow(String dataString) {

            LinearLayout layout = new LinearLayout(getApplicationContext());

            if (first2) { //first2 is true when application is launched
                // ONLY SET LAYOUT AND EDITTEXT IN FIRST RUN TO SAVE CAPACITY

                // LINEAR LAYOUT
                setContentView(layout);
                layout.setOrientation(LinearLayout.VERTICAL);
                layout.setBackgroundColor(Color.parseColor("#000000")); // black

                // EDITTEXT
                EditText et = new EditText(getApplicationContext());
                et.setHint("Enter Command");
                layout.addView(et);
                first2 = false;
            }


            // TEXTVIEW
            tv.setText(dataString); // KEEPS THE SAME UNTIL THE 1ST ROUND
            layout.addView(tv);
}

也请验证,如果 dataString 有一些文本,像这样

Log.d(TAG , "dataString: " + dataString + "with first time? " + first2.toString());

尝试传递给setContentView(layout);在 if statement.Because 之外我不太明白你为什么需要这个。

  LinearLayout layout = new LinearLayout(getApplicationContext());
  setContentView(layout);