Android的执行对变量的变化code [英] Android execute code on variable change

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

问题描述

我改变不同的按钮点击一个变量,(即的onclick侦听多个按钮内),我有一个是想显示这个变量一个TextView,但仍以外的听众code。

I alter a variable on different button clicks, (i.e. inside the onclick listener for multiple buttons) and I have a textview that is suppose to show this variable, but remains outside the listener code.

TextView的犯规更新变量,所以我想我可能需要一个的onclick处理程序版本的变量。

The textview doesnt update the variable, so I am thinking i possibly need an 'onclick' handler version for variables.

我怎么会去这样做?

推荐答案

有没有办法在Java中得到通知,当一个变量或类字段的变化。什么,你需要做的是实现一个简单的包装类的INT,这样客户端可以注册回调每当值改变。这个类可能看起来是这样的:

There is no way in Java to be notified when a variable or class field changes. What you need to do is to implement a simple wrapper class for your int so that clients can register for callbacks whenever the value changes. This class might look something like this:

package com.example.android;

/**
 * A store of an int value. You can register a listener that will be notified
 * when the value changes.
 */
public class IntValueStore {

    /**
     * The current value.
     */
    int mValue;

    /**
     * The listener (you might want turn this into an array to support many
     * listeners)
     */
    private IntValueStoreListener mListener;

    /**
     * Construct a the int store.
     *
     * @param initialValue The initial value.
     */
    public IntValueStore(int initialValue) {
        mValue = initialValue;
    }

    /**
     * Sets a listener on the store. The listener will be modified when the
     * value changes.
     *
     * @param listener The {@link IntValueStoreListener}.
     */
    public void setListener(IntValueStoreListener listener) {
        mListener = listener;
    }

    /**
     * Set a new int value.
     *
     * @param newValue The new value.
     */
    public void setValue(int newValue) {
        mValue = newValue;
        if (mListener != null) {
            mListener.onValueChanged(mValue);
        }
    }

    /**
     * Get the current value.
     *
     * @return The current int value.
     */
    public int getValue() {
        return mValue;
    }

    /**
     * Callbacks by {@link IntValueModel}.
     */
    public static interface IntValueStoreListener {
        /**
         * Called when the value of the int changes.
         *
         * @param newValue The new value.
         */
        void onValueChanged(int newValue);
    }
}

现在,你需要有一些类实现IntValueStoreListener接口。你可以让这成为了活动,这将随后跟踪的的TextView 更新。我想实现一个简单的自定义的TextView 代替,像这样的:

Now you need to have some class that implements the IntValueStoreListener interface. You could let that be the Activity, which would then keep track of a TextView to update. I would implement a trivial custom TextView instead, like this:

package com.example.android;

import com.example.android.IntValueStore.IntValueStoreListener;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class IntTextView extends TextView implements IntValueStoreListener{

    public IntTextView(Context context) {
        super(context);
    }

    public IntTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void onValueChanged(int newValue) {
        // The int got a new value! Update the text
        setText(String.valueOf(newValue));
    }
}

您现在可以设置您的布局XML。例如:

You can now setup your layout XML. For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_increment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Increment" />

    <Button
        android:id="@+id/btn_double"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Double" />

    <com.example.android.IntTextView
        android:id="@+id/text_current_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

这确实必要设置一个活动是这样的:

An activity that does the necessary setup would look like this:

package com.example.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class IntValueStoreActivity extends Activity {

    private IntValueStore mIntValueModel = new IntValueStore(1);

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

        // Initialize the text view with the initial value
        IntTextView intTextView = (IntTextView)findViewById(R.id.text_current_value);
        intTextView.setText(String.valueOf(mIntValueModel.getValue()));

        // Setup the text view as a listener so it gets updated whenever the int
        // value changes
        mIntValueModel.setListener(intTextView);

        // Setup an OnClickListener that will increment the int value by 1. The
        // TextView will be automatically updated since it is setup as a
        // listener to the int value store
        findViewById(R.id.btn_increment).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                mIntValueModel.setValue(mIntValueModel.getValue() + 1);
            }
        });

        // Setup an OnClickListener that will double the int value. The TextView
        // will be automatically updated since it is setup as a listener to the
        // int value store
        findViewById(R.id.btn_double).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                mIntValueModel.setValue(mIntValueModel.getValue() * 2);
            }
        });
    }
}

这会给你以下界面:

每当你点击任何按钮,在的TextView 将几乎神奇地更新它显示的值,即使没有任何OnClickListeners有任何code触摸的TextView

Whenever you click any of the buttons, the TextView will almost magically update the value it displays, even though none of the OnClickListeners have any code to touch the TextView!

这实际上是在规划一个共同的模式称为模型 - 视图 - 控制器。在示例code以上,型号为 IntValueStore 和View是 IntTextView 并没有控制。

This is actually a common pattern in programming called Model-view-controller. In the sample code above, the Model is IntValueStore and the View is IntTextView and there is no Controller.

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

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