首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用androidx从SeekBarPreference获取OnPreferenceChanged事件

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

Stack Overflow用户
提问于 2019-06-11 21:42:12
回答 1查看 400关注 0票数 0

我一直在尝试创建一个安卓设置屏幕,里面有几个SeekBarPreferences,可以选择0到10之间的三个数字。但是,我的OnPreferenceChanged代码没有被调用。我的设置活动代码:

代码语言:javascript
复制
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;
                }
            });
        }
    }
}

我的首选布局代码:

代码语言:javascript
复制
<?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>

我应该如何从一个搜索栏的首选项中获取一个类似于首选项变化的事件呢?(我使用的是v7支持库,不能选择切换/添加新库)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-11 22:38:57

好吧,我不小心忘了在Toast上调用.show()。此外,我为测试而错误地配置了它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56545141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档