Android Preferenceactivity getView

前提是你 提交于 2019-12-07 18:18:31

问题


I have such piece of code :) And i want to edit text property of my custom preference layout. But any changes made on object from getView function does not affect actual list in preference screen. any ideas? I know that i cant extend PreferenceScreen, and i cant use any other type of preference in this case, i only want to be able to edit my custom textview from my layout in code.

PreferenceScreen settings = getPreferenceManager().createPreferenceScreen(this);

settings.setLayoutResource(R.layout.mypreference);
View test = (View) settings.getView(null,getListView());

TextView text = (TextView)test.findViewById(R.id.text);
text.setText("BLA BLA");    

回答1:


We should be using getView(convertView, parent) but from countless unanswered questions on StackOverflow, it's obvious no one knows how to use it. So our only option is to create a custom Preference:

Create a new Class:

public class CustomPreference extends Preference {
    public CustomPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onBindView(View rootView) {
        super.onBindView(rootView);

        View myView = rootView.findViewById(R.id.myViewId);
        // do something with myView
    }
}

In your xml/preferences.xml, add your custom preference:

<your.package.name.CustomPreference
    android:key="custom_preference"
    android:layout="@layout/custom_layout" />



回答2:


Using the custom layout below:

custom_preferences_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <Button 
        android:id="@+id/preview_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:text="@string/notification_preview" />
    <ListView 
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Using the file above with your user preferences xml file, you can do this to access the items in your custom layout:

public class CustomPreferenceActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle bundle){
        super.onCreate(bundle);
        addPreferencesFromResource(R.xml.custom_preferences);
        setContentView(R.layout.custom_preferences_layout);
    }

    private void initCustomizePreferences(){
        //Button
        Button button = (Button)findViewById(R.id.preview_button);
        //Do something with the button.

    }

}

I hope this helps :)




回答3:


You need to override onBindView in your custom Preference class.

More details - Get view of preference with custom layout




回答4:


I never actually messed with the views of a PreferenceActivity, but here's something I do (usually for ListPreferences)

ListPreference listPref;
CheckBoxPreference cbPref;

// Set the summary as the current listpref value
listPref = (ListPreference) findPreference("list_pref_key");
listPref.setSummary(listPref.getEntry()); 

// change the title / summary / ??? of any preference
findPreference("anothe_pref_key").setTitle("New Title");
findPreference("anothe_pref_key").setSummary("New subtext for the view");

This way, even if the layout used by PreferenceActivity / PreferenceFragment change in a future version, you wont have to change anything in your code.



来源:https://stackoverflow.com/questions/12185189/android-preferenceactivity-getview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!