ボタンをクリックすると、ダイアログボックスが開きます。そのダイアログ ボックスで、テキストを動的に (ストップウォッチのように) ループ経由でテキストに設定したいと考えています。誰かがサンプルコードで私を案内してもらえますか? ネットで提供されている多くの例を試しましたが、結果をうまく達成できませんでした。
//Button where the action starts
public void onClickStart(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_details);
dialog.setTitle("Your Step Details");
dialog.show();
DisplayTask dd= new DisplayTask();
dd.execute();
}
public void doWork(){
final Handler handler=new Handler();
new Thread(new Runnable (){
boolean isRunning=true;
@Override
public void run() {
while(isRunning){
try{
handler.post(new Runnable(){
@Override
public void run() {
try{
TextView txtCurrentTime= (TextView)findViewById(R.id.txtLeft);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":" + minutes + ":" + seconds;
txtCurrentTime.setText(curTime);
}catch (Exception e) {}
}
});
}catch(Exception e){
}
}
}
}).start();
}
public class DisplayTask extends AsyncTask<Void , Void, Void> {
protected void onPostExecute(){
MainActivity main= new Mai`enter code here`nActivity();
main.doWork();
}
@Override
protected Void doInBackground(Void... params) {
onPostExecute();
return null;
}
}