每次用户点击时,我都会有一个5循环创建一个添加到父视图的textview。但是,如果我连续点击按钮(就像在previos看停止之前那样),那么临时值会超过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 :(得分: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>
也许这会让你开始。我不确定,你想要达到什么目的。