我正在爲Android應用程序設置屏幕。Android工具欄後退按鈕不被識別
設置屏幕包含2個「子屏幕」,它們都有一個工具欄和一個後退按鈕。
在屏幕上1後退按鈕不執行任何操作 屏幕上的1個作品完美地後退按鈕並返回我的主要設置屏幕(「硬」後退按鈕同時適用於屏幕)
屏幕1(首屏):
屏幕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(掌上十字參數屏幕):
XML的屏幕2:
<?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 AdParam的是獲得從一個XML文件中的各種參數)
我曾嘗試加入以下到我的代碼,但還是後退按鈕不執行任何功能: -
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
我很努力理解的是爲什麼我的應用程序中每個屏幕上的後退按鈕都完美無缺,除此之外。
我猜這將是明顯的東西,但我不知道是什麼。
你試過從RelativeLayout中移除android:clickable =「false」嗎?(你可以刪除android:shrinkColumns和android:stretchColumns,因爲是TableLayout屬性) – MatPag
@MatPag'android:clickable =「false」'只在屏幕2上顯示。屏幕2工作正常。屏幕1不起作用。 – Nik