How to add a button to a PreferenceScreen?

后端 未结 10 1311
抹茶落季
抹茶落季 2020-11-28 20:48

I\'m quite new to Android Development and just came across Preferences. I found PreferenceScreen and wanted to create a login functionality with it. The only pr

10条回答
  •  盖世英雄少女心
    2020-11-28 21:14

    If you'd like to catch a user click on one of the Preferences items and your app is using PreferenceFragmentCompat, then you can do this:

    
    ...
    
    ...
    
    

    Java:

    @Override
    onPreferenceTreeClick(Preference preference) {
        if (preference.getKey().equals(getContext().getString(R.string.pref_key_clickable_item))) {
           // user clicked the item
           // return "true" to indicate you handled the click
           return true;
        }
        return false;
    }
    

    Kotlin:

    override fun onPreferenceTreeClick(preference: Preference): Boolean {
        if (preference.key == context?.getString(R.string.pref_key_clickable_ite)) {
           // user clicked the item
           // return "true" to indicate you handled the click
           return true
        }
        return false
    }
    

提交回复
热议问题