我一直在尝试制作一个带有几个SeekBarPreferences的android设置屏幕,以选择0到10之间的三个数字。 但是,我的OnPreferenceChanged代码没有被调用。 我的设置活动代码:
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SeekBarPreference;
public class AdvancedSettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new AdvancedSettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
}
public static class AdvancedSettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.advanced_preferences, rootKey);
final SharedPreferences prefs=getContext().getSharedPreferences("gameSettings",0);
final SharedPreferences.Editor prefsedit=prefs.edit();
SeekBarPreference red=findPreference("redseekbar");
SeekBarPreference green=findPreference("greenseekbar");
SeekBarPreference blue=findPreference("blueseekbar");
red.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
prefsedit.putInt("red",(int)newValue);
prefsedit.apply();
Toast.makeText(getContext(),newValue+"",Toast.LENGTH_LONG);
return true;
}
});
green.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
prefsedit.putInt("green",(int)newValue);
prefsedit.apply();
Toast.makeText(getContext(),newValue+"",Toast.LENGTH_LONG);
return true;
}
});
blue.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
prefsedit.putInt("blue",(int)newValue);
prefsedit.apply();
Toast.makeText(getContext(),newValue+"",Toast.LENGTH_LONG);
return true;
}
});
}
}
}
我的首选项布局代码:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<SeekBarPreference
app:key="redseekbar"
app:title="Red"
app:summary="The red change value"
android:max="10"
app:defaultValue="0"
app:showSeekBarValue="true"/>
<SeekBarPreference
app:key="greenseekbar"
app:title="Green"
app:summary="The green change value"
android:max="10"
app:defaultValue="0"
app:showSeekBarValue="true"/>
<SeekBarPreference
app:key="blueseekbar"
app:title="Blue"
app:summary="The blue change value"
android:max="10"
app:defaultValue="0"
app:showSeekBarValue="true"/>
</PreferenceScreen>
我应该如何从seekbarpreference获取类似onpreferencechanged的事件? (我使用的是v7支持库,不能选择切换/添加新库)
答案 0 :(得分:0)
好的,我不小心忘记在Toasts上调用.show()了。另外,我没有正确配置它进行测试。