onBackpressed()中的问题

时间:2016-07-11 11:07:32

标签: java android onbackpressed

实际上我的问题是,当我按下后退按钮时它显示dialog并同时应用程序完成而没有对键进行任何操作?任何人都可以解决这个问题吗?

@Override
public void onBackPressed() {
    super.onBackPressed();
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setMessage("Really want to exit");
    ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Main.super.onBackPressed();
        }
    });
    ab.create();
    ab.show();
}

4 个答案:

答案 0 :(得分:3)

从方法中移除super.onBackPressed();,仅将其保留在onClick()

@Override
public void onBackPressed() {
    super.onBackPressed(); //remove it

}

您似乎在两个按钮中执行相同操作,应从Main.super.onBackPressed()

中删除negativeButton
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        super.onBackPressed();
    }
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        ab.dismiss();
    }
});

答案 1 :(得分:1)

请勿调用@Override public void onBackPressed() { AlertDialog.Builder ab = new AlertDialog.Builder(this); ab.setMessage("Really want to exit"); ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); ab.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); ab.create(); ab.show(); } 的超级方法。

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rs: <http://www.SemanticRecommender.com/rs#>
PREFIX mo: <http://www.musicontology.com/mo#>
PREFIX  : <http://www.musicsemanticontology.com/mso#>

mo:5th_symphony_for_beethoven a mo:GermanSymphony .
mo:nabucco_overture a mo:OperaOverture .
rs:operaWeek2016 a rs:TemporalContext ; rs:appliedOnItems  mo:OperaOverture .
rs:SymphonyFestival2016 a rs:TemporalContext ; rs:appliedOnItems mo:GermanSymphony .

答案 2 :(得分:0)

try this
  @Override
    public void onBackPressed() {

        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setMessage("Really want to exit");
        ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               finish();
            }
        });
        ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Main.super.onBackPressed();
            }
        });
        ab.create();
        ab.show();
    }

答案 3 :(得分:0)

您需要从super.onBackPressed();删除onBackPressed()。然后你的功能应该是,

@Override
public void onBackPressed() {

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();
    }
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Main.super.onBackPressed();
    }
});
ab.create();
ab.show();
}