在内置后退按钮和注销按钮中实现注销消息

时间:2013-12-14 17:35:48

标签: android

这是我的MainMenu.java

public class MainMenu extends Activity{

    Button userinfo,requestservice,makepayment,trackparcel,checkcard, logout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        // Show the Up button in the action bar.
        setupActionBar();
        userinfo = (Button) findViewById(R.id.userinfo);
        requestservice = (Button) findViewById(R.id.requestservice);
        makepayment = (Button) findViewById(R.id.makepayment);
        trackparcel = (Button) findViewById(R.id.trackparcel);
        checkcard = (Button) findViewById(R.id.checkcard);
        logout = (Button) findViewById(R.id.logout);

        userinfo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, UserInfo.class);              
                startActivity(intent);
            }
        });
        requestservice.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, RequestService.class);
                startActivity(intent);

            }
        });
        makepayment.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, Payment.class);
                startActivity(intent);

            }
        });
        trackparcel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, TrackParcel.class);
                startActivity(intent);
            }
        });
        checkcard.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, CheckCard.class);
                startActivity(intent);
            }
        });
        logout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainMenu.this, Login.class);
                SharedPreferences userDetails =getSharedPreferences("userdetails", MODE_PRIVATE);
                Editor edit = userDetails.edit();
                edit.clear();
                edit.putString("email", "");
                edit.putString("password", "");
                edit.commit();
                startActivity(intent);
            }
        });
    }

    public void dispachBackKey() {
        dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
        dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

我想触发内置后退按钮,我最终找到了这段代码

public void dispachBackKey() {
    dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
    dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}

您可能会注意到我也有退出按钮,我想要做的是如何显示一条小消息,要求用户确认退出确认退出按钮已构建 - 后退按钮。我应该如何实现呢?

2 个答案:

答案 0 :(得分:3)

对于后退按钮,您还可以使用public void onBackPressed()。然后,您只需在每次按下按钮后显示AlertDialog。这是代码的样子:

new AlertDialog.Builder(this)
    .setTitle("Logout")
    .setMessage("Would you like to logout?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // logout
        }
     })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // user doesn't want to logout
        }
     })
     .show();

这将放置在Logout按钮的onClickListener,以及onBackPressed(),或者您想要检测后退按钮单击。以下是onBackPressed()

的示例
public void onBackPressed() {
    //put the AlertDialog code here
}

答案 1 :(得分:0)

使用onBackPressed()了解用户何时点击后退按钮,然后显示确认注销的对话框,并在点击ok button时退出:

警报对话框代码:

new AlertDialog.Builder(this)
    .setTitle("Logout From Appname")
    .setMessage("Would you like to logout?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // add your logout code here
        }
     })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // dismiss dialog here because user doesn't want to logout
        }
     })
     .show();

然后在后退按钮上显示这个Dailog:

onBack Pressed:

public void onBackPressed() {
    //use the AlertDialog code here for user confirmation
}

我帮助你了!