触摸屏幕上的任何位置时如何使对话框可见?

时间:2012-12-07 09:29:23

标签: android android-alertdialog

当我触摸显示屏上的任何地方时,我想让我的对话框可见。 我有一个Activity,一个视图和一个Renderer类。我在View类中编写了我的代码:

public boolean onTouchEvent(final MotionEvent event) {
        queueEvent(new Runnable() {....}}

我发布了我的代码。 (仅限活动和观点)

public class MapEditorActivity extends Activity {
    private GLSurfaceView mGLSurfaceView;
    Region dialogRegion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        // Create our Preview view and set it as the content of our
        // Activity
        mGLSurfaceView = new MapEditorView(this);
        setContentView(mGLSurfaceView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGLSurfaceView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGLSurfaceView.onResume();
    }
}

我的View类:

public class MapEditorView extends GLSurfaceView {
    private static final String LOG_TAG = "de.rwth.arlab.colofields.ui.MapEditorView";

    MapEditorRenderer editMapRenderer;

    // we have a maximum of 5 pointer
    private int[] coordsX = new int[5];
    private int[] coordsY = new int[5];

    // we have to save first touch for motion
    private int[] coordsXFirst = new int[5];
    private int[] coordsYFirst = new int[5];

    // offsets for view
    private float offX;
    private float offY;
    private float offZ;
    Context context;
    boolean zooming;

    public MapEditorView(Context context) {
        super(context);
        editMapRenderer = new MapEditorRenderer(context);
        setRenderer(editMapRenderer);

        // initialize as not present
        Arrays.fill(coordsX, Integer.MIN_VALUE);
        Arrays.fill(coordsY, Integer.MIN_VALUE);
        zooming = false;
    }

    public boolean onTouchEvent(final MotionEvent event) {
        queueEvent(new Runnable() {

            // TODO: Speak about this thing, this functions need API8
            @SuppressLint("NewApi")
            public void run() {

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    offX = editMapRenderer.getxView();
                    offY = editMapRenderer.getyView();
                    coordsXFirst[0] = (int) event.getX(0);
                    coordsYFirst[0] = (int) event.getY(0);

                    startDialog(null);
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    offZ = editMapRenderer.getzView();
                    coordsXFirst[0] = (int) event.getX(0);
                    coordsYFirst[0] = (int) event.getY(0);
                    coordsXFirst[1] = (int) event.getX(1);
                    coordsYFirst[1] = (int) event.getY(1);
                    zooming=true;
                    break;
                case MotionEvent.ACTION_UP:
                    zooming=false;
                    break;

                default:

                    // track all touches
                    for (int i = 0; i < event.getPointerCount(); i++) {
                        int id = event.getPointerId(i);

                        // update all other pointer positions
                        coordsX[id] = (int) event.getX(i);
                        coordsY[id] = (int) event.getY(i);

                    }

                    // When only one finger
                    if (event.getPointerCount() == 1 && !zooming)
                        editMapRenderer.setPosition(offX
                                + (coordsX[0] - coordsXFirst[0])*editMapRenderer.getzView()*-1 / 1000.0f, offY
                                + (coordsYFirst[0] - coordsY[0])*editMapRenderer.getzView()*-1 / 1000.0f);
                    // When two fingers
                    float distanceOld = (float) FloatMath
                            .sqrt((coordsXFirst[1] - coordsXFirst[0])
                                    * (coordsXFirst[1] - coordsXFirst[0])
                                    + (coordsYFirst[1] - coordsYFirst[0])
                                    * (coordsYFirst[1] - coordsYFirst[0]));
                    float distanceNew = (float) FloatMath
                            .sqrt((coordsX[1] - coordsX[0])
                                    * (coordsX[1] - coordsX[0])
                                    + (coordsY[1] - coordsY[0])
                                    * (coordsY[1] - coordsY[0]));
                    float distanceMax = (float) FloatMath
                            .sqrt(getWidth()
                                    * getWidth()
                                    + getHeight()
                                    * getHeight());


                    if (event.getPointerCount() == 2) {
                        editMapRenderer.setZoom(offZ + (distanceNew
                                - distanceOld)/(distanceMax/10));

                    }
                    break;
                }

            }

                 public Dialog startDialog(Bundle savedInstanceState) {

                       final CharSequence[] items = new String[] { "#F5F6F6", "#FFC0CB", "#FF0000", 
                                "#C00000","#62492F","#DB3C00", "#98E01C" };

                       AlertDialog.Builder builder = new AlertDialog.Builder(this);
                       builder.setTitle("Pick a color");
                       builder.setItems(items, new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int item) {
                               Toast.makeText((Context) getApplicationWindowToken(), items[item], Toast.LENGTH_SHORT).show();
                            }
                       });
                       AlertDialog alert = builder.create();
                 }
        });
        return true;
    }

    MapEditorRenderer mRenderer;

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater =((Activity)context).getMenuInflater();
        inflater.inflate(R.layout.menu_save, menu);
        return true;
    }
 }

