当我点击一个偏好设置时,如何继续添加偏好设置?

发布于 2024-10-26 20:12:24 字数 364 浏览 2 评论 0原文

我有一个问题。我想让用户在我的应用程序中自由选择多个蓝牙设备。现在我不知道会有多少个蓝牙设备,所以我想知道是否可以为用户提供一个选项,单击“添加新蓝牙设备”自定义首选项,并打开一个新的首选项屏幕供他选择新的蓝牙设置设备?

总之,显示内容将如下所示:

添加一个新的蓝牙设备。

如果用户添加一个,它应如下所示:

蓝牙设备 1

添加一个新的蓝牙设备。

如果用户添加另一个蓝牙设备,它应如下所示:

蓝牙设备 1

蓝牙设备 2

添加新的蓝牙设备。

我知道如何使用标准首选项和基本编码。我唯一想知道的是如何在运行时继续添加这些设备的设置。我将不胜感激任何帮助。谢谢。

I have a problem. I want to give a user freedom to select a number of Bluetooth devices in my app. Now I dont know how many Bluetooth devices will there be so I was wondering is it possible to give user an option to click on "Add a new Bluetooth Device" custom preference and open up a new preference screen for him to select Bluetooth setting of new device?

In summary the display would look like:

Add a new Bluetooth Device..

If user adds one, it should then look like:

Bluetooth Device 1

Add a new Blutooth Device..

If user adds another one it should look like:

Bluetooth Device 1

Bluetooth Device 2

Add a new Bluetooth Device..

I know how to use standard preferences and basic coding. The only thing I want to know is how can I keep on adding settings for these devices at runtime.I will appreciate any help. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

oО清风挽发oО 2024-11-02 20:12:24

不确定我是否完全理解这一点,但听起来您想为每个蓝牙设备添加首选项。为此,您需要执行以下操作:

在添加蓝牙设备的函数内部:

SharedPreferences prefs = getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();
editor.putString("BT" + nameOfDevice, whateverYouWantToStoreAboutTheDevice);
editor.commit();

如果您想检索每个蓝牙设备的所有首选项,您可以在 SharedPreferences 文件中获取所有键的集合,找出哪些具有“BT”前缀,并提取每个首选项。像这样的事情:

Set<String> keySet = prefs.getAll().keySet();
for(String key : keySet){
   if(key.startsWith("BT"){
       String theValue = prefs.getString(key, null);
       //Do whatever with that value
   }
}

但是,我突然意识到,听起来您正在谈论动态添加首选项视图。这完全是另一回事=)。

编辑:以下是如何通过您的首选项以编程方式添加带有视图的首选项活动:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        CheckBoxPreference checkbox = new CheckBoxPreference(this);
        checkbox.setTitle("This is a checkbox preference");
        ((PreferenceScreen)findPreference("PREFSMAIN")).addPreference(checkbox);
    }

在此示例中,我为我的 PreferenceScreen 提供了“PREFSMAIN”键。您可以通过这种方式添加您想要的任何类型的首选项。

Not sure if I'm understanding this completely, but it sounds like you would want to add a preference for each bluetooth device. To do this, you would want to do something like this:

Inside of the function in which you add the bluetooth device:

SharedPreferences prefs = getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();
editor.putString("BT" + nameOfDevice, whateverYouWantToStoreAboutTheDevice);
editor.commit();

If you want to retrieve all the preferences for each bluetooth device, you could get the Set of all keys in your SharedPreferences file, figure out which ones have the "BT" prefix, and pull each of those preferences. Something like this:

Set<String> keySet = prefs.getAll().keySet();
for(String key : keySet){
   if(key.startsWith("BT"){
       String theValue = prefs.getString(key, null);
       //Do whatever with that value
   }
}

However, it just dawned on me that it sounds like you're talking about dynamically adding Preference Views. That's something else entirely =).

Edit: Here's how to add a Preference with a View programmatically from your preferences Activity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        CheckBoxPreference checkbox = new CheckBoxPreference(this);
        checkbox.setTitle("This is a checkbox preference");
        ((PreferenceScreen)findPreference("PREFSMAIN")).addPreference(checkbox);
    }

In this example, I gave my PreferenceScreen a key of "PREFSMAIN". You could add any kind of Preference you wanted in this manner.

网白 2024-11-02 20:12:24

Consider implementing your own preference activity and using http://developer.android.com/reference/android/preference/PreferenceManager.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文