Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

java - How to create a custom Preference layout and access the views within that layout programatically?

I've been stuck on this for 2 days and I've asked 2 questions with zero response.

The info about how to create a custom Preference is out there but they're all useless because when it comes to accessing the views that you create within that custom preference they just say to do everything under the onBindViewHolder() method of the class.

In order to do anything meaningful with these views they must be accessed from within the Preference fragment! So far I haven't found a way to do that.

Does anyone have a full working example of how to create a custom preference and then access its views from within the Preference fragment?

question from:https://stackoverflow.com/questions/65889492/how-to-create-a-custom-preference-layout-and-access-the-views-within-that-layout

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well you didn't show anything, so i'm just guessing here. Here an example of custom preferences.

import android.content.*;
import android.preference.*;
import android.widget.*;

public final class Preferences
{
    private static Preferences instance;
    private final SharedPreferences prefs;
    private final SharedPreferences.Editor editor;

        public final TextView textView;
    
    private final Preferences(final Context context) {
        instance = this;
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        editor = prefs.edit();
    }
    
    public static final Preferences getInstance(final Activity activity) {
        if(instance == null) {
                        instance = new Preferences(activity);
                        textView = activity.findViewById(R.id.my_textView);
                 }
        return instance;
    }

    /*****************
     *    UTILITY    *
     *****************/
    
    public final void putBoolean(final String name, final boolean value) {
        editor.putBoolean(name, value);
        editor.commit();
    }
    
    public final boolean getBoolean(final String name, final boolean defaultValue) {
        return prefs.getBoolean(name, defaultValue);
    }
    
    public final void putInt(final String name, final int value) {
        editor.putInt(name, value);
        editor.commit();
    }

    public final int getInt(final String name, final int defaultValue) {
        return prefs.getInt(name, defaultValue);
    }
    
    public final void putFloat(final String name, final float value) {
        editor.putFloat(name, value);
        editor.commit();
    }

    public final float getFloat(final String name, final float defaultValue) {
        return prefs.getFloat(name, defaultValue);
    }
    
    public final void putLong(final String name, final long value) {
        editor.putLong(name, value);
        editor.commit();
    }

    public final long getLong(final String name, final long defaultValue) {
        return prefs.getLong(name, defaultValue);
    }
}

Then you can just do this. Make sure to call this inside of a class that extends Activity.

Preferences prefs = Preferences.getInstance(this); //this is an Activity's instance.
prefs.textView.setText("Hello World");

EDIT

import android.content.*;
import android.preference.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class MyCustomPreference extends Preference
{

    public TextView textView;
    public ImageButton imageButton;

    public MyCustomPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public MyCustomPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyCustomPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCustomPreference(Context context) {
        super(context);
        
    }

    @Override
      public void onBindViewHolder(PreferenceViewHolder holder)
    {
                super.onBindViewHolder(holder);
        if(textView == null) {
            textView = view.findViewById(R.id.my_textView);
            imageButton = view.findViewById(R.id.my_imageButton);
        }
    }

        public TextView getTextView(Activity activity) {
               if(textView == null) return activity.findViewById(R.id.my_textView);
               return textView;
        }

        public ImageButton getImageButton(Activity activity) {
               if(imageButton == null) return activity.findViewById(R.id.my_imageButton);
               return imageButton;
        }
}

Hopefully this will resolve your NullPointerException problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...