当我点击一个偏好设置时,如何继续添加偏好设置?
我有一个问题。我想让用户在我的应用程序中自由选择多个蓝牙设备。现在我不知道会有多少个蓝牙设备,所以我想知道是否可以为用户提供一个选项,单击“添加新蓝牙设备”自定义首选项,并打开一个新的首选项屏幕供他选择新的蓝牙设置设备?
总之,显示内容将如下所示:
添加一个新的蓝牙设备。
如果用户添加一个,它应如下所示:
蓝牙设备 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定我是否完全理解这一点,但听起来您想为每个蓝牙设备添加首选项。为此,您需要执行以下操作:
在添加蓝牙设备的函数内部:
如果您想检索每个蓝牙设备的所有首选项,您可以在 SharedPreferences 文件中获取所有键的集合,找出哪些具有“BT”前缀,并提取每个首选项。像这样的事情:
但是,我突然意识到,听起来您正在谈论动态添加首选项视图。这完全是另一回事=)。
编辑:以下是如何通过您的首选项以编程方式添加带有视图的首选项活动:
在此示例中,我为我的 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:
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:
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:
In this example, I gave my PreferenceScreen a key of "PREFSMAIN". You could add any kind of Preference you wanted in this manner.
考虑实现您自己的首选项活动并使用 http://developer.android.com/reference /android/preference/PreferenceManager.html
Consider implementing your own preference activity and using http://developer.android.com/reference/android/preference/PreferenceManager.html