如何在android中重置延迟后处理程序中的静态变量
我有一个循环5每次用户点击5文本视图创建一个添加到父视图。但是,如果我连续单击按钮(如在previos look stop之前),则temp值将超过0到4并继续。。。。如何在处理程序中重置临时值(静态变量)如何在android中重置延迟后处理程序中的静态变量,android,static,handler,Android,Static,Handler,我有一个循环5每次用户点击5文本视图创建一个添加到父视图。但是,如果我连续单击按钮(如在previos look stop之前),则temp值将超过0到4并继续。。。。如何在处理程序中重置临时值(静态变量) // start of program. static int temp = 0; // button on click event temp = 0; for(k = 0; k
// 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;
对于(k=0;k<5;k++){
new Handler().postDelayed(new Runnable()){
public void run(){
动画a1=新字母动画(0.00f,1.00f);
a1.设定持续时间(350);
a1.设置后填充(真);
TextView tv=新的TextView(Main.this);
tv.setVisibility(视图.不可见);
//tv.setText(emotionnames.get(temp));//在这里崩溃。索引为5大小为5
Log.i(“temp”,Integer.toString(temp));
tv.setTextSize(32);
设置填充(10,0,10,0);
tv.clearAnimation();
电视直播(a1);
lhsv.addView(电视);
temp++;
}
},500+500*k);
}
编辑:我修改了我的答案。总的来说,我认为在类中添加更多的结构比在一个地方做所有事情要好。其中一个示例可能如下所示:
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);
}
}
公共类StackoverflowActivity扩展活动{
专用按钮mBtn;
私人线路布局mContainer;
私有最终处理程序mHandler=新处理程序()
{
公共无效handleMessage(消息消息消息)
{
int index=msg.what;
addTextViewAnimated(索引);
}
};
//单击“添加”按钮时调用
私有OnClickListener myClickListener=新OnClickListener()
{
@凌驾
公共void onClick(视图v){
//创建5个文本视图
对于(int k=0;k<5;k++)
{
mHandler.sendEmptyMessageDelayed(k,500+500*k);
}
}
};
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//容器视图->滚动视图中的线性布局
mContainer=(LinearLayout)findViewById(R.id.container);
//单击要单击的按钮
mBtn=(按钮)findviewbyd(R.id.btn);
//添加clicklistener
mBtn.setOnClickListener(myClickListener);
}
//此函数用于创建TextView
//并将其添加到容器中
public void addTextViewAnimated(int索引)
{
动画a1=新字母动画(0.00f,1.00f);
a1.设定持续时间(350);
a1.设置后填充(真);
TextView tv=新的TextView(此);
tv.setVisibility(View.VISIBLE);
tv.setTextSize(32);
设置填充(10,0,10,0);
tv.clearAnimation();
电视直播(a1);
tv.setText(“文本视图”+索引);
mContainer.addView(电视);
}
}
为了便于使用,下面是我的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>
也许这会让你开始。我不完全确定您想要实现什么。就像将其设置为零temp=0不100%确定您想要什么,但我修改了我的示例,以指示可能的方法!幸运的是,实际上你不能这样做,因为k在外类,因此我们必须使它最终,如果我们使它最终,它不会增加。