0

我的代码是:

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 秒后休眠。请帮我。谢谢

4

1 回答 1

1

在您调用dismiss()之前,您需要测试自上次触摸以来是否已经过3秒;这就是我将如何做到的。

public class EventDialog2 extends Dialog implements OnTouchListener{

    TextView textv;
    Context con;
    Thread t;
    int flag=0;
    Long lastTouchHappenedAt = System.currentTimeMillis();
    Handler handle = new Handler();

    public EventDialog2(Context context, int dialogslideanim) {
        super(context, dialogslideanim);
        con = context;

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog2);
        getWindow().setGravity(Gravity.BOTTOM);
        handle.postDelayed(delayDismis, 3000);

        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 Runnable delayDismis = new Runnable() {
        public void run() {
                if(System.currentTimeMillis() - lastTouchHappenedAt >= 3000){
                    dismiss();
                    {
            }
        };


    public boolean onTouch(View v, MotionEvent event) {
        System.out.println("touched");
        flag=1;
        lastTouchHappenedAt = System.currentTimeMillis();
        handle.postDelayed(delayDismis, 3000);
        return false;
    }
}
于 2012-10-25T12:12:25.870 回答