- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 SettingsActivity
,它扩展了 PreferenceActivity
以显示我的应用中的设置。
有一个后退箭头,但它不起作用。
这是 SettingsActivity.java
文件的代码:
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener{
private AppCompatDelegate mDelegate;
public static final String RESET_PASSWORD_KEY = "reset_password";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences sharedPreferencesCompat = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Preference myPref = (Preference) findPreference(RESET_PASSWORD_KEY);
myPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
//open browser or intent here
Intent resetPasswordActivityIntent = new Intent(SettingsActivity.this, ResetPasswordActivity.class);
startActivity(resetPasswordActivityIntent);
return true;
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
public ActionBar getSupportActionBar() {
return getDelegate().getSupportActionBar();
}
public void setSupportActionBar(@Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
getDelegate().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public MenuInflater getMenuInflater() {
return getDelegate().getMenuInflater();
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
@Override
public void setContentView(View view) {
getDelegate().setContentView(view);
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().setContentView(view, params);
}
@Override
public void addContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().addContentView(view, params);
}
@Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
@Override
protected void onTitleChanged(CharSequence title, int color) {
super.onTitleChanged(title, color);
getDelegate().setTitle(title);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getDelegate().onConfigurationChanged(newConfig);
}
@Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
public void invalidateOptionsMenu() {
getDelegate().invalidateOptionsMenu();
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(RESET_PASSWORD_KEY)) {
}
}
}
这是 preferences.xml
文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="xxx"
android:summary="xxxxxx"
android:key="reset_password" />
</PreferenceScreen>
这是在 AndroidManifest.xml
中定义“SettingsActivity”的方式:
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity">
</activity>
我怎样才能使那个箭头起作用?
请告诉我。
抱歉,如果问题格式不合适。我还是个初学者。
最佳答案
您需要在 onCreate()
中调用您的私有(private)方法 setSupportActionBar()
。
此外,您可能想要实现 onOptionsItemSelected
:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
关于android - setDisplayHomeAsUpEnabled() 在 PreferenceActivity 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36433299/
我的应用程序每次崩溃,错误为 java.lang.RuntimeException: Unable to start Activity ComponentInfo{sidssol.com.bit_ta
在使用 AppCompatActivity 时是否可以指定 getSupportActionBar().setDisplayHomeAsUpEnabled(true) 使用样式,而不是以编程方式这样做
我想在我的 Activity 中显示向上按钮,该功能运行良好,但无法显示左插入符号。相反,它显示了一个丑陋的后退箭头。我在我的 Activity 中这样做 - public class SecondA
我们可以在 Activity 上使用 setDisplayHomeAsUpEnabled() 方法在操作栏上显示“向上”箭头。 如果我没理解错的话,只有当我们不是根 Activity 时,我们才真正应
我的应用程序使用 android.support.v7.app.ActionBar 以便我可以在我的 Android 2.3.4 手机和使用 Android 4.4.2 的平板电脑上使用 Action
当我点击主页图标时,抽屉打开,然后立即将我带回之前的 Activity 。我相信 setDisplayHomeAsUpEnabled(true) 会干扰保持打开状态的操作栏抽屉。 代码中是否有任何内容
在我扩展 SherlockFragmentActivity 的 Activity 中,我有操作栏并且还设置了 actionBar.setDisplayHomeAsUpEnabled(true) 以返回
根据 Google's document , getActionBar().setDisplayHomeAsUpEnabled(true) 需要显示向上按钮。我使用 Eclipse 中的向导创建了一个
我使用 setDisplayHomeAsUpEnabled 来显示箭头而不是抽屉“汉堡”图标,但它没有动画或任何东西。相反,它会立即显示可绘制的箭头。 主屏幕:(专辑 1) 点击电影时:(专辑 2)
首先,我知道这个问题之前已经被问过,我花了一个小时查看其他解决方案,大部分来自 - getActionBar() returns null ,但没有一个有效,因此另一双眼睛也许能够做一些事情。 其他类
这个问题已经有答案了: The getActionBar method returns null [duplicate] (1 个回答) 已关闭 8 年前。 应用程序不断崩溃。它说在actionBar
我正在使用 Android Studios,但是,我无法添加以下两种方法,因为它说它无法解析它们。 getSupportActionBar().setDisplayHomeAsUpEnabled(t
当我按下工具栏中的“后退/向上”按钮上升一级时,父 Activity 正在重新创建自身(重新加载数据等),而我只想完成 当前 Activity 。 现在我的 Activity 正在自动创建 Toolb
我是编码新手..练习一些教程..我已经研究过其他有类似问题的问题..但我无法解决它... 来自 mainActivity 的代码.. package example.org.myfragment; i
package com.maya.helloandroid; import java.util.ArrayList; import android.app.Activity; import andro
当我单击它按预期工作的图标时, list 中的所有内容都已正确设置,但是 ActionBar 上未显示 <箭头。有什么想法吗? actionBar.setDisplayHomeAsUpEnabled(
我调用了 setDisplayHomeAsUpEnabled(true) 并显示了箭头,但它没有指向任何地方,我还想手动设置按钮的链接/目标以转到自定义位置。 如何指定自定义位置?谢谢。 谢谢 最佳答
我有一个 SettingsActivity,它扩展了 PreferenceActivity 以显示我的应用中的设置。 有一个后退箭头,但它不起作用。 这是 SettingsActivity.java
我想在我的应用程序中的所有 Activity 的操作栏中添加应用程序图标,点击图标,我想导航到我的应用程序的主页。 我尝试在 onCreate 中使用以下代码 ActionBar actionBar
我有一个基于 fragment 的布局,其中包含两个 ListFragment(A 和 B),它们都包含在一个 Activity (称为 ListingActivity 1)中。当应用程序启动时,将调
我是一名优秀的程序员,十分优秀!