Android studio如何通过输入的时间实现倒计时的功能

Android studio如何通过输入的时间实现倒计时的功能,第1张

概述话不多说直接上代码fragment_lamp.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apkes/android"xmlns:app="http://schemas.android.com/apkes-auto"xmlns:

话不多说直接上代码

fragment_lamp.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:background="@color/white"    androID:gravity="center"    androID:orIEntation="horizontal"    tools:context=".fragment.LampFragment">    <linearLayout        androID:ID="@+ID/buju2"        androID:layout_wIDth="wrap_content"        androID:layout_height="300dp"        androID:gravity="center"        androID:orIEntation="vertical">        <TextVIEw            androID:ID="@+ID/wenben_1"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_weight="1"            androID:paddingleft="20dp"            androID:paddingRight="35dp"            androID:text="@string/wenben_1"            androID:textcolor="@color/colorPrimary"            androID:textSize="25sp" />        <linearLayout            androID:ID="@+ID/buju4"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:background="@drawable/bg_time"            androID:gravity="bottom"            androID:padding="10dp">            <EditText                androID:ID="@+ID/ed_hour"                androID:layout_wIDth="70dp"                androID:layout_height="50dp"                androID:gravity="center"                androID:inputType="number"                androID:maxLength="2"                androID:text="00"                androID:textcolor="@color/colorPrimary" />            <TextVIEw                androID:ID="@+ID/maohao1"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_centerVertical="true"                androID:text=":"                androID:textcolor="@color/colorPrimary"                androID:textSize="20sp"                androID:textStyle="bold" />            <EditText                androID:ID="@+ID/ed_min"                androID:layout_wIDth="70dp"                androID:layout_height="50dp"                androID:gravity="center"                androID:inputType="number"                androID:maxLength="2"                androID:text="00"                androID:textcolor="@color/colorPrimary" />            <TextVIEw                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_centerVertical="true"                androID:text=":"                androID:textcolor="@color/colorPrimary"                androID:textSize="20sp"                androID:textStyle="bold" />            <EditText                androID:ID="@+ID/ed_second"                androID:layout_wIDth="70dp"                androID:layout_height="50dp"                androID:gravity="center"                androID:inputType="number"                androID:maxLength="2"                androID:text="00"                androID:textcolor="@color/colorPrimary" />        </linearLayout>    </linearLayout>    <ImageVIEw        androID:ID="@+ID/fengexian"        androID:layout_wIDth="1dp"        androID:layout_height="300dp"        androID:layout_marginleft="20dp"        androID:layout_toRightOf="@+ID/buju2"        androID:background="@color/colorPrimary" />    <linearLayout        androID:ID="@+ID/buju3"        androID:layout_wIDth="wrap_content"        androID:layout_height="300dp"        androID:layout_marginleft="20sp"        androID:gravity="center_horizontal"        androID:orIEntation="vertical">        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="@string/wenben_2"            androID:textcolor="@color/colorPrimary"            androID:textSize="25sp" />        <linearLayout            androID:layout_wIDth="wrap_content"            androID:layout_height="match_parent"            androID:layout_weight="1"            androID:gravity="center">            <TextVIEw                androID:ID="@+ID/tv_time"                androID:layout_wIDth="150dp"                androID:layout_height="wrap_content"                androID:gravity="center"                androID:background="@drawable/bg_time"                androID:padding="10dp"                androID:textcolor="@color/colorPrimary"                androID:textSize="30sp" />        </linearLayout>        <linearLayout            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:gravity="bottom"            androID:padding="10dp">            <button                androID:ID="@+ID/btn_start"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:background="@drawable/shapes"                androID:text="@string/start"                androID:enabled="true"                androID:textcolor="@color/white"                androID:textSize="20sp" />            <button                androID:ID="@+ID/btn_continues"                androID:layout_wIDth="wrap_content"                androID:layout_height="50dp"                androID:layout_marginleft="20dp"                androID:background="@drawable/btn_selector"                androID:text="@string/suspend"                androID:textcolor="@color/white"                androID:enabled="false"                androID:textSize="20sp" />            <button                androID:ID="@+ID/btn_stop"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_marginleft="20dp"                androID:background="@drawable/btn_selector"                androID:text="@string/end"                androID:enabled="false"                androID:textcolor="@color/white"                androID:textSize="20sp" />        </linearLayout>    </linearLayout></linearLayout>
LampFragment.java
public class LampFragment extends BaseFragment<FragmentLampBinding> {    private long time = 0;    public IConnectionManager manager;    CountDownTimer timer;    public static int mm = 0;    @OverrIDe    public voID onVIEwCreated(@NonNull VIEw vIEw, @Nullable Bundle savedInstanceState) {        super.onVIEwCreated(vIEw, savedInstanceState);        init();    }    private voID init() {        manager = SocketManager.getInstance();        // 暂停按键监听事件        vIEwBinding.btnContinues.setonClickListener(VIEw -> {            if(timer==null) {                Toast.makeText(getActivity(), R.string.toast_mes, Toast.LENGTH_SHORT).show();            }else {                if (vIEwBinding.btnContinues.getText().toString().equals(getResources().getString(R.string.continues))) {                    vIEwBinding.btnContinues.setText(R.string.suspend);                    if ("00:00:00".equals(vIEwBinding.tvTime.getText().toString())) {                        Toast.makeText(getActivity(), R.string.toast_mes, Toast.LENGTH_SHORT).show();                    } else {                        String[] item = vIEwBinding.tvTime.getText().toString().split(":");                        int hour = Integer.valueOf(item[0]);                        int min = Integer.valueOf(item[1]);                        int send = Integer.valueOf(item[2]);                        try {                            time = CountDownUntil.formatTurnSecond(hour, min, send);                        } catch (ParseException e) {                            e.printstacktrace();                        }                        startTime(time);                    }                } else {                                       timer.cancel();                    vIEwBinding.btnContinues.setText(R.string.continues);                }            }        });        // 开始按键监听事件        vIEwBinding.btnStart.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                if (MainActivity.flag2 ==0) {                    inputMethodManager imm = (inputMethodManager) getActivity().getSystemService(Context.input_METHOD_SERVICE);                    imm.hIDeSoftinputFromWindow(vIEwBinding.edHour.getwindowToken(), 0);                    vIEwBinding.btnContinues.setText(R.string.suspend);                    int hour = Integer.valueOf(vIEwBinding.edHour.getText().toString());                    int min = Integer.valueOf(vIEwBinding.edMin.getText().toString());                    int send = Integer.valueOf(vIEwBinding.edSecond.getText().toString());                    try {                        time = CountDownUntil.formatTurnSecond(hour, min, send);                    } catch (ParseException e) {                        e.printstacktrace();                    }                    if (time == 0) {                        Toast.makeText(getActivity(), R.string.toast_mes, Toast.LENGTH_SHORT).show();                    } else {                        startTime(time);                        vIEwBinding.btnStart.setEnabled(false);                        vIEwBinding.btnStart.setBackground(getResources().getDrawable(R.drawable.btn_selector));                        vIEwBinding.btnContinues.setEnabled(true);                        vIEwBinding.btnContinues.setBackground(getResources().getDrawable(R.drawable.shapes));                        vIEwBinding.btnStop.setEnabled(true);                        vIEwBinding.btnStop.setBackground(getResources().getDrawable(R.drawable.shapes));                        Log.e("wer", "" + time);                    }                } else {                    Toast.makeText(getActivity(), R.string.lamp, Toast.LENGTH_SHORT).show();                }            }        });        // 结束按键监听事件        vIEwBinding.btnStop.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                                LampFragment.mm=0;                vIEwBinding.tvTime.setText("00:00:00");                timer.cancel();                vIEwBinding.btnStart.setEnabled(true);                vIEwBinding.btnStart.setBackground(getResources().getDrawable(R.drawable.shapes));                vIEwBinding.btnContinues.setEnabled(false);                vIEwBinding.btnContinues.setBackground(getResources().getDrawable(R.drawable.btn_selector));                vIEwBinding.btnStop.setEnabled(true);                vIEwBinding.btnStop.setBackground(getResources().getDrawable(R.drawable.btn_selector));    }});    }    private voID startTime(long T) {        LampFragment.mm=1;               timer = new CountDownTimer(T, 1000) {            @OverrIDe            public voID onTick(long millisUntilFinished) {                Logger.d(millisUntilFinished / 1000);                vIEwBinding.tvTime.setText(CountDownUntil.formatTime(millisUntilFinished));            }            @OverrIDe            public voID onFinish() {                              LampFragment.mm=0;                vIEwBinding.tvTime.setText("00:00:00");                timer.cancel();                vIEwBinding.btnStart.setEnabled(true);                vIEwBinding.btnStart.setBackground(getResources().getDrawable(R.drawable.shapes));                vIEwBinding.btnContinues.setEnabled(false);                vIEwBinding.btnContinues.setBackground(getResources().getDrawable(R.drawable.btn_selector));                vIEwBinding.btnStop.setEnabled(true);                vIEwBinding.btnStop.setBackground(getResources().getDrawable(R.drawable.btn_selector));            }        };        timer.start();    }    @Subscribe(threadMode = ThreadMode.MAIN)    public voID onEventMainThread(MessageWrap event) {    }}

 

总结

以上是内存溢出为你收集整理的Android studio如何通过输入的时间实现倒计时功能全部内容,希望文章能够帮你解决Android studio如何通过输入的时间实现倒计时的功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/998878.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存