Java 如何在android中的MainActivity外部创建对话框

Java 如何在android中的MainActivity外部创建对话框,java,android,android-alertdialog,Java,Android,Android Alertdialog,我想在我的android应用程序中插入一个警报对话框。 当我按照教程将其添加到一个简单的项目中时,这很好,该项目只有一个MainActivity.java、activity_main.xml和一个布局xml 然而,当我想将代码插入到我的项目中时,我会遇到一些问题。 我的代码是: android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(this) .setTi

我想在我的android应用程序中插入一个警报对话框。 当我按照教程将其添加到一个简单的项目中时,这很好,该项目只有一个MainActivity.java、activity_main.xml和一个布局xml

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

android.app.AlertDialog alartDialog = new android.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;
        }
    });
}
将此替换为MainActivity。如下所示:

android.app.AlertDialog alartDialog=新建 android.app.AlertDialog.BuilderMainActivity.this

将其替换为MainActivity.this或getApplicationContext

如果您使用的是dialog,请查看以下内容:

对于活动:ActivityName.this

对于片段:getActivity

对于适配器:上下文

此外,请查看此传递上下文而不是此上下文,因为这是对当前对象的引用。这意味着,如果您从lambda调用它,这将是从匿名类扩展而来的对象引用

所以你可以像这样做

public void showDialog(Context context) {
    AlertDialog alartDialog = AlertDialog.Builder(context).
           ...
           .show();
}
然后像这样称呼它

showDialog(MainActivity.this); //for activity
showDialog(MainFragment.this.getActivity()); //for fragment
顺便说一句,最好为它创建类似于Util类的东西:

class DialogUtil {
   public static void showDialog(Context context) {
             AlertDialog alartDialog = AlertDialog.Builder(context).
           ...
           .show();
   }
}
那就这样说吧

DialogUtil.showDialog(MainActivity.this); //for activity
DialogUtil.showDialog(MainFragment.this.getActivity()); //for fragment
更新

而不是像这样称呼它:

shoaDialog(menuwindow.this);
您可以按照上面所述传递上下文,也可以从视图中获取上下文:

buttons[5].setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    //some code here
        showDialog(v.getContext());
        return true;
    }
});

AltertDialog需要一个上下文,您正在将其传递给活动。试用

new android.app.AlertDialog.Builder(this.getApplicationContext())

相反。

显示您的完整代码您将这些代码放在哪里?我是否应该为您粘贴所有类?还是只是功能?谢谢你的回答。所以,我创建了一个新方法,但无法在其他函数中调用showDialog。在其他函数中调用它是否正确?@Ron将其设置为静态。所以你可以在任何地方打电话给它。我会给你example@Ron就这样包起来。是的,调用其他函数是正确的。不幸的是,它仍然不能工作。当我使用静态或不使用静态的方法时,这是很好的。但是,我不能在其他函数中调用它。p、 我已经编辑了我的帖子并添加到我称之为的地方。谢谢你的回答。我试过了,但没用。谢谢你的回答。我试过了,但没用。
DialogUtil.showDialog(MainActivity.this); //for activity
DialogUtil.showDialog(MainFragment.this.getActivity()); //for fragment
shoaDialog(menuwindow.this);
buttons[5].setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    //some code here
        showDialog(v.getContext());
        return true;
    }
});
new android.app.AlertDialog.Builder(this.getApplicationContext())