Android在runOnUiThread中更新对话框内的TextView

时间:2015-03-31 05:13:52

标签: android multithreading dialog textview android-runonuithread

我花了几个小时尝试更新对话框中的textview,但失败了。

单击该选项时,会显示新对话框,并在对话框内部有文本视图和按钮,当我单击按钮时,textview将会更新。

以下是与onClick侦听器按钮相关的代码:

 start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                for (int i = 0; i < 50 ; i ++){

                    final String currentNum = String.valueOf(i + 1);

                    Thread t = new Thread() {
                          @Override
                          public void run() {
                            runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
                                  try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                  System.out.println(currentNum); //it is fine
                                  currentNum.setText(currentNum); //it is the problem, the setText only work when the for loop is finished.
                              }
                            });
                          }
                        };

                        t.start();

                }
            }
        });

如果您需要更多信息,请与我们联系。非常感谢提前!

//这是一个选项菜单

    case R.id.action_refresh:
    final TextView currentNum;
    final ImageButton start;

    String currentNum = Integer.toString(songList.size());

    final Dialog lyricsAnalysis = new Dialog(this,R.style.cust_dialog);
    lyricsAnalysis.requestWindowFeature(Window.FEATURE_NO_TITLE);
    lyricsAnalysis.setContentView(R.layout.analysis);
    lyricsAnalysis.setCancelable(true); //back button to cancel
    lyricsAnalysis.setCanceledOnTouchOutside(true); 


    start = (ImageButton) lyricsAnalysis.findViewById(R.id.start);

    //first value
    currentNum.setText(String.valueOf(currentNum));

    start.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {
            for (int i = 0; i < 50 ; i ++){

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                updateTextView(lyricsAnalysis,i);

            }
        }
    });    
    lyricsAnalysis.show();
    lyricsAnalysis.getWindow().setLayout(600, 1000);
  break;
}
return super.onOptionsItemSelected(item); }
public void updateTextView(Dialog dialog, int i) {
    final TextView currentNum = (TextView) dialog.findViewById(R.id.currentNum);
    currentNum.setText(Stri`enter code here`ng.valueOf(i));
    //return;
}

2 个答案:

答案 0 :(得分:0)

试试这个方法。这可能对你有所帮助。这对我有用。(但我不是在对话中使用它)

public void updateTextView(String toThis) {

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText(toThis);

    //return;
}

答案 1 :(得分:0)

试试这个

int elapsedtime=0;
  boolean isTimerRunning=false;

Timer timerr;

在onCreate里面

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  //declare your textview here;
     timerr=new Timer();
                startTimer();  
}

   /*
     * timer for displaying message bubble
     */
    protected static void startTimer() {
        isTimerRunning = true;
        elapsedtime = 0;
        // recordingseek.setProgress(0);

        timerr.scheduleAtFixedRate(new TimerTask() {
            public void run() {

                 // increase every sec

elapsedtime++;


                    mmHandler.obtainMessage(1).sendToTarget();
                    System.out.println("recording time" + elapsedtime);

                if(elapsedtime==50)
                       timerr.cancel();


            }
        }, 1000, 2000);
    };

    public static Handler mmHandler = new Handler() {

        public void handleMessage(Message msg) {



            textview.setText(elapsedtime);



}
    };




    }
};