如何从setOnClickListener获取数据并将其传递给CountdownTimer

时间:2018-01-02 07:44:25

标签: java android parameter-passing countdowntimer

如何获取我在onclick上执行的计算并将该数据用作CountDownTimer的参数?我在onclick方法中将输入简单转换为毫秒,我想将它传递给我的实际计时器以更改持续时间。据我所知,将参数传递给CountDownTimer的工作原理如下:

  private CountDownTimer countDownTimer = new CountDownTimer(duration_variable, 1000) {
        @Override
        public void onTick(long duration_variable) {
            long millis= duration_variable;
            String hms= String.format("%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))
            );
            timer.setText(hms);
        }

        @Override
        public void onFinish() {
            Toast.makeText(getActivity(), "timer stopped",Toast.LENGTH_SHORT).show();
        }
    };
}

现在唯一的问题是从我的onclick方法获取数据。

这是包含onclick方法的类:

public class page2 extends Fragment {
    private static final String TAG =   "page2";

    private Button btntest;
    private TextView timer,tv2;
    private TextView tv;
    private Spinner spinner, spinner2;
    public int hours, mins, duration;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page2_fragment,container,false);

        btntest = (Button) view.findViewById(R.id.button2);
        timer = (TextView) view.findViewById(R.id.Timer);
        tv2 = (TextView) view.findViewById(R.id.tv2);
        tv = (TextView) view.findViewById(R.id.textView3);

        //setting up spinners
        spinner = (Spinner) view.findViewById(R.id.spinner1);
        String [] values = {"00","01","02","03","04","05","06"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner.setAdapter(adapter);
        spinner2 = (Spinner) view.findViewById(R.id.spinner2);
        String [] values2 = {"00","10","20","30","40","50","60"};
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values2);
        adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner2.setAdapter(adapter2);

        btntest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String spin=spinner.getSelectedItem().toString();
                hours = Integer.parseInt(spin);
                hours = hours*3600000;
                String spin2=spinner2.getSelectedItem().toString();
                mins = Integer.parseInt(spin2);
                mins = mins*60000;
                duration = hours+mins;
                tv2.setText(String.valueOf(duration));
                countDownTimer.start();
            }
        });
        return view;
    }

1 个答案:

答案 0 :(得分:0)

  

您可以创建一个从onClickListener传递param的方法。

请注意以下代码。

public void setParam(**params**) {
   CountDownTimer countDownTimer = new CountDownTimer(param1, 1000) {
       @Override
       public void onTick(long duration_variable) {
           long millis = duration_variable;
           String hms = String.format("%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))
        );
        timer.setText(hms);
       }
       @Override
       public void onFinish() {
           Toast.makeText(mContext, "timer stopped", Toast.LENGTH_SHORT).show();
       }
   };
   countDownTimer.start();
}

从点击后调用方法setParam(your_params)..

向我询问更多疑问。

由于