在Android中,如何采取的动作,当一个变量的变化? [英] In Android, how do I take an action whenever a variable changes?

查看:454
本文介绍了在Android中,如何采取的动作,当一个变量的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Android应用程序(或Java更普遍,如果是没有什么不同),什么是调用一个方法的最佳途径,只要一个变量的值发生变化?

In an Android app (or Java more generally if it's no different), what is the best way of calling a method whenever a variable's value changes?

推荐答案

你真的想要做的是建立事件驱动模型来触发一个侦听器,当一个事件发生什么(在你的情况,说一个变量的值发生了变化) 。这是很常见的,不仅对Java,但对于其他的编程语言,以及特别是在UI编程的上下文中(虽然它不一定只对)

What you really want to do is set up event-driven model to trigger a listener when an event happen (in your case, say a variable value has changed). This is very common not only for Java, but for other programming languages as well especially in the context of UI programming (though it is not necessarily only for that)

通常,这是通过执行以下步骤进行:

Typically this is done by doing the following steps:

  • 决定听者应在事件的情况下执行被触发的接口。对于你的情况,你可以把它叫做VariableChangeListener和定义接口:


    public interface VariableChangeListener {
        public void onVariableChanged(Object... variableThatHasChanged);
    }

您可以把你认为的监听器来处理在这里是很重要的任何参数。通过抽象进入界面,必须执行在可变的情况下,必要行动的灵活性,而紧耦合改变了它与该事件发生的类。

You could put any argument that you think it is important for the listener to handle here. By abstracting into the interface, you have flexibility of implementing the necessary action in the case of variable has changed without tight coupling it with the class where the event is occurring.

  • 在发生该事件类(再次在你的情况,在培训班里的变量可能会改变),增加注册一个监听事件的方法。如果你打电话给你的界面VariableChangeListener,那么你将有一个方法,如

    // while I only provide an example with one listener in this method, in many cases
    // you could have a List of Listeners which get triggered in order as the event 
    // occurres
    public void setVariableChangeListener(VariableChangeListener variableChangeListener) {
       this.variableChangeListener = variableChangeListener;
    }

默认有无人听事件

By default there is no one listening to the event

  • 在事件发生的情况下(变量发生了变化),然后你会触发监听器,在code会是什么样子像


   if( variableValue != previousValue && this.variableChangeListener != null) {
       // call the listener here, note that we don't want to a strong coupling
       // between the listener and where the event is occurring. With this pattern
       // the code has the flexibility of assigning the listener
       this.variableChangeListener.onVariableChanged(variableValue);
   }

再次,这是在编程一个非常普遍的做法基本上反应的事件或变量的变化。在Javascript中,你看这是作为一部分的onclick(),在Android中你可以检查的事件驱动模型,各种监听器,如<一个href="http://developer.android.com/reference/android/view/View.OnClickListener.html">OnClickListener在按钮 onclick事件设置。你的情况,你只会触发根据不同的事件监听器是每当变量发生了变化

Again this is a very common practice in programming to basically reacts to an event or variable change. In Javascript you see this is as part of onclick(), in Android you could check the event driven model for various listener such as OnClickListener set on a Button onclick event. In your case you will just trigger the listener based on different event which is whenever the variable has changed

这篇关于在Android中,如何采取的动作,当一个变量的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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