پروژه ساعت ساده مکتب خونه- فرگمنت- تایمر-رشته از ریسورس گرفتن
سه شنبه, ۹ ارديبهشت ۱۳۹۹، ۰۴:۴۱ ب.ظ
CNTFragment cd=new CNTFragment(); getFragmentManager().beginTransaction().add(R.id.frame,cd).addToBackStack(null) .commit();
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.cntlayout, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); config(view); setonclickss(); } if (TextUtils.isEmpty(min.getText()) || TextUtils.isEmpty(sec.getText())) return; pausebt.setEnabled(true); CountDownTimer bomb; bomb = new CountDownTimer(total, 1000) { @Override public void onTick(long l) { min.setText(doo(Integer.parseInt(min.getText().toString()) - 1)); } @Override public void onFinish() { startbt.setText(getString(R.string.start)); } }; bomb.start(); } }); @Override public void onPause() { super.onPause(); bomb.cancel(); }
public class CNTFragment extends Fragment { TextView msg; EditText min, sec; Boolean active; Button startbt, pausebt; Integer intmin, intsec, total; CountDownTimer bomb; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.cntlayout, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); config(view); setonclickss(); } private void setonclickss() { startbt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (TextUtils.isEmpty(min.getText()) || TextUtils.isEmpty(sec.getText())) return; intmin = Integer.valueOf(min.getText().toString()); intsec = Integer.valueOf(sec.getText().toString()); if (intsec >= 60) { intmin += (intsec / 60); intsec %= 60; } min.setText(doo(intmin)); sec.setText(doo(intsec)); startbt.setEnabled(false); msg.setVisibility(View.INVISIBLE); pausebt.setEnabled(true); total = intmin * 60 + intsec; total *= 1000; min.setEnabled(false); sec.setEnabled(false); bomb = new CountDownTimer(total, 1000) { @Override public void onTick(long l) { if (Integer.parseInt(sec.getText().toString()) == 0) { min.setText(doo(Integer.parseInt(min.getText().toString()) - 1)); sec.setText("59"); } else sec.setText(doo(Integer.parseInt(sec.getText().toString()) - 1)); } @Override public void onFinish() { sec.setText("00"); pausebt.setEnabled(false); startbt.setEnabled(true); startbt.setText(getString(R.string.start)); msg.setVisibility(View.VISIBLE); min.setEnabled(true); sec.setEnabled(true); } }; bomb.start(); active=true; } }); pausebt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { bomb.cancel(); active=false; startbt.setEnabled(true); startbt.setText(getString(R.string.resume)); pausebt.setEnabled(false); } }); } private void config(View v) { startbt = v.findViewById(R.id.count_down_start_resume); pausebt = v.findViewById(R.id.count_down_pause_clear); min = v.findViewById(R.id.minutes_input); sec = v.findViewById(R.id.seconds_input); msg = v.findViewById(R.id.end_message); msg.setVisibility(View.INVISIBLE); pausebt.setEnabled(false); active=false; } @Override public void onPause() { super.onPause(); if(active){ active=false; bomb.cancel(); startbt.setEnabled(true); startbt.setText(getString(R.string.resume)); pausebt.setEnabled(false);} } String doo(Integer k) { if(k<10) return "0"+String.valueOf(k); return String.valueOf(k); } }
۹۹/۰۲/۰۹