我在我的preferences一个搜索栏,以更新屏幕亮度。有没有办法来刷新UI看直播的变化? [英] I have a seekbar in my preferences to update screen brightness. Is there a way to refresh the UI to see the live change?

查看:114
本文介绍了我在我的preferences一个搜索栏,以更新屏幕亮度。有没有办法来刷新UI看直播的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索栏工作正常无处不在,但我希望看到发生的变化,因为它们是在preference屏幕进行。问题在于seekbar的滑动是preference,因此我不能做共享preferences并在onProgressChanged方法更新它。

The seekbar works fine everywhere, but I'd like to see the changes happen as they are made in that preference screen. The problem is that the seekbar is a preference, and thus I can't do sharedpreferences and update it in the onProgressChanged method.

有没有解决这个办法吗?

Is there any way around this?

设置我的亮度是这样的:

I set my brightness like this:

    SharedPreferences prefs;
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    float sb = prefs.getInt("screenBrightness", 1);
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = sb / 100.0f;
    getWindow().setAttributes(lp);

但由于该类扩展preference我不能使用,在onProgressChanged。

But I can't use that in onProgressChanged because the class is extending Preference.

推荐答案

您需要做的就是使用 OnShared preferencesChanged 听者如 onShared preferenceChanged(共享preferences共享preferences,串键)。里面你preferenceActivity的做到这一点:

What you need to do is use an OnSharedPreferencesChanged listener like onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key). Inside of your PreferenceActivity do this:

SharedPreferences.OnSharedPreferenceChangeListener prefChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() { 
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 
        if(key.equals("screenBrightness")) {
        //set brightness here
            float sb = sharedPreferences.getInt(key, 1);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = sb / 100.0f;
            getWindow().setAttributes(lp);
        }
    }
};

,然后在 onShared preferenceChanged ,你可以使用你已经使用密钥和相应地设置亮度

and then in onSharedPreferenceChanged, you can use the key you've been using and set the brightness accordingly

这篇关于我在我的preferences一个搜索栏,以更新屏幕亮度。有没有办法来刷新UI看直播的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