2012-02-15 47 views
1

我有一個循環5每次用戶單擊5 textview創建一個添加到父視圖。但是,如果我連續單擊按鈕(就像之前previos看停止)然後temp值超過0到4,並繼續....我怎麼能重置臨時值(靜態變量)內處理程序。如何重置android延遲處理程序中的一個靜態變量android

 // 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 2012-02-15 08:15:45

+0

喜歡使它成爲零溫度= 0 – Programmer 2012-02-15 08:17:09

+0

不是100%確定你想要什麼,但我修改了我的例子,給出一個可能的方式去指示!祝你好運 – Entreco 2012-02-16 09:22:56

回答

0

編輯:我修改了我的答案。總的來說,我認爲在課堂上增加更多的結構和在一個地方做所有事情是很好的。一個這樣的例子可能是這樣的:

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

Actully你無法做到這一點,因爲k是在外部類,因此我們必須使它最終,如果我們使它最終它不會增加。 – Programmer 2012-02-15 08:50:27