gpt4 book ai didi

java - 取消 CountDownTimer Android Java

转载 作者:行者123 更新时间:2023-12-02 03:44:33 24 4
gpt4 key购买 nike

我在时间选择器对话框中有一个倒计时器。我的类(class)扩展了 Fragment具有三个页面 View 。

当我处于情况 0 并通过时间选择器 CountdownTimer 选择时,一切都很完美。当我处于情况 1 并且我想取消我的时间时,当我单击按钮时什么也没有发生。

只有当我处于情况 0 并像这样开始计时时,timer.start();当我点击timer.cancel()时它起作用;我希望有人能帮助我解决这个问题。

我的 fragment

 public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private CountDownTimer timer;



public PlaceholderFragment() {
}

public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//textView.setText(getArguments().getInt(ARG_SECTION_NUMBER));


//togglebutton.setChecked(false);



text1=(TextView) rootView.findViewById(R.id.textView1);


//textAlarmPrompt = (TextView) rootView.findViewById(R.id.textView1);
textViewTime = (TextView) rootView.findViewById(R.id.textView1);
buttonstartSetDialog = (Button) rootView.findViewById(R.id.btn1);

buttonstartSetDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//textAlarmPrompt.setText("");


openTimePickerDialog(true);



}
});



switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 0: {

ImageView im = (ImageView) rootView.findViewById(R.id.im);
im.setBackgroundResource(R.drawable.thunder1);
ttsButton = (ToggleButton) rootView.findViewById(R.id.btn);
ttsButton.setChecked(true);




break;
}
case 1: {
ImageView im = (ImageView) rootView.findViewById(R.id.im);
im.setBackgroundResource(R.drawable.thunder2);
ttsButton = (ToggleButton) rootView.findViewById(R.id.btn);
ttsButton.setChecked(true);




break;
}
case 2: {
ImageView im = (ImageView) rootView.findViewById(R.id.im);
im.setBackgroundResource(R.drawable.thunder3);
ttsButton = (ToggleButton) rootView.findViewById(R.id.btn);
ttsButton.setChecked(true);




break;
}


}




return rootView;
}

我的时间选择器对话框

 private void openTimePickerDialog(boolean is24r) {
Calendar calendar = Calendar.getInstance();

timePickerDialog = new TimePickerDialog(
getActivity(),
onTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
is24r);
timePickerDialog.setTitle("Set Sleep Time");

timePickerDialog.show();


}

TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timer = new CounterClass((minute * 60 * 1000) + (hourOfDay * 60* 60 * 1000), 1000);
long millis = (minute * 60 * 1000) + (hourOfDay * 60* 60 * 1000);
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
//System.out.println(hms);
textViewTime.setText(hms);


timer.start();




}};


public class CounterClass extends CountDownTimer {


/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/

public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
//System.out.println(hms);


textViewTime.setText(hms);



@Override
public void onFinish() {

System.exit(0);
}
}

当我点击按钮时我尝试停止它

  @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);





if (this.isVisible()) {



btnstop.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
if(timer != null)
timer.cancel();


}
});




}

}

最佳答案

在您的 Activity 中声明您的计时器。

class MyClass extends AppCompatActivity {
**STUFF**
public CountDownTimer timer;
}

然后像这样开始和停止

((MyClass)getActivity()).timer.start();
((MyClass)getActivity()).timer.cancel();

关于java - 取消 CountDownTimer Android Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36429125/

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