gpt4 book ai didi

android - 如何在android对话框中放置一个计时器

转载 作者:行者123 更新时间:2023-11-30 02:57:42 26 4
gpt4 key购买 nike

如何在 android 对话框中放置一个计时器?我已经构建了所有对话框方法,但我似乎无法弄清楚每次计时器触发时如何更新对话框 View 中的文本。我的更新方法已经被另一个类每秒调用一次。

到目前为止,这是我的代码:

    public class PlaybackTimerEndingDialog extends DialogFragment implements TimerCallbacks.updateTimer {
private AlertDialog.Builder mBuilder;
private long mTime;
private Context mContext;
private View mTimerEndingView;

public PlaybackTimerEndingDialog(long startTime, Context context){
this.mTime = startTime;
this.mContext = context;
}


private void updateView(String message_text){
LayoutInflater inflater = this.getActivity().getLayoutInflater();
this.mTimerEndingView = inflater.inflate(R.layout.playback_timer_ending, null);
TextView messageView = (TextView) this.mTimerEndingView.findViewById(R.id.timer_ending_message);
messageView.setText(message_text);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
this.updateView("The sleep timer will expie in "+ this.formatTime(this.mTime));

this.mBuilder = new AlertDialog.Builder(this.mContext)
// this.mBuilder.setMessage("The sleep timer will expie in "+ this.formatTime(this.mTime))
.setPositiveButton("Add more time", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
TimerCallbacks.createTimer listener = (TimerCallbacks.createTimer) PlaybackTimerEndingDialog.this.mContext;
listener.createTimerDialog();
}
})
.setNegativeButton("Cancel timer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
TimerCallbacks.createTimer listener = (TimerCallbacks.createTimer) PlaybackTimerEndingDialog.this.mContext;
listener.stopTimer();
}
});
this.mBuilder.setView(this.mTimerEndingView);
// Create the AlertDialog object and return it
return this.mBuilder.create();
}

@Override
public void finished() {
// TODO Auto-generated method stub
}

@Override
public void update(long time) {
// TODO Auto-generated method stub
// this.getDialog().set
this.updateView("The sleep timer will expie in "+ this.formatTime(time));
this.mTime = time;
Log.d("current time", Long.toString(time));
// this.mBuilder.setMessage("The sleep timer will expie in "+ this.formatTime(time));
}

private String formatTime(long time) {
int seconds = (int) (time / 1000) % 60 ;
int minutes = (int) ((time / (1000*60)) % 60);
String mnStr = (minutes<10 ? "0" : "")+minutes;
String secStr = (seconds<10 ? "0" : "")+seconds;
return "-"+mnStr +":"+ secStr;
}
}

如有任何帮助,我们将不胜感激。

最佳答案

将其放在您想要启动计时器的某个函数中。

        final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
updateView("fired");

}
}, 1000);

Update:当dialog使用的是第一个inflated布局时,它是每次inflate新布局的update方法。

关于android - 如何在android对话框中放置一个计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23021873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com