ボタンのテキストを数秒後、たとえば5秒後に変更したいとします。ボタンのテキストを、ボタンを押したときに、Androidでクリックした後に変更します。
最初にアクティビティにボタンが1つあり、このボタンをクリックすると というテキストが表示され、1つの数字をテキストとして表示した後に0-10、 とすると、ボタンを押すと再び "?"に変わります。 5秒後に
私はThread.sleep()を試しましたが、動作しませんでした。
おかげ
ボタンのテキストを数秒後、たとえば5秒後に変更したいとします。ボタンのテキストを、ボタンを押したときに、Androidでクリックした後に変更します。
最初にアクティビティにボタンが1つあり、このボタンをクリックすると というテキストが表示され、1つの数字をテキストとして表示した後に0-10、 とすると、ボタンを押すと再び "?"に変わります。 5秒後に
私はThread.sleep()を試しましたが、動作しませんでした。
おかげ
使用:
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View view)
{
btn.setText(""+i);
Handler.postDelayed(new Runnable()
{
public void run()
{
btn.setText("?");
}
}, 5000);
}
}
//クラスレベルで宣言
タイマータイマー= NULL;
Handler handler=new Handler() {
@Override
public void handleMessage(Message msg)
{
// update UI here i.e. set The value on Text View
}
};
// ActivityからstartTheTaskメソッドを呼び出し、それは、すべての5秒後に動作するタイマーを開始する
ボイドstartTheTask(){
if(timer != null)
{
timer.cancel();
timer = new Timer();
TimerTask timerTask = new TimerTask()
{
public void run()
{
// your random number code generation here
// to update UI call
handler.sendMessage(handler.obtainMessage());
}
};
timer.schedule(timerTask, 5000);
}
}
ここにはボタンの派生的な機能があります。
package com.anonymous.sample;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
public class AutoChangeButton extends Button {
private static final int DEFAULT_DELAY = 1000;
private Runnable backToQuestionMark = new Runnable() {
public void run() {
setText("?");
}
};
private void initButton() {
setText("?");
this.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
setText("foo~~");
postDelayed(backToQuestionMark, DEFAULT_DELAY);
}
});
}
public AutoChangeButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initButton();
}
public AutoChangeButton(Context context, AttributeSet attrs) {
super(context, attrs);
initButton();
}
public AutoChangeButton(Context context) {
super(context);
initButton();
}
}
あなたは、タイマーを設定すると、ボタンをクリックし、下に次のコードのようにボタンのテキストを変更することができます。
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view)
{
button.setText(sometext);
timer.schedule(new TimerTask()
{
public void run()
{
button.setText(text);
}
}, 0, 3000);
}
}
感謝し、私は同じことを実現しているし、それが正常に動作しています。 –
あなたの問題が解決した場合は、回答を受け入れてください。これは将来的に迅速に回答を得るのに役立ちます。 – jeet