2016-07-06 25 views
1

我曾經使用註銷按鈕來註銷某個活動時按下的按鈕,但現在我願意將它放在操作欄菜單中。我不知道該怎麼做。如何將註銷放在操作欄菜單中

我想做什麼:如果用戶按下正確的操作欄菜單上的註銷,他應該註銷應用程序。

這是我經常使用會對按下按鈕退出的代碼(考慮到活動頁面只有註銷按鈕):

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> 

回答

2

在你menu.xml文件的文件,我想你有這樣的事情:現在

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<item 
    android:id="@+id/rate_app" 
    android:title="@string/rate_app" 
    android:orderInCategory="100" 
    app:showAsAction="showIfRoom" /> 

<item 
    android:id="@+id/action_logout" 
    android:title="@string/logout" 
    android:orderInCategory="100" 
    app:showAsAction="showIfRoom" /> 

,在`onOptionsItemSelected(菜單項菜單),切換就像你已經這樣做:

@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.action_logout: 
    logoutUser(); 
    return true; 
    default: 
    return super.onOptionsItemSelected(item); 
    } 
} 

這應該可以幫助你實現你所需要的。讓我知道事情的後續!

+0

這是應該如何做的。 – DeaMon1

+0

如果你使用這個答案得到它,請接受它,所以它會被標記爲已回答!我很高興它爲你工作! – Eenvincible

+0

你保持這麼簡單。大。加工。接受了答案。謝謝。 –

0

有一個menu.xml文件文件與操作欄按鈕。您可以替換註銷按鈕的任何按鈕,並且開關盒控制單擊哪個按鈕。 在正確的情況下,添加註銷功能的代碼。

+0

你的意思是說做同樣的方式,我使用按鈕?我是android新手。你可以在上面的代碼中展示如何做。我會很感激。 –

+0

@ Eenvincible的答案與您應該使用的代碼相當完整。 – filipebarretto

+0

是的,我得到它的工作。謝謝你幫助我。 –