Android get view of Preference in PreferenceActivity

后端 未结 3 1744
慢半拍i
慢半拍i 2021-01-11 13:56

I would like to get View instance that is used to display specific Preference in my PreferenceActivity, so i can modify its properties, for example:

public          


        
相关标签:
3条回答
  • 2021-01-11 13:59

    To get the view of a desired preference you can use this:

    @Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        super.onWindowFocusChanged(hasFocus);
        Preference preference=findPreference(“preferenceKey”);
        View preferenceView=getListView().getChildAt(preference.getOrder());
        //Do your stuff
    }
    

    Note: You can not do this in the onCreate method because it will throw a NullPointerException.

    0 讨论(0)
  • 2021-01-11 14:07

    PreferenceActivity inherits the ListActivity class. ListActivity has a method called getListView() which returns the ListView that displays your preferences.

    EDIT: Here is the code in my comment formatted:

    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
           // ... put listener code here 
    });
    
    0 讨论(0)
  • 2021-01-11 14:16

    I'm not sure on how to get the view for a preference, but if you want to remove the view from the screen (set visibility to View.gone) you can use the following:

    getPreferenceScreen().removePreference(thePreference)

    0 讨论(0)
提交回复
热议问题