2017-01-10 12 views
0

私はアンドロイドのアプレイケーションの設定画面で作業しています。Androidツールバーの戻るボタンが認識されない

設定画面には2つの 'サブ画面'があり、両方ともツールバーと戻るボタンがあります。

スクリーン1上の戻るボタンは1つの作品完全に画面上の[戻る]ボタンとは、(両方の画面の「ハード」のバックボタンの作品)

スクリーン1(環境設定画面のメイン設定画面に私を返す 何もしません):

Screen 1 (Preferences Screen)

画面1つのxml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_preferences" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.clear.pocketcross.Preferences"> 

    <Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?android:attr/colorPrimary" 
     android:popupTheme="@android:style/ThemeOverlay.Material.Light" 
     android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/toolbar" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 

     <Switch 
      android:id="@+id/counterscroll" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:defaultValue="true" 
      android:key="counter_scroll" 
      android:shadowColor="@color/gradient_mid" 
      android:shadowDx="5" 
      android:shadowDy="5" 
      android:text="@string/scrollCounters" 
      android:thumb="@drawable/pocketcross_btn_radio_on_holo_light" /> 

     <Switch 
      android:id="@+id/navswipe" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:defaultValue="true" 
      android:key="nav_swipe" 
      android:shadowColor="@color/gradient_mid" 
      android:shadowDx="5" 
      android:shadowDy="5" 
      android:text="@string/swipeTitle" 
      android:thumb="@drawable/pocketcross_btn_radio_on_holo_light" /> 
    </LinearLayout> 

</RelativeLayout> 

Javaのスクリーン1について:

Switch nav; 
Switch scroll; 
SharedPreferences shared; 
public static final String MyPREFERENCES = "PocketCross"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_preferences); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    //Toolbar will now take on default Action Bar characteristics 
    setActionBar(toolbar); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    this.setTitle("Preferences"); 

    nav = (Switch) findViewById(R.id.navswipe); 
    scroll = (Switch) findViewById(R.id.counterscroll); 

    if (shared == null) { 
     shared = this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
    } 
    nav.setChecked(shared.getBoolean("nav_swipe", true)); 
    scroll.setChecked(shared.getBoolean("counter_scroll", true)); 

    nav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      SharedPreferences.Editor spEdit = shared.edit(); 
      spEdit.putBoolean("nav_swipe", isChecked); 
      spEdit.apply(); 
     } 
    }); 

    scroll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      SharedPreferences.Editor spEdit = shared.edit(); 
      spEdit.putBoolean("counter_scroll", isChecked); 
      spEdit.apply(); 
     } 
    }); 
} 

画面2(ポケットクロスパラメータ画面):画面2用

Screen 2 (Pocket Cross Parameters Screen)

XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parametersTable" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:shrinkColumns="*" 
    android:stretchColumns="*" 
    android:background="@color/background" 
    android:clickable="false"> 

    <Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?android:attr/colorPrimary" 
     android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar" 
     android:popupTheme="@android:style/ThemeOverlay.Material.Light" /> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/parametersList" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="@dimen/activity_vertical_margin" 
     android:layout_marginBottom="@dimen/activity_vertical_margin" 
     android:layout_marginLeft="@dimen/activity_horizontal_margin" 
     android:layout_marginRight="@dimen/activity_horizontal_margin" 
     android:layout_below="@+id/toolbar" /> 

</RelativeLayout> 

のJava画面2: -

 @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_paramaters); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     //Toolbar will now take on default Action Bar characteristics 
     setActionBar(toolbar); 
     getActionBar().setDisplayHomeAsUpEnabled(true); 

     this.setTitle(R.string.parametersTitle); 

     mntFile = MainMenu.mntFile; 

     fileData = xmlTools.getXmlFile(mntFile); 
     tags = fileData.xmlTags; 
     for (int i = 0; i < tags.size(); i++) { 
      tag = tags.get(i); 
      if (tag.nodeTag.equalsIgnoreCase("PocketCrossSysParam")) { 
       nodes = tag.nodeData; 
       paramSetting = xmlTools.getNodeFromList(nodes, "ParamName"); 
       paramValue = xmlTools.getNodeFromList(nodes, "ParamValue"); 
       parameter = new PocketCrossParameter(paramSetting,paramValue); 
       parameters.add(parameter); 
      } 
     } 
     loadParams(); 
    } 

(lo adParams私は自分のコードに次の行を追加しようとしているが、それでもバックボタンは何もしません

)xmlファイルから様々なパラメータを取得するための機能である: -

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

    return super.onOptionsItemSelected(item); 
} 

私が理解するのに苦労しています何されますなぜ私のアプリのすべての単一の画面上の戻るボタンは、この1つを除いて完全に動作します。

私はそれが何か明白になると思っていますが、私は何が分かりません。

+0

RelativeLayoutからandroid:clickable = "false"を削除しようとしましたか?(そして、あなたはアンドロイドを削除することができます:shrinkColumnsとandroid:stretchColumnsもTableLayoutプロパティなので) – MatPag

+0

@MatPag 'android:clickable = "false"'は画面2にしかありません。画面1は動作しません。 – Nik

答えて

0

を上書きライン、android:parentActivityName=".(Name of the activity you want to return)"

ちょうどこの

<activity 
      android:name=".(CurrentActivity)" 
      android:parentActivityName=".(Activity to be returned)"/> 
+0

これは良いコメントかもしれませんが、これは質問に答えません。ここでの質問は、なぜ戻り値ボタンが機能していないのか、別のアクティビティに戻る方法ではないかを尋ねることです。 – Mesabloo

+0

ありがとう。私はそんなにばかな気分だ。私はマニフェストをチェックすることは決して考えなかった! 2つの画面の違いは、画面2は親がマニフェストに設定され、画面1は設定されていませんでした。 – Nik

+0

私は助けてくれると嬉しいです:) –

1

あなたは、次の

1を確認することができます)あなたの活動は

Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(mToolbar); 
getSupportActionBar().setTitle("Title"); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

3)AppCompatActivity
2を拡張する)、これを追加のmanifest.xmlには、以下の方法

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId()== android.R.id.home) { 
      finish(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
+0

これは "com.android.support.appcompat-v7"を使用している場合にのみ機能します – Mesabloo

+0

この回答は良いかもしれませんが、もっと簡単なことがあります: 'getSupportActionBar()を使用してください。setNavigationOnClickListener(View.OnClickListener listener);' – Mesabloo

1

のような問題は、あなたのxmlファイルであると思われます。あなたの親レイアウトをクリック可能な偽と指定しているため、クリックコールが得られません。削除してください。

関連する問題