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

查看:136
本文介绍了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();
    }


}

现在,当活动开始,Toast显示嘘声的状态......太棒了。问题是这只发生在onCreate中,这有意义,因为.somethingChanged放在那里。我想要的是每次boo从false变为true时生成Toast,并且无论是否在onCreate内,都为true。即:

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所需的变更然后,当你更改它调用onChange的变量时,将BooVariable类传递给this的副本。

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();
     }
});

PS。必须从UI线程调用toast,因此如果要在不同的线程中更改变量,则需要切换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天全站免登陆