標題:

android 計時器開女生英文名

發問:

我想做一個計時器設定每隔幾分鐘就會去抓一次手機的時間可是現在不知道程式哪裡有問題無法執行?package com.example.taiwan;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class GetTime... 顯示更多 我想做一個計時器設定每隔幾分鐘就會去抓一次手機的時間 可是現在不知道程式哪裡有問題無法執行? package com.example.taiwan; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class GetTime extends Activity { TextView tv = (TextView) findViewById(R.id.textView1); Timer timer = new Timer(true); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_time); timer.schedule(new timerTask(), 1000, 100000); } public class timerTask extends TimerTask{ public void run(){ Calendar calendar = Calendar.getInstance(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/mm/dd HH:mm:ss"); String tm = simpleDateFormat.format(calendar.getTime()); tv.setText(tm); } } }

最佳解英文翻譯google答:

//不同Thread,傳遞資料要用Handler //加下列3行 import android.os.Bundle; import android.os.Handler; import android.os.Message; //以下修改 public class GetTime extends Activity { TextView tv; Timer timer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_time); tv = (TextView)findViewById(R.id.textview1); timer = new Timer(true); timer.schedule(new timerTask(new Handler() { public void handleMessage(Message m) { tv.setText(m.getData().getString("text")); } }), 1000, 100000); } public class timerTask extends TimerTask{ Handler mHandler; public timerTask(Handler handler){ mHandler = handler; } public void run(){ Calendar calendar = Calendar.getInstance(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/mm/dd HH:mm:ss"); String tm = simpleDateFormat.format(calendar.getTime()); Message msg = mHandler.obtainMessage(); Bundle b = new Bundle(); b.putString("text", tm); msg.setData(b); mHandler.sendMessage(msg); } } }

其他解轉學考補習找樂學網答:4D962705AE823D35
arrow
arrow

    martinewudi 發表在 痞客邦 留言(0) 人氣()