我曾經使用註銷按鈕來註銷某個活動時按下的按鈕,但現在我願意將它放在操作欄菜單中。我不知道該怎麼做。如何將註銷放在操作欄菜單中
我想做什麼:如果用戶按下正確的操作欄菜單上的註銷,他應該註銷應用程序。
這是我經常使用會對按下按鈕退出的代碼(考慮到活動頁面只有註銷按鈕):
public class Activity_Main extends Activity {
private Button btnLogout;
private SQLiteHandler db;
private SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogout = (Button) findViewById(R.id.btnLogout);
// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
// Logout button click event
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logoutUser();
}
});
}
/**
* Logging out the user. Will set isLoggedIn flag to false in shared
* preferences Clears the user data from sqlite users table
* */
private void logoutUser() {
session.setLogin(false);
db.deleteUsers();
// Launching the login activity
Intent intent = new Intent(Activity_Main.this, Activity_Login.class);
startActivity(intent);
finish();
}
目前的操作欄菜單中有兩個項目更多的應用程序和速率的應用程序,其重定向到谷歌播放..我想用註銷動作替換更多的應用程序。
這是我想修改的內容:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.rate_app:
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
}
return true;
case R.id.more_app:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.more_apps))));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
這是menu.xml文件
<item
android:id="@+id/ic_menu"
android:icon="@drawable/ic_ab_overflow_compat"
android:showAsAction="always"
android:title="">
<menu>
<!-- Rate App -->
<item
android:id="@+id/rate_app"
android:showAsAction="ifRoom|withText"
android:title="@string/rate"/>
<!-- More App -->
<item
android:id="@+id/more_app"
android:showAsAction="ifRoom|withText"
android:title="@string/more"/>
</menu>
</item>
這是應該如何做的。 – DeaMon1
如果你使用這個答案得到它,請接受它,所以它會被標記爲已回答!我很高興它爲你工作! – Eenvincible
你保持這麼簡單。大。加工。接受了答案。謝謝。 –