我的问题是方法startDialog()。 这是行不通的。我收到一个错误:

"The constructor AlertDialog.Builder(new Runnable(){}) is undefined"

我该如何解决?


好的,我再次发布我的代码:

&GT;

公共类MapEditorActivity扩展了Activity {

private GLSurfaceView mGLSurfaceView;

Region dialogRegion;

MapEditorRenderer editMapRenderer;

// we have a maximum of 5 pointer
private int[] coordsX = new int[5];
private int[] coordsY = new int[5];

// we have to save first touch for motion
private int[] coordsXFirst = new int[5];
private int[] coordsYFirst = new int[5];

// offsets for view
private float offX;
private float offY;
private float offZ;
Context context;
boolean zooming;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    // Create our Preview view and set it as the content of our
    // Activity
    mGLSurfaceView = new MapEditorView(this);



    setContentView(mGLSurfaceView);
    mGLSurfaceView.setOnTouchListener(MapEditorActivity.this);
}


@Override
protected void onPause() {
    super.onPause();
    mGLSurfaceView.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    mGLSurfaceView.onResume();
}

public boolean onTouchEvent(final MotionEvent event) {
    queueEvent(new Runnable() {

        // TODO: Speak about this thing, this functions need API8
        @SuppressLint("NewApi")
        public void run() {

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                offX = editMapRenderer.getxView();
                offY = editMapRenderer.getyView();
                coordsXFirst[0] = (int) event.getX(0);
                coordsYFirst[0] = (int) event.getY(0);

                startDialog(null);
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                offZ = editMapRenderer.getzView();
                coordsXFirst[0] = (int) event.getX(0);
                coordsYFirst[0] = (int) event.getY(0);
                coordsXFirst[1] = (int) event.getX(1);
                coordsYFirst[1] = (int) event.getY(1);
                zooming=true;
                break;
            case MotionEvent.ACTION_UP:
                zooming=false;
                break;

            default:

                // track all touches
                for (int i = 0; i < event.getPointerCount(); i++) {
                    int id = event.getPointerId(i);

                    // update all other pointer positions
                    coordsX[id] = (int) event.getX(i);
                    coordsY[id] = (int) event.getY(i);

                }

                // When only one finger
                if (event.getPointerCount() == 1 && !zooming)
                    editMapRenderer.setPosition(offX
                            + (coordsX[0] - coordsXFirst[0])*editMapRenderer.getzView()*-1 / 1000.0f, offY
                            + (coordsYFirst[0] - coordsY[0])*editMapRenderer.getzView()*-1 / 1000.0f);
                // When two fingers
                float distanceOld = (float) FloatMath
                        .sqrt((coordsXFirst[1] - coordsXFirst[0])
                                * (coordsXFirst[1] - coordsXFirst[0])
                                + (coordsYFirst[1] - coordsYFirst[0])
                                * (coordsYFirst[1] - coordsYFirst[0]));
                float distanceNew = (float) FloatMath
                        .sqrt((coordsX[1] - coordsX[0])
                                * (coordsX[1] - coordsX[0])
                                + (coordsY[1] - coordsY[0])
                                * (coordsY[1] - coordsY[0]));
                float distanceMax = (float) FloatMath
                        .sqrt(getWidth()
                                * getWidth()
                                + getHeight()
                                * getHeight());


                if (event.getPointerCount() == 2) {
                    editMapRenderer.setZoom(offZ + (distanceNew
                            - distanceOld)/(distanceMax/10));

                }
                break;
            }

        }



             private int getHeight() {
            // TODO Auto-generated method stub
            return 0;
        }



            private int getWidth() {
            // TODO Auto-generated method stub
            return 0;
        }



            public Dialog startDialog(Bundle savedInstanceState) {

                   final CharSequence[] items = new String[] { "#F5F6F6", "#FFC0CB", "#FF0000", 
                            "#C00000","#62492F","#DB3C00", "#98E01C" };

                   AlertDialog.Builder builder = new AlertDialog.Builder(MapEditorActivity.this);
                   builder.setTitle("Pick a color");
                   builder.setItems(items, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int item) {
                           Toast.makeText((Context) getApplication(), items[item], Toast.LENGTH_SHORT).show();
                        }
                   });
                   AlertDialog alert = builder.create();

                return alert;

             }






    });
    return true;
}

