我怎么能继续增加preferences当我点击一个? [英] How can I keep on adding preferences when i click one?

查看:118
本文介绍了我怎么能继续增加preferences当我点击一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我想给用户自由我的应用程序选择一个号码蓝牙设备。现在,我不知道有多少蓝牙设备会不会有,所以我想知道是否有可能给用户一个选项,点击添加新的蓝牙设备定制preference,开辟一个新的preference屏幕为他选择新设备的蓝牙设置?

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:

蓝牙设备1

添加新设备Blutooth ..

Add a new Blutooth Device..

如果用户添加另外一个就应该是这样的:

If user adds another one it should look like:

蓝牙设备1

蓝牙设备2

添加一个新的蓝牙设备。

Add a new Bluetooth Device..

我知道如何使用标准preferences和基本编码。我想知道的唯一的事情是我可以继续增加对这些设备设置在runtime.I将AP preciate任何帮助。谢谢你。

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.

推荐答案

不知道如果我充分理解这一点,但它听起来像你想添加一个preference每个蓝牙设备。要做到这一点,你会想要做这样的事情:

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();

如果您想要检索的所有preferences每个蓝牙设备,你可以得到的将所有键设置在你的共享preferences文件,找出哪些有BTpreFIX,拉各的preferences的。事情是这样的:

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
   }
}

然而,它只是我恍然大悟,这听起来像你所谈论的动态添加preference意见。这完全是另一回事=)。

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

编辑:这是如何从preferences活动以编程方式添加一个preference有一个观点:

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);
    }

在这个例子中,我给我的preferenceScreenpreFSMAIN的关键。你可以添加任何类型的preference你以这种方式想要的。

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

这篇关于我怎么能继续增加preferences当我点击一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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