2012-02-15 10 views
1

ユーザが5回クリックするたびにループが5回あり、親ビューに追加されます。しかし、ボタンを連続的にクリックすると(previosが見えなくなる前のように)、temp値が0から4に上がり、次のように進みます。どのようにしてハンドラ内の一時的な値(静的変数)をリセットできますか?アンドロイドの投稿遅延ハンドラ内で静的変数をリセットする方法

 // start of program. 
     static int temp = 0; 

     // button on click event 
     temp = 0; 

         for(k = 0; k < 5; k++){ 
          new Handler().postDelayed(new Runnable() { 
           public void run() { 
            Animation a1 = new AlphaAnimation(0.00f, 1.00f); 
            a1.setDuration(350); 
            a1.setFillAfter(true); 
            TextView tv = new TextView(Main.this); 
            tv.setVisibility(View.INVISIBLE); 

           // tv.setText(emotionnames.get(temp)); //crashing here. index is 5 size is 5 


            Log.i("temp", Integer.toString(temp)); 
            tv.setTextSize(32); 
            tv.setPadding(10, 0, 10, 0); 
            tv.clearAnimation(); 
            tv.startAnimation(a1); 
            lhsv.addView(tv); 

            temp++; 
           } 
          }, 500 + 500 * k); 
        } 
+0

リセットしたらどういう意味ですか? – Altaaf

+0

0 temp = 0にするのが好きです – Programmer

+0

あなたが望むものは100%ではありませんが、私の例を変更して、可能な方法を示すようにしました!幸運 – Entreco

答えて

0

EDIT:私の答えを変更しました。一般的には、クラスに構造を追加して、すべてを1か所で行うのが良いと思います。このような例の一つは、以下のようなものがあります使用を容易にするため

public class StackoverflowActivity extends Activity { 

    private Button mBtn; 
    private LinearLayout mContainer; 

    private final Handler mHandler = new Handler() 
    { 
    public void handleMessage(Message msg) 
    { 
     int index = msg.what; 
     addTextViewAnimated(index); 
    } 
    }; 


    // Called when the add-button is clicked 
    private OnClickListener myClickListener = new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) { 

      // Create 5 textViews 
      for(int k=0; k < 5; k++) 
      { 
       mHandler.sendEmptyMessageDelayed(k, 500 + 500 * k); 
      } 
     } 

    }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Container view -> the linearlayout inside a scrollview 
     mContainer = (LinearLayout)findViewById(R.id.container); 

     // The button to click 
     mBtn = (Button)findViewById(R.id.btn); 

     // Add the clicklistener 
     mBtn.setOnClickListener(myClickListener); 

    } 

    // This function creates the TextView 
    // And adds it to the container Animated 
    public void addTextViewAnimated(int index) 
    { 
     Animation a1 = new AlphaAnimation(0.00f, 1.00f); 
     a1.setDuration(350); 
     a1.setFillAfter(true); 
     TextView tv = new TextView(this); 
     tv.setVisibility(View.VISIBLE); 

     tv.setTextSize(32); 
     tv.setPadding(10, 0, 10, 0); 
     tv.clearAnimation(); 
     tv.startAnimation(a1); 
     tv.setText("Textview " + index); 
     mContainer.addView(tv); 
    } 
} 

、ここに私のmain.xmlです:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentBottom="true" 
    android:layout_centerVertical="true" 
    android:text="Button" /> 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:fillViewport="true" 
    android:layout_above="@id/btn" > 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 
    </LinearLayout> 
</ScrollView> 

</RelativeLayout> 

多分これはあなたが始められるでしょう。私は確かにあなたが達成したいとは思っていません。

+0

あなたはkが外側クラスにあるので、これを行うことはできません。したがって、最終的にそれを作る必要があります。 – Programmer

関連する問題