电脑屏幕几秒钟黑屏一下_简单的设置库可在几秒钟内建立一个设置屏幕

电脑屏幕几秒钟黑屏一下

Most Android developers would agree, that building a settings screen, even with the Jetpack Preference API, is quite painful. You have to create the xml file, the activity and the settings fragment where the preferences are getting rendered. Especially realizing dynamic preferces for certain purposes is a small challenge and the resulting code is everything, but not clean. This article describes a simplification for it, using the SimpleSettings library. With this library, you can specify your preference configuration directly in your Kotlin/Java file or if you want to stick with the xml version, be happy: this is possible as well and the recommended way for large and complex preference configurations!

大多数Android开发人员都会同意,即使使用Jetpack Preference API来构建设置屏幕也很痛苦。 您必须创建要呈现首选项的xml文件,活动和设置片段。 尤其是针对某些目的实现动态优先级是一个小挑战,并且生成的代码是所有内容,但不是干净的。 本文介绍了使用SimpleSettings库进行的简化。 有了这个库,您可以直接在Kotlin / Java文件中指定您的首选项配置,或者如果您想使用xml版本,请开心:这也是可能的,也是大而复杂的首选项配置的推荐方式!

Example preference screen, generated with the SimpleSettings lib
Example preference screen, generated with the SimpleSettings lib
使用SimpleSettings lib生成的示例首选项屏幕

This screenthot shows a preference screen, which was generated with only a few lines of Kotlin code.

该屏幕提示显示了一个首选项屏幕,该屏幕仅由几行Kotlin代码生成。

SimpleSettings is an extension of the existing framework, provided by the Jetpack Preference API by Google, which makes it a lot more easier to get started with a settings screen. Futhermore, SimpleSettings gives you useful extensions, e.g. predefined PreferenceClickListeners (WebsiteClickListener, PlayStoreClickListener, etc.), which can easily be attached to preference items (More about this later on).

SimpleSettings是Google的Jetpack Preference API提供的现有框架的扩展,它使使用设置屏幕入门变得更加容易。 此外,SimpleSettings为您提供了有用的扩展,例如预定义的PreferenceClickListeners(WebsiteClickListener,PlayStoreClickListener等),可以轻松将其附加到首选项(稍后将对此进行详细介绍)。

Also, there are lots of useful additional features. For instance, you can show menu items dynamically in the toolbar or show a reset option for resetting all preferences at once. These options can be directly passed to the SimpleSettings instance, which will be demonstrated later in this article.

另外,还有许多有用的附加功能。 例如,您可以在工具栏中动态显示菜单项,也可以显示一个重置选项以立即重置所有首选项。 这些选项可以直接传递到SimpleSettings实例,这将在本文稍后进行演示。

设置您的设置屏幕 (Setup your settings screen)

As always, the first step is to add the dependencies to your build.gradle file at module level.

与往常一样,第一步是在模块级别将依赖项添加到build.gradle文件中。

implementation 'com.chillibits:simplesettings:1.0.0-alpha08'


// Required dependencies
implementation 'com.google.android.material:material:<latest-version>'
implementation 'androidx.preference:preference:<latest-version>'

The second thing we have to do is to register the SimpleSettingsActivity in the apps manifest.

我们要做的第二件事是在应用清单中注册SimpleSettingsActivity。

<activity
    android:name="com.chillibits.simplesettings.ui.SimpleSettingsActivity"
    android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar" />

Then you have two options to choose: Kotlin or XML.

然后,您有两个选择:Kotlin或XML。

Kotlin配置 (Kotlin configuration)

Let’s assume you’re developing a flight booking app and you want to provide a SwitchPreference item for each booked flight on the central settings screen, where the user can enable/disable status notifications about this specific flight. With the standard Jetpack Preference API, this behaviour would be hard to achieve, whereas SimpleSettings comes with a simple solution for this problem. The settings screen can be populated with nested Kotlin objects like this:

假设您正在开发一个航班预订应用程序,并且想在中央设置屏幕上为每个预订的航班提供一个SwitchPreference项目,用户可以在其中启用/禁用有关此特定航班的状态通知。 使用标准的Jetpack首选项API,将很难实现此行为,而SimpleSettings附带了针对此问题的简单解决方案。 可以使用如下所示的嵌套Kotlin对象填充设置屏幕:

