Android:监听变量变化 [英] Android: Listening for variable changes

查看:46
本文介绍了Android:监听变量变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找有关如何执行此操作的 KISS 示例,虽然它们看起来都很简单(而且我已经全部看完了!),但我仍然无法理解这个概念.我指的是自定义侦听器(不扩展任何东西的侦听器)...特别是,为布尔变量创建侦听器,以便在其值发生变化时触发一些方法.

I've been searching for a KISS example of how to do this, and while they all seem (and I've gone through them ALL!) simple enough, I still cannot get my head around the concept. I'm referring to custom listeners (ones that do not extend anything)... In particular, creating a listener for a boolean variable so that I can set off some methods when its value changes.

这是我试过的一个例子:android 如何监听自定义变量?

this is an example of an example I've tried: android how to make listener to a custom variable?

如果有人有时间用简单的英语解释自定义侦听器及其工作原理,我们将不胜感激.我希望一旦我确切地了解了听众是如何收到更改通知的,那么我将能够创建自己的并了解该死的东西是如何工作的.这让我很头疼...提前致谢

If someone has the time to explain a custom listener and how it works in plain English, that would be much appreciated. I'm hoping that once I understand exactly how the listener is notified of a change, then I'll be able to create my own and understand how the damn thing works. This is giving me a headache... Thanks in advance

这是我目前所处的位置:

Here is where I'm at at the moment:

public class ChangeListener {
    boolean boo = false;

    public ChangeListener(boolean b){
        boo = b;
    }

    private listener l = null;

    public interface listener{
        public void onChange(boolean b);
    }

    public void setChangeListener(listener mListener){
        l = mListener;
    }

    public void somethingChanged(){
        if(l != null){
            l.onChange(boo);
        }
    }


}

这是我主要活动的快照:

This is a snapshot of my main activity:

public class Main extends Activity implements ChangeListener.listener{
    private ChangeListener listener;
    private boolean boo = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listener = new ChangeListener(boo);
        listener.setChangeListener(this);
        listener.somethingChanged();

    }

    @Override
    public void onChange(boolean b) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "REST = "+b, Toast.LENGTH_SHORT).show();
    }


}

现在,当 Activity 开始时,Toast 会显示 boo 状态……太好了.问题是这只发生在 onCreate 中,这是有道理的,因为 .somethingChanged 被放置在那里.我想要的是每次 boo 从假变为真,真变为假时生成 Toast,无论它是否在 onCreate 内.即:

Now, when the activity starts, the Toast displays the state of boo... great. Problem is this only happens within onCreate, which makes sense as .somethingChanged is placed in there. What I would like is for the Toast to be generated every time boo is changed from false to true, and true to false regardless if its within onCreate or not. I.e:

  • 点击按钮
  • 导致 boo 从 false 变为 true
  • 侦听器接收更改并运行 onChange 中的任何内容

我知道我可以只使用按钮 actionListener 来做到这一点,但如果可能的话,我想远离这个,目前我开始认为这是不可能的.

I know I could just use the button actionListener to do this, but I would like to stay away from this if possible, which at the moment I'm starting to think is impossible.

任何见解将不胜感激.提前致谢!

Any insight would be much appreciated. Thanks in advance!

推荐答案

您将变量封装在自定义类中,这样唯一的访问函数也会触发侦听器.因此,翻转开关的唯一方法是通过切换和调用侦听器的函数.

You wrap the variable up inside a custom class such that the only access functions also trip the listener. So that the only way to flip the switch is via the functions which toggles and calls the listener.

public class BooVariable {
    private boolean boo = false;
    private ChangeListener listener;

    public boolean isBoo() {
        return boo;
    }

    public void setBoo(boolean boo) {
        this.boo = boo;
        if (listener != null) listener.onChange();
    }

    public ChangeListener getListener() {
        return listener;
    }

    public void setListener(ChangeListener listener) {
        this.listener = listener;
    }

    public interface ChangeListener {
        void onChange();
    }
}

要监控更改,您需要实现 BooVariable.ChangeListener,然后将this"的副本传递给 BooVariable 类,然后当您更改变量时,它会调用 onChange.

To monitor the change you need to implement BooVariable.ChangeListener and then pass the BooVariable class a copy of "this" then, when you change the variable it calls onChange.

另外,请记住,您可以只内联代码而不是直接扩展:

Also, keeping in mind you can just inline the code rather than extend directly:

BooVariable bv = new BooVariable();
bv.setListener(new BooVariable.ChangeListener() {
    @Override
    public void onChange() {
        Toast.makeText(MainActivity.this,"blah", Toast.LENGTH_LONG).show();
     }
});

附注.Toast 必须从 UI 线程调用,因此如果要在不同的线程中更改变量,则需要切换 Looper.不过这可能不会出现.

PS. The toast must be called from the UI thread, so you need to switch the Looper if you're going to change the variable in a different thread. This likely won't come up though.

这篇关于Android:监听变量变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