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

时间:2013-12-18 09:38:31

标签: 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"));     
    }   
}

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

@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();
    }

现在,我真的不知道在哪里称这种方法。有人可以提出任何想法吗?

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

5 个答案:

答案 0 :(得分:3)

尝试以下代码:

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();

}

答案 1 :(得分:1)

使用此:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Alert Message:");
        builder.setMessage("Do you want to save this image to your file??");
        builder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                                           // do your stuff

                        }
                });

        builder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                        finish();
                    }
                });

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

        return true;
        }

      return super.onKeyDown(keyCode, event);
    }

答案 2 :(得分:0)

您可以将此代码修改为acc。要求。

 dialog.setOnKeyListener(new Dialog.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface arg0, int keyCode,
                        KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        finish();
                        dialog.dismiss();
                    }
                    return true;
                }
            });

答案 3 :(得分:0)

请勿在{{1​​}}

中致电super.onBackPressed();

答案 4 :(得分:0)

另外,你可以简单地在activity_main.xml(tab.xml)中添加onBackPressed()

机器人:的onClick = “onBackPressed” 在你的按钮属性中提到它