有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在安卓的MainActivity之外创建对话框

我想在我的安卓应用程序中插入一个提醒对话框。 当我按照教程将其添加到一个简单的项目中时,这很好,该项目只有一个MainActivity.javaactivity_main.xml和一个layoutxml

然而,当我想在我的项目中插入代码时,我会遇到一些问题。 我的代码是:

安卓.app.AlertDialog alartDialog = new 安卓.app.AlertDialog.Builder(this)
                        .setTitle("Exit?")
                        .setMessage("Are you sure want to QUIT ?")
                        .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                pmaControllerManager.OnExit();
                            }
                        })
                        .setNegativeButton("no", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                dialog.cancel();
                            }
                        })
                        .setNeutralButton("cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.cancel();
                            }
                        })
                        .show();

第一行的中有一个警告符号。 我该怎么修? p、 下面是我调用showDialog的函数(我在按钮[5]中调用它):

private void setbutton()
{
    buttons[0]=(ImageButton)menu.findViewById(R.id.button0);
    buttons[1]=(ImageButton)menu.findViewById(R.id.button1);
    buttons[2]=(ImageButton)menu.findViewById(R.id.button2);
    buttons[3]=(ImageButton)menu.findViewById(R.id.button3);
    buttons[4]=(ImageButton)menu.findViewById(R.id.button4);
    buttons[5]=(ImageButton)menu.findViewById(R.id.button5);

    buttons[1].setBackgroundResource(R.drawable.screen);
    buttons[2].setBackgroundResource(R.drawable.sketch);
    buttons[3].setBackgroundResource(R.drawable.importdraw);
    buttons[4].setBackgroundResource(R.drawable.setting);
    buttons[5].setBackgroundResource(R.drawable.out);
    center.setBackgroundResource(R.drawable.noticeicon_using);

    /*center.setOnClickListener(new ImageButton.OnClickListener(){
        @Override
        public void onClick(View v) {
            if(click)
            {
                closemenu();
                //mode=false;
            }
            else
            {
                openmenu();
                //mode=true;
            }
            click=!click;
        }
    });*/
    center.setOnTouchListener(new View.OnTouchListener() {
        boolean isMove=false;
        int touchy,touchx,y;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action=motionEvent.getAction();
            if(action==MotionEvent.ACTION_DOWN)
            {
                y=params.y;
                //Log.i("y:",String.valueOf(y));
                touchy=(int)motionEvent.getRawY();
                touchx=(int)motionEvent.getRawX();
            }
            else if(action==MotionEvent.ACTION_MOVE)
            {
                if(Math.abs(motionEvent.getRawX()-touchx)<30)
                {
                    isMove=true;
                    params.gravity=Gravity.RIGHT;
                    params.y=y+(int)motionEvent.getRawY()-touchy;
                    wm.updateViewLayout(center,params);
                    wm.updateViewLayout(menu,params);
                }
            }
            else if(action==MotionEvent.ACTION_UP)
            {
                if(!isMove)
                {
                    if(click)
                    {
                        closemenu();
                        //mode=false;
                    }
                    else
                    {
                        openmenu();
                        //mode=true;
                    }
                    click=!click;
                }
                else
                    isMove=false;

            }
            return true;
        }
    });

    buttons[1].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[1].setBackgroundResource(R.drawable.screen_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[1].setBackgroundResource(R.drawable.screen);
                pmaControllerManager.newTask("screen");
            }
            return true;
        }
    });
    buttons[2].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[2].setBackgroundResource(R.drawable.sketch_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[2].setBackgroundResource(R.drawable.sketch);
                pmaControllerManager.newTask("sketch");
            }
            return true;
        }
    });
    buttons[3].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[3].setBackgroundResource(R.drawable.importdraw_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[3].setBackgroundResource(R.drawable.importdraw);
            }
            return true;
        }
    });
    buttons[4].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[4].setBackgroundResource(R.drawable.setting_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[4].setBackgroundResource(R.drawable.setting);
                pmaControllerManager.Setting();
            }
            return true;
        }
    });
    buttons[5].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[5].setBackgroundResource(R.drawable.exit_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[5].setBackgroundResource(R.drawable.out);
                shoaDialog(menuwindow.this);
                pmaControllerManager.OnExit();
            }
            return true;
        }
    });
}

共 (2) 个答案

  1. # 1 楼答案

    MainActivity.thisgetApplicationContext()替换this

    android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(MainActivity.this)
    

    如果您正在使用dialog,请查看以下内容:

    活动:ActivityName.this

    android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(MainActivity.this)
    

    对于片段:getActivity()

     android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(getActivity())
    

    对于适配器:Context context;

    android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(context)
    

    还有,看看这个Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

  2. # 2 楼答案

    this替换为MainActivity.this,如下所示:

    android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(MainActivity.this)