作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的 Android 应用程序中插入一个警报对话框。当我按照教程将其添加到一个简单的项目中时就可以了,该项目只有一个 MainActivity.java
、activity_main.xml
和一个 layout
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();
第一行的this中有一个警告符号。我该如何解决它?附注下面是我调用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;
}
});
}
最佳答案
传递Context
而不是this
,因为this
是对当前对象的引用。这意味着如果您从 lambda 调用它,this
将是从匿名类扩展的对象的引用。
所以你可以制作类似的方法
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);
您可以按照我上面的描述传递上下文,也可以从 View 中获取它:
buttons[5].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//some code here
showDialog(v.getContext());
return true;
}
});
关于java - 如何在android中的MainActivity之外创建一个对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48679942/
我是一名优秀的程序员,十分优秀!