private void queueEvent(Runnable runnable) {
    // TODO Auto-generated method stub

}

MapEditorRenderer mRenderer;

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater =((Activity)context).getMenuInflater();
    inflater.inflate(R.layout.menu_save, menu);
    return true;
}

}

4 个答案:

答案 0 :(得分:0)

替换

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());

你的问题将得到解决。

答案 1 :(得分:0)

替换

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

AlertDialog.Builder builder = new AlertDialog.Builder(MapEditorView.this.getContext());

答案 2 :(得分:0)

写下面的代码行

builder.show();

而不是

AlertDialog alert = builder.create();

它将解决您的问题。

答案 3 :(得分:0)

好的,我已经改变了我的代码。这是最后一个版本。但我有同样的问题。 公共类MapEditorActivity扩展Activity实现OnTouchListener {

private GLSurfaceView mGLSurfaceView;

MapEditorRenderer editMapRenderer;
// we have a maximum of 5 pointer
    private int[] coordsX = new int[5];
    private int[] coordsY = new int[5];

    // we have to save first touch for motion
    private int[] coordsXFirst = new int[5];
    private int[] coordsYFirst = new int[5];

    // offsets for view
    private float offX;
    private float offY;
    private float offZ;
    Context context;
    boolean zooming;
Region dialogRegion;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    // Create our Preview view and set it as the content of our
    // Activity
    mGLSurfaceView = new MapEditorView(this);



    setContentView(mGLSurfaceView);
    mGLSurfaceView.setOnTouchListener(MapEditorActivity.this);



}

private void showDialog(){

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    final String[] color = { "#F5F6F6", "#FFC0CB", "#FF0000", 
            "#C00000","#62492F","#DB3C00", "#98E01C" };
    dialogBuilder.setTitle("pick one Color");
    dialogBuilder.setItems(color, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {

            Toast.makeText(getApplicationContext(),color[which], Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
}
@Override
protected void onPause() {
    super.onPause();
    mGLSurfaceView.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    mGLSurfaceView.onResume();
}

public boolean onTouch(View v, MotionEvent event) {

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        offX = editMapRenderer.getxView();
        offY = editMapRenderer.getyView();
        coordsXFirst[0] = (int) event.getX(0);
        coordsYFirst[0] = (int) event.getY(0);

        showDialog();
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        offZ = editMapRenderer.getzView();
        coordsXFirst[0] = (int) event.getX(0);
        coordsYFirst[0] = (int) event.getY(0);
        coordsXFirst[1] = (int) event.getX(1);
        coordsYFirst[1] = (int) event.getY(1);
        zooming=true;
        break;
    case MotionEvent.ACTION_UP:
        zooming=false;
        break;

    default:

        // track all touches
        for (int i = 0; i < event.getPointerCount(); i++) {
            int id = event.getPointerId(i);

            // update all other pointer positions
            coordsX[id] = (int) event.getX(i);
            coordsY[id] = (int) event.getY(i);

        }

        // When only one finger
        if (event.getPointerCount() == 1 && !zooming)
            editMapRenderer.setPosition(offX
                    + (coordsX[0] - coordsXFirst[0])*editMapRenderer.getzView()*-1 / 1000.0f, offY
                    + (coordsYFirst[0] - coordsY[0])*editMapRenderer.getzView()*-1 / 1000.0f);
        // When two fingers
        float distanceOld = (float) FloatMath
                .sqrt((coordsXFirst[1] - coordsXFirst[0])
                        * (coordsXFirst[1] - coordsXFirst[0])
                        + (coordsYFirst[1] - coordsYFirst[0])
                        * (coordsYFirst[1] - coordsYFirst[0]));
        float distanceNew = (float) FloatMath
                .sqrt((coordsX[1] - coordsX[0])
                        * (coordsX[1] - coordsX[0])
                        + (coordsY[1] - coordsY[0])
                        * (coordsY[1] - coordsY[0]));
        float distanceMax = (float) FloatMath
                .sqrt(getWidth()
                        * getWidth()
                        + getHeight()
                        * getHeight());


        if (event.getPointerCount() == 2) {
            editMapRenderer.setZoom(offZ + (distanceNew
                    - distanceOld)/(distanceMax/10));

        }
        break;
    }
    return false;
}

private int getHeight() {
    // TODO Auto-generated method stub
    return 0;
}

private int getWidth() {
    // TODO Auto-generated method stub
    return 0;
}

}