如何在按下后退按钮时创建警报对话框?

use*_*300 0 java android android-dialog

我想在按下后退按钮时向用户显示一个警报对话框。例如:您确定要退出吗?我在创建警报对话框时没有遇到问题。我在在哪里调用此对话框方法()。

这是我的 HomeScreen.java

public class HomeScreen extends TabActivity {
    ConferenceOpenHelper helper_ob;
    Cursor cursorHome;
    ProfileEditScreen profileEditScreen;
    ConferenceAdapter adapter;
    EcMeeting meeting;
    final Context context = this;
    public static int HOMETEMP = 1;// Constant for Deleting the last created id
// in edit profile screen (Cancel)
    public static String HOME = "home";// constant for updating status column
    public static String CUSTOM_DIALER = "custom";
    public static String TEMPLATE = "template";// constant for updating status              // column   
    public static TabHost tabHost;
    public static final int QUICK_START = 1;// Constant for menu options
    public static final int TEMPLATE_SCREEN = 2;// Constant for menu options
    public static final int USER_GUIDE = 3;// Constant for menu options
    public static final int QUICK_CALL = 4;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.tab);        
        Resources ressources = getResources();      
         tabHost = getTabHost();        
        // Android tab
         Intent photo = new Intent().setClass(this,EcMeeting.class);
            TabSpec tabSpecphoto = tabHost
              .newTabSpec("Calendar")
              .setIndicator("Calendar", ressources.getDrawable(R.drawable.calendartab))
              .setContent(photo);
        // Apple tab
        Intent intentApple = new Intent().setClass(this,CallListScreen.class);
        TabSpec tabSpecApple = tabHost
          .newTabSpec("Calls")
          .setIndicator("Calls", ressources.getDrawable(R.drawable.ic_action_device_access_call))
          .setContent(intentApple);

        // Windows tab
        Intent intentWindows = new Intent().setClass(this,UserSettingActivity.class);
        TabSpec tabSpecWindows = tabHost
          .newTabSpec("Settings")
          .setIndicator("Settings", ressources.getDrawable(R.drawable.ic_action_setting))
          .setContent(intentWindows); 

        // add all tabs 
        tabHost.addTab(tabSpecphoto);
        tabHost.addTab(tabSpecApple);
        tabHost.addTab(tabSpecWindows);

        //set Windows tab as default (zero based)
        /*tabHost.setCurrentTab(0);*/

         for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            {
                tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#d3d3d3"));
            }
            tabHost.getTabWidget().setCurrentTab(0);
            /*tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#C35817"));*/
         }
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, QUICK_START, 0, "Quick Start").setIcon(R.drawable.ic_menu_add_icon);        
        menu.add(0, USER_GUIDE, 0, "User Guide").setIcon(R.drawable.ic_menu_guide_icon);

        return true;
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
               .setMessage("Are you sure you want to exit?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        HomeScreen.this.finish();
                   }
               })
               .setNegativeButton("No", null)
               .show();
    }







    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
        case QUICK_START:
            startActivity(new Intent(this, ConfDialerScreen.class));
            break;

        case USER_GUIDE:
            startActivity(new Intent(this, UserGuideScreen.class));
            break;
        }
        return true;
    }
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
            for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            {
                tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#8A4117"));
            }

            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#C35817"));     
    }   
}
Run Code Online (Sandbox Code Playgroud)

这是创建警报对话框的方法()

@Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
               .setMessage("Are you sure you want to exit?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        HomeScreen.this.finish();
                   }
               })
               .setNegativeButton("No", null)
               .show();
    }
Run Code Online (Sandbox Code Playgroud)

现在,我真的不知道在哪里调用这个方法。有人可以给出任何想法吗?

当用户按下后退按钮时,我想显示警报对话框。

San*_*ket 5

尝试下面的代码:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        dialogOnBackPress();

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

protected void dialogOnBackPress() {

    new AlertDialog.Builder(this)
           .setMessage("Are you sure you want to exit?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    HomeScreen.this.finish();
               }
           })
           .setNegativeButton("No", null)
           .show();

}
Run Code Online (Sandbox Code Playgroud)