【问题标题】:How to access the methods in this class in Java如何在 Java 中访问此类中的方法
【发布时间】:2015-09-16 20:27:44
【问题描述】:

我在 android 中有以下点击监听器:

    this.startButton.setOnClickListener(
        new View.OnClickListener() {
            @Override


            public void onClick(View v) {
                flag.set(false);
                   Runnable myRunnable = new Runnable()  {
                    int updateInterval = 4000; //=4 second
                    ImageView currentImage = (ImageView)findViewById(R.id.picture);


                    public void x(){//dosomething}




                    @Override
                    public void run() {
                        if(!flag.get()) {
                            currentImage.postDelayed(this, updateInterval);
                            picture.setImageResource(images[current_image_index]);
                            caption.setText(captions[current_image_index]);
                            current_image_index++;
                            current_image_index = current_image_index % images.length;
                        }
                    }
                };

                myRunnable.run();

            }
    }

    );

但是我有另一个 onClickListener 需要从 myRunnable 调用方法 x()。但是我似乎无法这样做。这里的上下文是,当单击 startButton 时,它会导致图像每 4 秒更改一次,但是我有另一个名为 nextButton 的按钮,单击时会更改图像并且必须重置更新间隔,否则下一个图像会在 nextButton 时更改不一致已按下,谢谢!

【问题讨论】:

  • 这个问题需要一个最低限度的可验证代码示例。
  • 此代码在语法上无效。
  • 很确定它也会锁定主 UI 线程。 vs new Thread(myRunnable).start().
  • 代码有什么问题?
  • 代码不是有什么问题?由于public void x(){//dosomething} 这一行,它是无效的

标签: java android


【解决方案1】:

程序无法按预期运行。您正在覆盖

中的方法

重写方法。最好还是创建一个单独的方法来处理该事件并

然后在你的 eventListener 中调用它。希望这会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多