-1

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

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

这是与按钮 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;
}
4

2 回答 2

0

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

public void updateTextView(String toThis) {

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

    //return;
}
于 2015-03-31T05:22:54.610 回答
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);



}
    };




    }
};
于 2015-03-31T05:28:35.247 回答