کلیدستان

نسخه‌ی کامل: نحوه ساخت دکمه خروج و سوال هنگام خارج شدن از برنامه اندروید
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
سلام
من میخوام وقتی که کاربر روی دکمه خروج کلیک میکنه صفحه ای (alert) بیاد که آیا میخواهید خارج شوید یا خیر؟
و بعد از اینکه بلی رو زد خارج بشه.

من این کار رو انجام دادم اما فقط روی دکمه Back کار میکنه.
چطوری باید برای دکمه خروج این کارو انجام بدم؟
دکمه خروجمم onclick کردم و کد رو بین تگ نوشتم اما خطا میده.

باید چکار کرد که هم دکمه Back برای خروج باشه و هم دکمه خروج؟!
این کد رو امتحان کنید اگر مشکل داشتید کد خودتون رو بزارید

کد پی‌اچ‌پی:
Button btnd = (ButtonfindViewById(R.id.button4);
        
btnd.setOnClickListener(new View.OnClickListener() {
            
            @
Override
            
public void onClick(View arg0) {
                
// TODO Auto-generated method stub
                
onBackPressed();
            }
        });
        }
        @
Override
        
public void onBackPressed() {
            
//Display alert message when back button has been pressed
            
backButtonHandler();
            return;
        }
     
        public 
void backButtonHandler() {
            
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    
Menu1.this);
            
            
alertDialog.setTitle("درخواست خروج");
            
            
alertDialog.setMessage("آیا قصد خروج از برنامه را دارید؟");
           
            
            
alertDialog.setPositiveButton("بله",
                    new 
DialogInterface.OnClickListener() {
                        public 
void onClick(DialogInterface dialogint which) {
                            
finish();
                        }
                    });
            
            
alertDialog.setNegativeButton("خیر",
                    new 
DialogInterface.OnClickListener() {
                        public 
void onClick(DialogInterface dialogint which) {
                            
// Write your code here to invoke NO event
                            
dialog.cancel();
                        }
                    });
       
            
alertDialog.show();
        }
    } 
 
امتحان کردم.
کار نکرده.

کد خودم رو چطور باید بزنم؟
اینو تست کن

کد پی‌اچ‌پی:
AlertDialog.Builder alertDialog= new AlertDialog.Builder(
                    
Menu1.this);
            
            
alertDialog.setTitle("are U sure?");
            
            
alertDialog.setMessage("R U sure U want to EXIT?");
           
            
            
alertDialog.setPositiveButton("yes",
                    new 
DialogInterface.OnClickListener() {
                        public 
void onClick(DialogInterface dialogint which) {
                            
system.exit(1)
                        }
                    });
            
            
alertDialog.setNegativeButton("no",
                    new 
DialogInterface.OnClickListener() {
                        public 
void onClick(DialogInterface dialogint which) {
                            
// Write your code here to invoke NO event
                            
alertDialog.cancel();
                        }
                    });
       
            
alertDialog.show();
        } 
 الان ممشکلتونو حل می کنم
کد زیر برای دکمه خروج خود گوشی

کد پی‌اچ‌پی:
}

    @
Override
    
public void onBackPressed() {
        
backButtonHandler();
        return;
    }
    public 
void backButtonHandler (){


        
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            
BuyPremiumActivity.this);

        
// set title
        
alertDialogBuilder.setTitle("خروج");

        
// set dialog message
        
alertDialogBuilder
            
.setMessage("از خرید منصرف شدید؟")
            .
setCancelable(false)
            .
setPositiveButton("بله",
            new 
DialogInterface.OnClickListener() {
                public 
void onClick(DialogInterface dialog,
                                    
int id) {
                    
// if this button is clicked, close
                    // current activity
                    
System.exit(id);
                }
            })
            .
setNegativeButton("ادامه میدهم",
            new 
DialogInterface.OnClickListener() {
                public 
void onClick(DialogInterface dialog,
                                    
int id) {
                    
dialog.cancel();
                }
            });

        
// create alert dialog
        
AlertDialog alertDialog alertDialogBuilder.create();

        
// show it
        
        
alertDialog.show();
        
        }



کد زیر برای دکمه ای که در برنامه تان ساختید 

 
کد پی‌اچ‌پی:
    Button btnex=(ButtonfindViewById(R.id.button_exit);
        btnex.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick
(View arg0) {
        
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            
BuyPremiumActivity.this);

        
// set title
        
alertDialogBuilder.setTitle("خروج");

        
// set dialog message
        
alertDialogBuilder
            
.setMessage("از خرید منصرف شدید؟")
            .
setCancelable(false)
            .
setPositiveButton("بله",
            new 
DialogInterface.OnClickListener() {
                public 
void onClick(DialogInterface dialog,
                                    
int id) {
                    
// if this button is clicked, close
                    // current activity
                    
System.exit(id);
                }
            })
            .
setNegativeButton("ادامه میدهم",
            new 
DialogInterface.OnClickListener() {
                public 
void onClick(DialogInterface dialog,
                                    
int id) {
                    
dialog.cancel();
                }
            });

        
// create alert dialog
        
AlertDialog alertDialog alertDialogBuilder.create();

        
// show it
        
        
alertDialog.show();
        
        }



سلام
مشکل رو حل کردم.
فقط توی کدهای دکمه onBackPressed رو قرار دادم اونی که میخواستم انجام شد.