我正在设置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>
屏幕1的Java:
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的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>
屏幕2的Java: -
@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();
}
(loadParams是一个从xml文件中获取各种参数的函数)
我尝试将以下内容添加到我的代码中,但后退按钮仍无效: -
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
我正在努力理解为什么我的应用程序中每个屏幕上的后退按钮都能完美地工作,除了这个。
我猜它会显而易见,但我不知道是什么。
答案 0 :(得分:1)
您可以查看以下内容
1)您的活动延长AppCompatActivity
2)
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
3)覆盖以下方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:1)
在manifest.xml中添加此行android:parentActivityName=".(Name of the activity you want to return)"
就像这样
<activity
android:name=".(CurrentActivity)"
android:parentActivityName=".(Activity to be returned)"/>
答案 2 :(得分:1)
问题似乎与您的xml文件有关。您已将父布局指定为可点击的false,因此无法获得您的点击通话。尝试删除它。