我创建了一个扩展android.support.v7.preference的类来创建自定义首选项,然后将其包含在我的preference.XML文件中。 我在我的MainActivity中实现SharedPreferences.OnSharedPreferenceChangeListener和@Override onSharedPreferenceChanged默认首选项触发函数但自定义首选项没有。 任何人都可以帮忙
您可以查看here
中的所有项目这是我的自定义偏好类
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekbarPreference extends Preference {
private static final String TAG = "SeekbarPreference";
public SeekbarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.seekbar_preferences);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
holder.itemView.setClickable(false);
SeekBar seekBar = (SeekBar) holder.findViewById(R.id.seekbar);
final TextView tvSummary = (TextView) holder.findViewById(R.id.tvSummary);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvSummary.setText(String.valueOf(progress));
Log.d(TAG, "onProgressChanged: ");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d(TAG, "onStartTrackingTouch: ");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d(TAG, "onStopTrackingTouch: ");
}
});
}
}
MainActivity类
public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String SWITCH_ON_SHARED_KEY = "switchON";
private static final String CHECK_SHARED_KEY = "allUsers";
private static final String SEEKBAR_SHARED_KEY = "Seekbar_after";
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupSharedPreferences();
}
private void setupSharedPreferences() {
// Get all of the values from shared preferences to set it up
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.setting_item) {
Intent intent = new Intent(this, SettingActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d(TAG, "onSharedPreferenceChanged: " + key);
}
}
我的preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreferenceCompat
android:defaultValue="@bool/default_switch"
android:key="switchON"
android:summaryOff="@string/hidden_text"
android:summaryOn="@string/shown_text"
android:title="Mode ON" />
<PreferenceCategory android:title="Decorate">
<CheckBoxPreference
android:defaultValue="@bool/default_check"
android:key="allUsers"
android:summaryOff="@string/hidden_text"
android:summaryOn="@string/shown_text"
android:title="All" />
<com.example.momenali.custompreference.SeekbarPreference
android:defaultValue="light"
android:key="Seekbar_after" />
</PreferenceCategory>
</PreferenceScreen>