外部类中的Android对话框处理程序

外部类中的Android对话框处理程序,android,android-alertdialog,Android,Android Alertdialog,晚上好。我是Android开发的初学者。我不想弄脏我的主要活动类,这就是为什么我想使用一些外部类(我可以给一些外部类指定“this”吗?)。我的问题可能很简单。我想显示AlertDialog。 所以我的班级 public class Dialogs { public static void exitActivity(Context c ){ AlertDialog.Builder builder = new AlertDialog.Builder(c); //Construc

晚上好。我是Android开发的初学者。我不想弄脏我的主要活动类,这就是为什么我想使用一些外部类(我可以给一些外部类指定“this”吗?)。我的问题可能很简单。我想显示AlertDialog。 所以我的班级

public class Dialogs {

public static void exitActivity(Context c ){
    AlertDialog.Builder builder = new AlertDialog.Builder(c);

    //Construct dialog
    builder.setMessage("Are your sure you want to exit?");
    builder.setCancelable(false);

    //Listener for yes button
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            FifteenActivity.c.finish(); ////<<< Here is an error >>>>//
        }
    });

    //Listener for no button
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    AlertDialog alert = builder.create();
    alert.show();
}

}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.newGame:
        restart();
        return true;
    case R.id.settings:

        return true;
    case R.id.info:
        return true;
    case R.id.help:
        return true;
    case R.id.exit:
        Dialogs.exitActivity(this);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
公共类对话框{
公共静态无效存在性(上下文c){
AlertDialog.Builder=新建AlertDialog.Builder(c);
//构造对话框
setMessage(“您确定要退出吗?”);
builder.setCancelable(false);
//“是”按钮的侦听器
builder.setPositiveButton(“是”,新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
fifteneActivity.c.finish();/>//
}
});
//无按钮侦听器
setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.newGame:
重启();
返回true;
案例R.id设置:
返回true;
案例R.id.info:
返回true;
案例R.id.help:
返回true;
案例R.id.exit:
对话。存在性(本);
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}

在外部类中创建所有菜单是一种“良好做法”吗?

在第三个类中分离对话框代码不是一个坏主意。特别是在整个应用程序中显示相同的对话框时

传递上下文时,请确保传递的是活动上下文而不是应用程序上下文


您将能够使用相同的上下文来完成您的活动

我的理解是,您希望通过传递上下文,然后调用finish来使用单独的类完成活动。上述方法应该足够了

public static void exitActivity(Context activityContext){
   ...
   (Activity)activityContext.finish();
   ...
}

是的,正如在另一篇文章中提到的,你应该确保你传递了正确的上下文。上下文可能与视图或线程等相关,因此请确保在活动上下文中传递。

您不必担心传递“this”,这是在类之间传递上下文的典型方式。最好不要保留上下文,只在调用的范围内使用它(就像您在exitActivity的这个小示例中所做的那样)。onoptionItemSelected()通常作为活动的一部分保留,并像您设置的那样为对话框类使用类/静态调用。这将代码分隔开一点,使内容更具可读性。感谢您的回复,以及我如何从外部类完成活动?请参阅我的代码,如果您可以在崩溃发生时从程序的LogCat打印输出中提供错误消息,这将非常有用。这通常会得到最好的响应,所以我们知道它是NullPointerException还是不能强制转换到类,或者类似的东西。谢谢,它可以工作。因此,我们必须将我们的环境转换为活动<代码>代码((活动)activityContext.finish()<代码>代码基类“活动”具有完成功能。当然,将它转换到您自己的特定类,扩展“活动”类也会起作用(MyActivity)activityContext.finish();'或者使用类本身“myActivityClass.finish();”