0
public class Stopper implements Runnable {
private long mStartTime;
private volatile int since;
private Handler handler;
private TextView tv;
private Activity activity;
private Context context;
/*
* Constants
* */
public static final int MILISEC_TO_MINUTE = 60000;
public static final int MILISEC_TO_HOUR = 3600000;
/*
* Flags
* */
private boolean mIsRunning;
public Stopper(){
//this.context = context;
activity = (Activity)context;
Message message = Message.obtain();
handler = new Handler(Looper.getMainLooper());
}
public void start(){
mStartTime = System.currentTimeMillis();
mIsRunning = true;
}
public void stop(){
mIsRunning = false;
}
@Override
public void run() {
while (mIsRunning) {
since = (int)(System.currentTimeMillis() - mStartTime);
int hours = (since/MILISEC_TO_HOUR) % 24; // ms -> h; 360 000= 1[h]
int minutes = (since/MILISEC_TO_MINUTE) % 60;// ms -> min; 60 000[ms] = 1[min]
int seconds = (since/1000) % 60; // ms -> s ;1 000[ms] = 1[s] -> (1000 [ms] /1000)%60 = 1[s]
int ms = since % 1000;
tv.setText(String.format("%02d:%02d:%02d:%03d", hours, minutes, seconds, ms));
//((Activity) context).updateTimerText(String.format("%02d:%02d:%02d:%03d", hours, minutes, seconds, ms));
}
}
public void getStooperTextView(TextView textView)
{
this.tv = textView;
}
public int getSince() {
return since;
}
}
私はchronometerについてのチュートリアルを読んでいます。それは私が2つの異なる活動でクロノメーターを使用する必要が始まるまでうまく動作します。以下のコード行には戻り値が必要です。UIのTextViewをrunnableインターフェイスを使用するクラスから更新するにはどうすればよいですか?
((Activity) context).updateTimerText(String.format("%02d:%02d:%02d:%03d", hours, minutes, seconds, ms));
私はHandlerクラスを使用しようとしますが、実行するには実行可能オブジェクトが必要です。
を必要とするものであると信じています。私は 'runOnUiThread'を使用しましたが、この状況で2つの異なる場合、' Activity'に値を返す方法がわかりません。 – detector
ここに実際に必要なものは何ですか? 2つの異なるアクティビティ間で同じテキストビューを再利用しようとしていますか?それは間違っているからです。それとも、前の時間を次の時間に設定する必要がありますか? –
2種類のアクティビティ( 'Activity'、' Activity2')の中にディスプレイストッパーを試してみます。このクラスは私がTextViewの中に入れたい時間に関する情報を与えます。問題は最後の行のコード '(Activity)context).updateTimerText'です。まず、アクティビティにメソッド 'updateTimerText'がある場合は情報が必要なので、2つの異なるアクティビティの変数を返すだけではできません。 – detector