这是我尝试进行偏好设置活动。它已被黑了,但没有弃用的方法。它源于一个没有使用偏好的黑客。该hack 正常工作 ,直到应用程序离开当前在内存中的应用列表,此时值将重置,就像下次使用时firstUse
一样。
添加偏好设置代码(在dealWithPreferences
和onOptionsItemSelected
中)对此没有任何改变。只要app留在内存中,就可以正常工作。一旦它离开,我们就会回到firstUse
。
添加的代码来自修改教科书示例,这绝对不是现代的#34;处理偏好的方式。很短。它来自Sams Teach Yourself Android App Development,24小时内完成。我想我没有接受挑战。或者文本中的代码可能是错误的。或两者兼而有之。
我只想永久保存两个首选项。到目前为止,没有去。
这里是94行SSCCE,后面跟着xml。我真的砍掉了大量的代码,只留下最简单的骨头来查看问题。 (顺便说一句,我也试过extends PreferenceActivity
;没有改变。)
package com.whatever.Prefer;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.view.Window;
import java.lang.reflect.Field;
public class MyActivity extends Activity{
boolean debugging = false, firstUse, screenSaver, focusAtClue;
public final String
prefix = "com.whatever.prefer",
SETTINGS = prefix + ".settings",
FIRST_USE = prefix + ".firstUse",
FOCUS_AT_CLUE = prefix + ".focusAtClue",
SCREENSAVER = prefix + ".screensaver",
literally_Focus_At_Clue = "Focus at clue",
literally_Screen_saver = "Screen saver";
SharedPreferences preferences;
SharedPreferences.Editor editor;
private void dealWithPreferences(){
preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);
editor = preferences.edit();
boolean firstUse = preferences.getBoolean(FIRST_USE, true);
if (firstUse) {
editor.putBoolean(FIRST_USE, false);
focusAtClue = true;
screenSaver = true;
editor.putBoolean(FOCUS_AT_CLUE, focusAtClue);
editor.putBoolean(SCREENSAVER, screenSaver);
}
else {
editor.putBoolean(FIRST_USE, false);
editor.putBoolean(FOCUS_AT_CLUE, focusAtClue);
editor.putBoolean(SCREENSAVER, screenSaver);
}
editor.commit();
}
@Override protected void onCreate(Bundle savedInstanceState) {
dealWithPreferences();
super.onCreate(savedInstanceState);
getWindow().setFormat(Window.FEATURE_ACTION_BAR);
makeActionOverflowMenuShown();
} // onCreate
private void makeActionOverflowMenuShown(){ // a hack from SO
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
}
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_my, menu);
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);
// Toggle whichever item was tapped
switch (item.getItemId()) {
case R.id.itemScreensaver:
if (item.getTitle().toString().equals(literally_Screen_saver)) {
item.setTitle("No " + literally_Screen_saver);
screenSaver = false;
}
else {
item.setTitle(literally_Screen_saver);
screenSaver = true;
}
break;
case R.id.itemFocus:
if (item.getTitle().toString().equals(literally_Focus_At_Clue)) {
item.setTitle("No " + literally_Focus_At_Clue);
focusAtClue = false;
}
else {
item.setTitle(literally_Focus_At_Clue);
focusAtClue = true;
}
} // switch
dealWithPreferences();
return true;
}
}
此处menu_my.xml
。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyActivity">
<item android:id="@+id/itemHelp"
android:title="Tap below to toggle option..."
android:orderInCategory="10"
app:showAsAction="never"/>
<item android:id="@+id/itemFocus"
android:title="@string/focusAtClue"
android:orderInCategory="200"
app:showAsAction="never"/>
<item android:id="@+id/itemScreensaver"
android:title="@string/screenSaver"
android:orderInCategory="300"
app:showAsAction="never"/>
</menu>
AndroidManifest.xml
。
<?xml version="1.0" encoding="utf-8"
?>
<manifest
xmlns:android= "http://schemas.android.com/apk/res/android"
package= "com.whatever.Prefer"
>
<application
android:icon="@mipmap/ic_launcher"
android:allowBackup="true"
>
<activity
android:screenOrientation= "portrait"
android:label= "BS"
android:icon= "@mipmap/ic_launcher"
android:name= ".MyActivity"
>
<intent-filter>
<action
android:name= "android.intent.action.MAIN"
/>
<category
android:name= "android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
应该发生的是这个。当用户点击操作栏溢出设置图标时,弹出下面的菜单,用户可以切换&#34;选项&#34;,这将添加&#34;否&#34;选项的文本(如果没有)或带走&#34;否&#34; (如果有的话)。
它有效,但它并不存在。
我意识到这是非常黑的,但这只是我需要的。当然,这很容易解决(我希望)。
我该怎么办才能修复它?