微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在片段中调用倒数计时器?

如何解决如何在片段中调用倒数计时器?

我正在制作一个可穿戴应用。我的认布局是框架布局。我在 onFinish 方法中有一个倒数计时器,我想在同一个框架布局中调用 5 秒倒数计时器。

基本上当第一次倒计时结束时,我将所有东西都隐藏起来。然后我调用一个将我连接到我的片段的方法。我最终得到了布局,但它没有倒计时。我的意思是我的片段中的文本视图应该已经改变了,但它没有。

我的倒数计时器是否在下面的代码中声明不正确?

    package com.example.ticwatch_dc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Stop_Pump_fragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Stop_Pump_fragment extends Fragment {

    // Todo: Rename parameter arguments,choose names that match
    // the fragment initialization parameters,e.g. ARG_ITEM_NUMBER
    private static final String ARG_ParaM1 = "param1";
    private static final String ARG_ParaM2 = "param2";
    private TextView textView;

    // Todo: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Stop_Pump_fragment() {
        // required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Stop_Pump_fragment.
     */
    // Todo: Rename and change types and number of parameters
    public static Stop_Pump_fragment newInstance(String param1,String param2) {
        Stop_Pump_fragment fragment = new Stop_Pump_fragment();
        Bundle args = new Bundle();
        args.putString(ARG_ParaM1,param1);
        args.putString(ARG_ParaM2,param2);
        fragment.setArguments(args);
        return fragment;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_ParaM1);
            mParam2 = getArguments().getString(ARG_ParaM2);
        }
        
        //My count down timer method
           counter();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_stop__pump_fragment,container,false);
    }

    public void setText(String text)
    {
        textView = (TextView) getView().findViewById(R.id.textView);
        textView.setText(text);
    }

    public void counter()
    {
        CountDownTimer countDownTimer = new CountDownTimer(5000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //time in minutes and seconds
//            int minutes = (int) (5000 / 1000) / 60;
                int seconds = (int) (5000 / 1000) % 60;
                //formating the above to appear as String
                String timeLeftFormattedsec = String.format("%02d",seconds);
                setText(timeLeftFormattedsec);
            }

            @Override
            public void onFinish() {
                Intent i = new Intent(getActivity(),stop_pump.class);
                startActivity(i);
                ((Activity) getActivity()).overridePendingTransition(0,0);

            }
        }.start();
    }
}

所以基本上我有一个名为 counter方法,我在其中使用了倒数计时器类,并在片段的 onCreate 方法调用它。所以基本上当我运行我的应用程序时,我的活动有一个倒数计时器,完成后,就像我之前所说的那样,使用框架布局作为容器调用我的片段。片段的内容出现了,但片段中的倒计时并没有更新我的文本视图,它被称为 textView 那么我做错了什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?