SimpleSettings(this).show {
    Section {
        title = "Test section"
        for (i in 0..4) {
            SwitchPref {
                title = "Test 1.$i"
                summary = "This is a Test 1.$i"
                defaultValue = if(i % 2 == 0) SimpleSwitchPreference.ON else SimpleSwitchPreference.OFF
            }
        }
        if(true) {
            TextPref {
                title = "Test 2"
                summary = "This is a Test 2"
            }
        }
    }
    Section {
        InputPref {
            title = "Test 3"
            summary = "This is a Test 3"
        }
    }
}

The library uses the same concept, which Jetpack Compose uses, but with even less boilerplate, due to the special use cases.

该库使用与Jetpack Compose相同的概念,但由于特殊的用例,其模板更少。

If you only have a few preference items to handle, this is also the recommended way to setup your settings screen.

如果您只需要处理一些首选项,这也是设置设置屏幕的推荐方法。

XML配置 (XML configuration)

The second option is to provide a xml file.

第二个选项是提供一个xml文件。

<PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <PreferenceCategory
        app:key="preference_category"
        app:title="Preference category">


        <Preference
            app:key="preference_test"
            app:title="Test"
            app:summary="This is a test preference"/>


        <SwitchPreferenceCompat
            app:key="test"
            app:defaultValue="true"
            app:title="Test"
            app:summary="This is a test"/>
    </PreferenceCategory>
</PreferenceScreen>

To generate an activity from this XML configuration, you can tie this file to the SimpleSettings builder instance with an one-liner:

要从此XML配置生成活动,可以将该文件与单行绑定到SimpleSettings构建器实例:

SimpleSettings(this@MainActivity).show(R.xml.preferences)

自定义SettingsActivity (Customize the SettingsActivity)

There are a few customization options for the underlying activity:

基础活动有一些自定义选项:

  • Providing menu items for the toolbar

    提供工具栏的菜单项
  • Enabling a reset option for the user to reset all preferences

    为用户启用重置选项以重置所有首选项
  • Disabling “HomeAsUpEnabled” for the activity

    为活动禁用“ HomeAsUpEnabled”
  • More to come …

    更多……

You can apply those options by creating a SimpleSettingsConfig instance and pass it to the SimpleSettings construcor. The example below also shows the usage of a PreferenceClick callback for custom ClickListeners. One of them is set to the above mentioned WebsiteClickListener. You just have to pass the URL and the library does the rest for you. Fairly simple isn’t it?

您可以通过创建SimpleSettingsConfig实例并将其传递给SimpleSettings构造器来应用这些选项。 以下示例还显示了自定义ClickListener的PreferenceClick回调的用法。 其中之一设置为上述的WebsiteClickListener。 您只需要传递URL,然后库将为您完成其余的工作。 是不是很简单?

private fun showPreferences() {
    val config = SimpleSettingsConfig().apply {
        showResetOption = true
        preferenceCallback = this@MainActivity
    }
    SimpleSettings(this@MainActivity, config).show(R.xml.preferences)
}


override fun onPreferenceClick(context: Context, key: String): Preference.OnPreferenceClickListener? {
    return when(key) {
        "preference" -> WebsiteClickListener(this@MainActivity, getString(R.string.url_github))
        "custom-preference" -> Preference.OnPreferenceClickListener {


            true
        }


        else -> super.onPreferenceClick(context, key)
    }
}

将Jetpack首选项迁移到SimpleSettings (Migrating Jetpack Preference to SimpleSettings)

The migration from Jetpack Preference to the SimpleSettings lib is quite simple. You can use the same XML preference file, which you have used with the Jetpack Preference framework to setup a SimpleSettings screen. Just delete your SettingsActivity and the SettingsFragment and you are ready to go! Just use the code snippet above to initialize the library with a XML configuration.

从Jetpack Preference迁移到SimpleSettings库非常简单。 您可以使用与Jetpack Preference框架一起使用的XML首选项文件来设置SimpleSettings屏幕。 只需删除您的SettingsActivity和SettingsFragment,就可以开始了! 只需使用上面的代码段即可使用XML配置初始化库。

I hope this article helped you to develop your apps preferences much faster and with more efficiency.

我希望本文能帮助您更快,更高效地开发应用程序首选项。

To get more information about the SimpleSettings library, please visit it on GitHub and take a look on its GitHub wiki!

要获取有关SimpleSettings库的更多信息,请在GitHub上访问它,并查看其GitHub Wiki

翻译自: https://medium.com/swlh/simple-settings-library-build-a-settings-screen-in-seconds-5b6394fbd2fc

电脑屏幕几秒钟黑屏一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值