2012-02-15 33 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