gpt4 book ai didi

Android 自定义首选项

转载 作者:行者123 更新时间:2023-11-30 02:00:40 26 4
gpt4 key购买 nike

我正在使用 PreferenceActivity 和 PreferenceFragment 开发我的应用程序设置,我是这方面的新手,我发现我的自定义首选项之一有问题。

ImagePreference 是显示用户配置文件图像的首选项,问题是如果我从我的图库中选择图像,然后选中/取消选中其中一个 ChecboxPreferences,图像会更改为第一个,因为我从未选择过一个新形象。

设置 fragment

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();

}
}

}

}

首选项.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;
}
}

最佳答案

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

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

以及保存和恢复首选项的状态

关于Android 自定义首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31537414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com