我的代码是:
public class EventDialog2 extends Dialog implements OnTouchListener{
TextView textv;
Context con;
Thread t;
int flag=0;
public EventDialog2(Context context, int dialogslideanim) {
super(context, dialogslideanim);
con = context;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog2);
getWindow().setGravity(Gravity.BOTTOM);
timing();
LinearLayout line = (LinearLayout) findViewById(R.id.linear2);
line.setOnTouchListener(this);
line.removeAllViews();
for (int i = 0; i < 15; i++) {
TextView tv = new TextView(con);
tv.setText("TestProcess " + i);
tv.setTextColor(Color.BLACK);
line.addView(tv);
}
}
private void timing() {
// TODO Auto-generated method stub
if(flag==1){
t.stop();
flag=0;
}
t = new Thread(new Runnable() {
public void run() {
try {
t.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dismiss();
}
});
t.start();
}
public boolean onTouch(View v, MotionEvent event) {
System.out.println("touched");
flag=1;
timing();
return false;
}
}
这里线程工作正常,因为它会在 3 秒后休眠。但是我需要当我们在对话框中触摸时线程应该被重置,并且应该在触摸后 3 秒后休眠。请帮我。谢谢