Android:如何在点击画布一段时间后显示弹出对话框

时间:2014-03-06 15:49:35

标签: android canvas dialog popup click

我想在点击画布5秒后输出一个弹出对话框。但是,在我单击画布后,我的对话框会立即出现。我该如何解决这个问题?

我的onTouch代码如下:

public boolean onTouchEvent(MotionEvent event) {

    x = event.getX();
    y = event.getY();

    System.out.println(x);
    System.out.println(y);

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

            AlertDialog.Builder builder = new Builder(getContext());
                    final EditText text = new EditText(getContext());

                    builder.setTitle("Change Name")
                            .setMessage("New Name").setView(text);
                    builder.setPositiveButton("Change",
                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface di, int i) {
                                    name = text.getText().toString();

                                }

                            });
                    builder.setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface di, int i) {
                                }

                            });
                    builder.create().show();

        }

        break;
    case MotionEvent.ACTION_UP:
        sX = event.getX();
        sY = event.getY();
        break;

    case MotionEvent.ACTION_MOVE:
        fX = event.getX();
        fY = event.getY();


        break;

    }// switch

    return true;

}// ontouch

2 个答案:

答案 0 :(得分:0)

我建议您查看Timer上的文档。

答案 1 :(得分:0)

您可以尝试使用带有postDelayed的Handler:

 Handler handler = new Handler();
 handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    AlertDialog.Builder builder = new Builder(getContext());
                    final EditText text = new EditText(getContext());

                    builder.setTitle("Change Name")
                            .setMessage("New Name").setView(text);
                    builder.setPositiveButton("Change",
                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface di, int i) {
                                    name = text.getText().toString();

                                }

                            });
                    builder.setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface di, int i) {
                                }

                            });
                    builder.create().show();
                }
            }, 5000);