Android自定义首选项

时间:2015-07-21 11:10:31

标签: android settings custom-component

我正在使用PreferenceActivity和PreferenceFragment开发我的应用程序的设置,我是新的,我发现我的一个自定义偏好设置有问题。

ImagePreference是一个显示用户个人资料图片的首选项,问题是如果我从我的图库中选择一个图像,然后选中/取消选中其中一个ChecboxPreferences,图像将变为第一个图像,因为我已经永远不要选择新的形象。

SettingsFragment

public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);

        imagePreference = (ImagePreference) findPreference("cambiar_foto");
        imagePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(GaleryIntent, 0);
                return true;
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0){
            if ( resultCode == Activity.RESULT_OK && null != data) {
                ...    
                mProfileImage = (ImageView) getActivity().findViewById(R.id.iv_ic_foto);
                mProfileImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                Toast.makeText(getActivity(), picturePath, Toast.LENGTH_SHORT).show();

            }
        }

    }

}

的preferences.xml

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <...>
    <PreferenceCategory
        android:title="Cambiar o agregar foto">
        <com.zonaapp.taxis.demo.preferences.ImagePreference
            android:key="cambiar_foto"/>
    <PreferenceCategory
        android:title="Cobertura de información">

        <CheckBoxPreference
            android:key="parent_checkbox_preference"
            android:title="Mi ciudad"/>

        <CheckBoxPreference
            android:key="parent_checkbox_preference"
            android:title="Mi país"/>

        <CheckBoxPreference
            android:key="parent_checkbox_preference"
            android:title="Todos"/>

    </PreferenceCategory>
    <...>
</PreferenceScreen>

自定义偏好类:

public class ImagePreference extends Preference {

   private Context mContext;

   public ImagePreference(Context context) {
       super(context);
       mContext = context;
   }

   public ImagePreference(Context context, AttributeSet attributeSet){
       super(context, attributeSet);
       mContext = context;
   }

   public ImagePreference(Context context, AttributeSet attributeSet, int defStyle){
       super(context, attributeSet, defStyle);
       mContext = context;
   }

   public View getView(View convertView, ViewGroup parent){
       View row = convertView;
       if (convertView == null) {
           LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           row = inflater.inflate(R.layout.preference_screen_cambiar_foto, parent, false);
       }

       ImageView imageView = (ImageView) row.findViewById(R.id.iv_ic_foto);
       Picasso.with(mContext).load(ParseUser.getCurrentUser().getParseFile("profilePic").getUrl()).into(imageView);

       return row;
   }
}

1 个答案:

答案 0 :(得分:1)

您需要保存您的偏好状态。应用官方文档中所示的程序。

http://developer.android.com/guide/topics/ui/settings.html#CustomSaveState

并保存和恢复首选项状态