Android get view of Preference in PreferenceActivity

后端 未结 3 1739
慢半拍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.

提交回复
热议问题