简体   繁体   English

如何创建自定义首选项布局并以编程方式访问该布局中的视图?

[英]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.我已经坚持了 2 天,我问了 2 个问题,但回答为零。

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.有关如何创建自定义首选项的信息在那里,但它们都没有用,因为当涉及到访问您在该自定义首选项中创建的视图时,他们只是说在 class 的onBindViewHolder()方法下执行所有操作。

In order to do anything meaningful with these views they must be accessed from within the Preference fragment.为了对这些视图做任何有意义的事情,必须从 Preference 片段中访问它们。 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?有没有人有一个完整的工作示例来说明如何创建自定义首选项,然后从 Preference 片段中访问其视图?

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 .确保在扩展Activity的 class 内部调用它。

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.希望这将解决您的 NullPointerException 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM