Android EditText在内部类中为int

时间:2016-05-26 21:52:42

标签: java android timer

因此,在尝试减少EditText的值时,我遇到了一个棘手的问题。让我解释一下,我有一些CountDownTimers,当第二个计时器到达结束时,之前设置的值需要递减,这样定时器就不会重复无限次。

这是我的代码:

public void poslijeDelaya() {

                int counterInt = Integer.parseInt(editTextNOI.getText().toString());

                if (counterInt > 0) {


                    String tekstTrci = editTextHIIVrijeme.getText().toString();
                    long longTrci = Long.parseLong(tekstTrci);

                    CountDownTimer timerTrci = new CountDownTimer(longTrci * 1000 + 100, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            updateTimer((int) millisUntilFinished / 1000);

                        }

                        @Override
                        public void onFinish() {
                            textViewGlavniTimer.setText("00:00");
                            textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.liiState));

                            String tekstHodaj = editTextLIIVrijeme.getText().toString();
                            long longHodaj = Long.parseLong(tekstHodaj);

                            CountDownTimer timerHodaj = new CountDownTimer(longHodaj * 1000 + 100, 1000) {
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    updateTimer((int) millisUntilFinished / 1000);
                                }

                                @Override
                                public void onFinish() {
                                    counterInt --;
                                    textViewGlavniTimer.setText("00:00");
                                    textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.hiiState));
                                    poslijeDelaya();

                                }
                            }.start();

                        }
                    }.start();
                } else {
                    textViewNatpisZaOpisAktivnosti.setText("Done");
                }
            }
        });

棘手的部分是counterInt --;部分。它说变量需要被声明为final,因为它是从内部类访问的,所以我将int counterInt = Integer.parseInt(editTextNOI.getText().toString());设置为final int counterInt = Integer.parseInt(editTextNOI.getText().toString());并猜测是什么?

它表示它不能为最终变量赋值。

关于如何让Android Studio停止拖钓我的任何想法?

5 个答案:

答案 0 :(得分:1)

counterInt作为成员变量,你很高兴。

private int counterInt;

随时分配值。

另外,将counterInt = Integer.parseInt(editTextNOI.getText().toString());移到poslijeDelaya()之外。

答案 1 :(得分:1)

您可以使int成为一个类变量,但是您可以遇到从多个线程访问该值的问题。相反,在int类中创建CountDownTimer类级别。像这样:

 CountDownTimer timerTrci = new CountDownTimer(longTrci * 1000 + 100, 1000) {

                        int counterInt = Integer.parseInt(editTextNOI.getText().toString()); <---- add your variable here

                        @Override
                        public void onTick(long millisUntilFinished) {
                            ...
                        }

                        ....
                        CountDownTimer timerHodaj = new CountDownTimer(longHodaj * 1000 + 100, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                updateTimer((int) millisUntilFinished / 1000);
                            }

                            @Override
                            public void onFinish() {
                                counterInt --;  <-- now this is accessible  
                                ....
                            }
                        }.start();
                        }
                    }.start();

答案 2 :(得分:1)

也许有帮助,当你在超类中添加一个新方法来递减counterInt然后在onFinish()中调用这个方法?

答案 3 :(得分:1)

在进一步阅读您的代码后,我意识到我之前的答案无法正常工作,因为您正在递归调用该方法。所以试试这个:

public void poslijeDelaya(final int count) {
                if (counterInt > 0) {

                    String tekstTrci = editTextHIIVrijeme.getText().toString();
                    long longTrci = Long.parseLong(tekstTrci);

                    CountDownTimer timerTrci = new CountDownTimer(longTrci * 1000 + 100, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            updateTimer((int) millisUntilFinished / 1000);

                        }

                        @Override
                        public void onFinish() {
                            textViewGlavniTimer.setText("00:00");
                            textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.liiState));

                            String tekstHodaj = editTextLIIVrijeme.getText().toString();
                            long longHodaj = Long.parseLong(tekstHodaj);

                            CountDownTimer timerHodaj = new CountDownTimer(longHodaj * 1000 + 100, 1000) {
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    updateTimer((int) millisUntilFinished / 1000);
                                }

                                @Override
                                public void onFinish() {
                                    textViewGlavniTimer.setText("00:00");
                                    textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.hiiState));
                                    poslijeDelaya(counterInt - 1);

                                }
                            }.start();

                        }
                    }.start();
                } else {
                    textViewNatpisZaOpisAktivnosti.setText("Done");
                }
            }
        });

然后,最初这样打电话:

int counterInt = Integer.parseInt(editTextNOI.getText().toString());
poslijeDelaya(counterInt);

答案 4 :(得分:0)

解决方案:

var html = $($(html).find("option").filter(function () { return $(this).html() == "Master"; }).prop('selected', true));