2012-02-21 15 views
2

ボタンをクリックするたびに(つまり複数のボタンのonclickリスナー内で)変数を変更し、この変数を表示するが、リスナーコードの外に留まるテキストビューを持っています。Androidが変数を変更したときにコードを実行

textviewは変数を更新しないので、変数の 'onclick'ハンドラのバージョンが必要になる可能性があります。

どうすればいいですか?

+0

私はあなたの質問を明確にする必要があると思います。まず、あなたの変数のタイプは何ですか?次に、変数の変更時にリスナーからテキストフィールドを変更する理由は何ですか? –

+0

変数は整数であり、多くのボタンリスナで変更されます。私は変数を変更するたびにsetText(String.valueOf(variable))したい、私はすべてのリスナーに置くことができますが、私はリスナーの外でそれを行う方法があったのだろうか、変数の 'リスナー' 。 – user1220086

+0

メソッドを作成できます。 setNewValue(int newVal)リスナーの外側で、変数を変更するたびにそれを呼び出します。 – m1ntf4n

答えて

15

変数またはクラスフィールドが変更されたときにJavaに通知する方法はありません。あなたがする必要があるのは、値が変わるたびにクライアントがコールバックのために登録できるように、あなたのint用の単純なラッパークラスを実装することです。このクラスは次のようになります。

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インターフェイスを実装するクラスが必要です。あなたはそれをActivityにすることができます。の更新を追跡します。

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:私はこのように、代わりに些細なカスタムTextViewを実装します。たとえば、次のように

<?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> 

必要な設定を行い活動は次のようになります。

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

これはあなたに以下のUI与える:あなたがいずれかをクリックするたびに

App screenshot

TextViewは、OnClickListenersのどれもがコードtを持たないにもかかわらず、それが表示する値をほぼ魔法のように更新しますo TextViewに触れてください!

これは実際にはModel-view-controllerというプログラミングの一般的なパターンです。上記のサンプルコードでは、モデルはIntValueStoreであり、ビューはIntTextViewであり、コントローラはありません。

+0

古いスレッドにコメントしていますが、これは非常に便利です。答えをありがとう。 –

-1

C#では、変数が変更されてJavaになると、何らかのアクションを引き起こす簡単な方法があります。 Java afaikは演算子のオーバーロードをサポートしていませんが、この機能は必要ありません。クラス(オブジェクト)指向のプログラミングについては、標準の型から特別な型を作成してください。新しいクラスは、任意の型(int、long、stringなど)の機能を拡張します。たとえば、独自の "SET"や "GET"メソッドを定義し、標準の "="の代わりにこれらを使用するか、値/ポンターを左のオペランドとして使用します。

例:

public class myINT 
{ 
public int Value; 

public void SET(int v) {Value = v; // PLUS additional ACTION } 
public int GET(int v) {return Value ;} 
public void ADD(int v) {Value += v; // PLUS additional ACTION } 
public void SUB(int v) {Value -= v; // PLUS additional ACTION } 
} 
関連する問題