2

当我触摸显示屏上的任何位置时,我想让我的对话框可见。我有一个活动、一个视图和一个渲染器类。我在 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();
    }
}

我的视图类:

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"

我该如何解决?


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

>

公共类 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

4 回答 4

0

代替

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

AlertDialog.Builder builder = new AlertDialog.Builder(MapEditorView.this.getContext());
于 2012-12-07T09:50:42.977 回答
0

写在下面的代码行

builder.show();

代替

AlertDialog alert = builder.create();

它会解决你的问题。

于 2012-12-07T09:59:32.583 回答
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;
}

}

于 2012-12-07T14:22:56.680 回答
0

代替

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

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

你的问题就会得到解决。

于 2012-12-07T09:36:13.157 回答