2017-10-26 5 views
2

私は、メニューからバックイベントをキャッチする方法がわからない:メニューからバックイベントを取得するにはどうすればいいですか?

enter image description here

私が押したときにバックbutton、画面の左上にイベントをキャッチしたいわけ。ここで

メニューのxmlファイル:

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:theme="@style/ActionModeMenu"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <CheckBox 
      android:id="@+id/context_menu_select_tweet_checkbox" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="2dp" 
      android:onClick="onSelectAllTweet" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:theme="@style/ActionModeMenuSubtitle" 
      android:text="@string/menu_tweet_select_all_text"/> 

    </LinearLayout> 

    <TextView 
     android:id="@+id/context_menu_select_tweet_textview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:paddingLeft="10dp" 
     android:theme="@style/ActionModeMenuTitle"/> 

    </LinearLayout> 

ありがとう!

+1

[Androidを実装する方法ActionBar戻るボタン?](https://stackoverflow.com/questions/10108774/how-to-implement-the-android-actionbar-back-button) –

+0

いいえ、私のアクションバーはアイテムを長時間クリックしたときにのみ表示されます複数選択の場合。 – VinceMedi

答えて

4

使用android.R.id.home

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       onBackPressed(); 
       break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

あなたが戻って矢印の画像を変更したい場合は、あなたはOnCreate方法

final ActionBar actionBar = getSupportActionBar(); 
actionBar.setDisplayHomeAsUpEnabled(true); 
actionBar.setHomeAsUpIndicator(R.mipmap.back_to); // change the image 
1

でこれを書くことができます使用android.R.id.homeナビゲーションへ戻るボタン

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == android.R.id.home) { 
     //Navigation Back Pressed 
    } 
    return super.onOptionsItemSelected(item); 
} 
関連する問題