怎样的android使侦听器添加到自定义变量?侦听器、自定义、变量、android

2023-09-05 00:04:49 作者:终究只是一时的过客。

我已经看到了这个主题: Android的:如何实现监听器如何实现侦听器。

它实际上pretty的简单,但我不明白究竟怎么做的,以及如何在自己的code实现。

我有这样的静态变量变量:AppLoader.isInternetOn。 我想建立一个监听器,将听这个变量的变化和更新的TextView。

我应该这样做:

建立一个接口:

 公共接口InternetStateListener {
     公共无效onStateChange();
}
 

在我的活动运行:

 公共类MyActivity延伸活动{
私人InternetStateListener mListener;

setTheListener(本);

公共无效setTheListener(InternetStateListener听){
    mListener =听;
}

私人无效onStateChange(){
    如果(mListener!= NULL){
       如果(AppLoader.isInternetOn)
        text.setText(上)
       其他
        text.setText(关)
    }
  }
}
 

解决方案 Android环境变量设置

您的活动确实没有什么特别的,只是自身注册与提供听者其他类(因为接口是直接在类中实现)。

 公共类MyActivity扩展活动实现InternetManager.Listener {

    私人TextView的多行文字;
    私人InternetManager mInetMgr;

    / *调用一样的onCreate在某个时间点* /
    公共无效onStateChange(布尔州){
        如果(州){
            mText.setText(上);
        } 其他 {
            mText.setText(关);
        }
    }

    公共无效的onCreate(){
        mInetMgr =新InternetManager();
        mInetMgr.registerListener(本);
        mInetMgr.doYourWork();
    }
}
 

其他类有做pretty的太多了所有的工作。此外,它具有处理听众它调用 onStateChange 方法一次东西happend登记。

 公共类InternetManager {
    //所有的听众下面的东西
    公共接口监听{
        公共无效onStateChange(布尔州);
    }

    私人监听mListener = NULL;
    公共无效registerListener(监听器监听){
        mListener =侦听器;
    }

    // -----------------------------
    //这个类做的部分

    私人布尔isInternetOn = FALSE;
    公共无效doYourWork(){
        //做的事情在这里
        // 在某一点
        isInternetOn = TRUE;
        //现在通知,如果有人有兴趣。
        如果(mListener!= NULL)
            mListener.onStateChange(isInternetOn);
    }
}
 

i've seen this thread : Android: how to implement listener about implement listeners.

its actually pretty simple but i don't get how exactly its done and how to implement in my own code.

i have this static variable variable: AppLoader.isInternetOn. i want to build a listener which will listen to this variable changes and update a TextView.

should i do this: ?

build an interface:

public interface InternetStateListener {
     public void onStateChange();
}

run it in my activity:

public class MyActivity extends Activity {
private InternetStateListener mListener;

setTheListener(this);

public void setTheListener(InternetStateListener listen) {
    mListener = listen;
}

private void onStateChange() {
    if (mListener != null) {
       if (AppLoader.isInternetOn)
        text.setText("on")
       else
        text.setText("off")
    }
  }
}

解决方案

Your Activity does nothing special, just register itself (since the interface is implemented directly in the class) with the Other class that provides the listener.

public class MyActivity extends Activity implements InternetManager.Listener {

    private TextView mText;
    private InternetManager mInetMgr;

    /* called just like onCreate at some point in time */ 
    public void onStateChange(boolean state) {
        if (state) {
            mText.setText("on");
        } else {
            mText.setText("off");
        }
    }

    public void onCreate() {
        mInetMgr = new InternetManager();
        mInetMgr.registerListener(this);
        mInetMgr.doYourWork();
    }
}

The other class has to do pretty much all the work. Besides that it has to handle the registration of listeners it has to call the onStateChange method once something happend.

public class InternetManager {
    // all the listener stuff below
    public interface Listener {
        public void onStateChange(boolean state);
    }

    private Listener mListener = null;
    public void registerListener (Listener listener) {
        mListener = listener;
    }

    // -----------------------------
    // the part that this class does

    private boolean isInternetOn = false;
    public void doYourWork() {
        // do things here
        // at some point
        isInternetOn = true;
        // now notify if someone is interested.
        if (mListener != null)
            mListener.onStateChange(isInternetOn);
    }
}