(安卓 2.2)
我正在尝试检索 PreferenceActivity 中设置的值。尽管活动本身正确存储/调用了该值,但我无法弄清楚如何自己检索设置值(例如,将其显示在 textView 中)。此代码没有给出错误,但是值 0 显示在 Spel.java 的 textView1 中,而不是 PrefsManager 首选项(在 NumberPickerPreferenceActivity 中)中的实际/当前值。
如何使正确的值显示在 textView1 那里?
另外:onSharedPreferenceChanged(在 NumberPickerPreferenceActivity 中)不显示 toast 消息(没有弹出,没有记录)。什么问题,如何解决?
这是 NumberPickerPreferenceActivity 类:
package mypackage.tabnavui;
import mypackage.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
public class NumberPickerPreferenceActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
private static final String TAG = NumberPickerPreferenceActivity.class.getSimpleName();
private SharedPreferences prefs;
public static int aantalBanen;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.setSharedPreferencesMode(MODE_PRIVATE);
preferenceManager.setSharedPreferencesName("numberPicker.preferences");
addPreferencesFromResource(R.xml.preferences);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
}
public int prefsAantalBanen() {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
return prefs.getInt( "AANTAL_BANEN", 0 );
}
public int getAantalBanen() {
aantalBanen = prefsAantalBanen();
return aantalBanen;
}
@Override
protected void onResume() {
super.onResume();
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
int selectedNumber = prefsAantalBanen();
Log.d(TAG, "Number selected: " + selectedNumber);
Toast t = Toast.makeText(this, "Number selected: " + selectedNumber, Toast.LENGTH_SHORT);
t.show();
}
}
这是 Spel.java,我想在其中显示选择/存储在 ReferenceActivity 中的值,请参见方法 toonClickHandler:
package mypackage.tabnavui;
import mypackage.tabnavui.R;
import mypackage.Controls2.MyGestureDetector;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.AbsListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class Spel extends Activity implements NumberPickerDialog.OnNumberSetListener {
private static final String TAG = Spel.class.getSimpleName();
private GestureDetector gestureDetector;
private TextView textView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetector = new GestureDetector(new MyGestureDetector());
// Use a custom layout file
setContentView(R.layout.spel);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_sample_picker, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_dialog_item) {
NumberPickerDialog dialog = new NumberPickerDialog(this, -1, 2);
dialog.setTitle(getString(R.string.dialog_picker_title));
dialog.setOnNumberSetListener(this);
dialog.show();
return true;
} else if (item.getItemId() == R.id.menu_preferences_item) {
startActivity(new Intent(this, NumberPickerPreferenceActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public void toonClickHandler(View v) {
textView1 = (TextView) this.findViewById(R.id.textView1);
int aantalBanen = NumberPickerPreferenceActivity.aantalBanen;
textView1.setText(String.valueOf(aantalBanen));
}
public void onNumberSet(int number) {
Log.d(TAG, "Number selected: " + number);
Toast t = Toast.makeText(this, "Number selected: " + number, Toast.LENGTH_SHORT);
t.show();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
class MyGestureDetector extends SimpleOnGestureListener {
private static final int SWIPE_MAX_OFF_PATH = 200;
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onDown (MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// left to right swipe and right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// left swipe
Toast t = Toast.makeText(Spel.this, "Left swipe", Toast.LENGTH_LONG);
t.show();
// startActivity(Tabs3.tab3);
startActivityForResult(Tabs3.tab3, 500);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// right swipe
Toast t = Toast.makeText(Spel.this, "Right swipe", Toast.LENGTH_LONG);
t.show();
// startActivity(Tabs3.tab1);
startActivityForResult(Tabs3.tab1, 500);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
return true;
}
return false;
}
}
}
和preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:picker="http://schemas.android.com/apk/res/mypackage.tabnavui"
xmlns:android="http://schemas.android.com/apk/res/android">
<nl.computerhuys.tabnavui.NumberPickerPreference
android:key="AANTAL_BANEN"
android:title="Aantal banen"
android:summary="Number picker as a preference"
picker:defaultValue="2"
picker:startRange="0"
picker:endRange="12" />
</PreferenceScreen>