2014-03-31 178 views
3

我开发了一款可成功运行的应用程序,但有两项活动,第一项是启动屏幕,第二项是webview。现在的问题是,当后退按钮被按下时,它会关闭应用程序并退出,但是后退,前进和刷新按钮在应用程序中用于内部导航。所以如何显示你确定要退出对话框或防止用户停用后退键。如何防止用户在按下后退按钮时退出应用程序

回答

0

覆盖在你的活动

@Override 
    public void onBackPressed() { 

     super.onBackPressed(); 

       AlertDialog.Builder alertDialog = new AlertDialog.Builder(SettingsActivity.this); 

       // Setting Dialog Title 
       alertDialog.setTitle("Signout your app"); 


       alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int which) { 

           dialog.cancel(); 
        } 
       }); 


       alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 



        } 
       }); 


       alertDialog.show(); 



    } 
0

onBackPressed通过覆盖ActivityonBackPressed()方法试试这个办法

public class YourActivity extends Activity { 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 

    @Override 
    public void onBackPressed() { 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 

     int imageResource = android.R.drawable.ic_dialog_alert; 
     Drawable image = getResources().getDrawable(imageResource); 

     builder.setTitle("Exit").setMessage("want to exit?").setIcon(image).setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       finish(); 
      } 
     }).setNegativeButton("no", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int id) { 
      } 
     }); 

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

    } 

} 
9

事情是这样的:

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


} 
0

只要做,

@Override 
public void onBackPressed() { 

    // super.onBackPressed(); 

      AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); 

      // Setting Dialog Title 
      alertDialog.setTitle("Exit alert"); 
      alertDialog.setMessage("Do You want Exit??"); 
      alertDialog .setIcon(R.drawable.galleryalart); 


      alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int which) { 

          dialog.cancel(); 
       } 
      }); 


      alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 

      [youractivity].super.onBackPressed(); 


       } 
      }); 


      alertDialog.show(); 



} 
+0

谢谢大家回答。 –

相关问题