gpt4 book ai didi

java - 如何使用 androidx 从 SeekBarPreference 获取 OnPreferenceChanged 事件

转载 作者:行者123 更新时间:2023-12-02 04:37:15 28 4
gpt4 key购买 nike

我一直在尝试制作一个 Android 设置屏幕,其中包含一些 SeekBarPreferences,以选择从 0 到 10 的三个数字。但是,我的 OnPreferenceChanged 代码没有被调用。我的设置 Activity 代码:

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 支持库,并且不能切换/添加新支持库)

最佳答案

好吧,我不小心忘记在我的 Toast 上调用 .show() 。另外,我没有正确配置它进行测试。

关于java - 如何使用 androidx 从 SeekBarPreference 获取 OnPreferenceChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56545141/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com