我试图解决我的问题,所以我更改了很多代码。我甚至更改帖子的标题。我可以在首选项ui中成功更改imageview背景的颜色。但是,在离开片段并再次启动之后,将无法以与以前相同的方式来更新ui。
首先,我使用sherlockactionbar。有3个标签。当按下第三条时,将加载一个片段,其中包括按钮。当按下一个按钮时,将加载另一个首选项片段。
以下代码显示了按下按钮之一时如何调用首选项片段:SettingsMenuFragment.java
public class SettingsMenuFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button ICSbutton= (Button) view.findViewById(R.id.CallSearchSettingsButton);
ICSbutton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
clearFragmentStack();
SettingsIncomingSearchFragment removeSISF = (SettingsIncomingSearchFragment) getActivity().getSupportFragmentManager().findFragmentByTag("SISF");
if(removeSISF != null)
{
getActivity().getSupportFragmentManager().beginTransaction().remove(removeSISF).commit() ;
getActivity().getSupportFragmentManager().executePendingTransactions();
}
SettingsIncomingSearchFragment Prefrag = new SettingsIncomingSearchFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, Prefrag ,"SISF");
transaction.addToBackStack(null);
transaction.commit();
getActivity().getSupportFragmentManager().executePendingTransactions();
}
} );}}
并且下面的代码显示preferencefragment:
public class SettingsIncomingSearchFragment extends PreferenceListFragment implements SharedPreferences.OnSharedPreferenceChangeListener,PreferenceListFragment.OnPreferenceAttachedListener {
Context ctx ;
public static final String SHARED_PREFS_NAME = "settings";
LinearLayout mainlayout ;
LinearLayout sublayout ;
View view ;
Preference myPref ;
ImageView img ;
SharedPreferences sp ;
@Override
public void onCreate(Bundle icicle) {
ctx = getActivity() ;
super.onCreate(icicle);
addPreferencesFromResource(R.xml.pref_incomingsearchsettings);
myPref = (Preference) findPreference("incomingsearchbackgroundcolor");
setColor() ;
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
HSVColorPickerDialog cpd = new HSVColorPickerDialog( ctx, 0xFF4488CC, new OnColorSelectedListener() {
@Override
public void colorSelected(Integer color)
{
sp.edit().putString("incomingsearchbackgroundcolor", String.valueOf(color)).commit();
}
});
cpd.setTitle( "Pick a color" );
cpd.show();
return true ;
}
});
}
private void setColor() {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rectangle_layout, null);
mainlayout = (LinearLayout)view.findViewById(R.id.rectangle_main_layout_ll);
sublayout = (LinearLayout)mainlayout.findViewById(R.id.rectangle_layout_ll);
sp = ctx.getSharedPreferences(SHARED_PREFS_NAME, ctx.MODE_PRIVATE);
String defValue = null ;
defValue = sp.getString("incomingsearchbackgroundcolor", null);
img = (ImageView)sublayout.findViewById(R.id.iv_priority);
img.setBackgroundColor(Integer.parseInt(defValue));
}
@Override
public void onPreferenceAttached(PreferenceScreen root, int xmlId) {
// TODO Auto-generated method stub
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
if(key.equals("incomingsearchbackgroundcolor"))
{
sp = ctx.getSharedPreferences(SHARED_PREFS_NAME, ctx.MODE_PRIVATE);
String defValue = null ;
defValue = sp.getString("incomingsearchbackgroundcolor", null);
Log.d("Debug", defValue);
int iColor = Integer.parseInt(defValue);
img.setBackgroundColor(iColor);
img.invalidate();
if(this.isAdded())
{
getActivity().getSupportFragmentManager().beginTransaction().detach(this).commit() ;
getActivity().getSupportFragmentManager().executePendingTransactions();
getActivity().getSupportFragmentManager().beginTransaction().attach(this).commit();
getActivity().getSupportFragmentManager().executePendingTransactions();
}
}
}
@Override
public void onResume()
{
super.onResume();
sp.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
sp.unregisterOnSharedPreferenceChangeListener(this);
}
}
这是首选项xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:summary = "Options" android:title = "OPTIONS" android:key="options">
<CheckBoxPreference android:key="IncomingCallSearch" android:summary="On/Off" android:title="Enable Incoming Call Search" android:defaultValue="true"/>
</PreferenceCategory>
<PreferenceCategory android:summary = "Screen Settings" android:title = "Screen settings" android:key="ScreenSettings" >
<ListPreference
android:entries="@array/screenLocOptions"
android:entryValues="@array/screenLocValues"
android:key="incomingcallsearch_screenlocation"
android:title="Location"
android:defaultValue="Top"/>
<Preference
android:defaultValue="0xFF000000"
android:key="incomingsearchbackgroundcolor"
android:title="Background Color"
android:layout="@layout/rectangle_layout" />
</PreferenceCategory>
这是rectangular_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="wrap_content"
android:padding="5dp"
android:id="@+id/rectangle_main_layout_ll">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Background color"
android:layout_weight= "0.9"/>
<LinearLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:orientation="vertical"
android:layout_weight= "0.1"
android:id="@+id/rectangle_layout_ll">
<ImageView
android:id="@+id/iv_priority"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
/>
</LinearLayout>
当我写这篇文章的先前版本时,上面的代码在加载的片段,imageview背景颜色在首选项ui中更新的第一个中成功运行。我尝试逐步编写失败条件:
1。我从操作栏中选择第三个栏,然后选择一个包含按钮的片段。
2。我按下加载首选项片段的按钮(您可以看到上面的代码)
3。在此首选项片段中,有一个首选项,包括textview和imageview(您可以看到上面的详细信息)
4。当我单击此首选项时,运行了一个颜色选择器。(您可以看到上面的详细信息)
5。我从颜色选择器中选择一种颜色,并将其保存到sharedpreference。(您可以看到上面的详细信息)
6。触发了onsharedpreferencechanged事件,并且成功更改了imageview的背景颜色(您可以看到上面的详细信息)。
7。我可以通过从操作栏中选择另一个选项卡或使用Backpress按钮来保留片段。
8。我在第三栏中按下相同的按钮来启动相同的片段。
9。我再次使用颜色选择器,并触发了onsharedpreferencechanged事件。
10。我可以通过调试看到,真彩色代码是从sharedpreference提取的,并将其设置为imageview背景色,并且运行了以下代码:
getActivity()。getSupportFragmentManager()。beginTransaction()。detach(this).commit(); getActivity()。getSupportFragmentManager()。executePendingTransactions(); getActivity()。getSupportFragmentManager()。beginTransaction()。attach(this).commit(); getActivity()。getSupportFragmentManager()。executePendingTransactions();
11。但是,此时偏好没有更新。在imageview中看到旧的颜色或黑色。
非常感谢你
最后我解决了这个问题,真的很简单。希望这篇文章对任何对动态首选项更新有疑问的人有所帮助。
public class SettingsIncomingSearchFragment extends PreferenceListFragment
implements SharedPreferences.OnSharedPreferenceChangeListener,
PreferenceListFragment.OnPreferenceAttachedListener {
Context ctx ;
public static final String SHARED_PREFS_NAME = "settings";
<-- Begin : i delete these global variables and define them in methods locally and
set the values in methods -->
LinearLayout mainlayout ;
LinearLayout sublayout ;
View view ;
Preference myPref ;
ImageView img ;
SharedPreferences sp ;
<-- End : i delete these global variables and define them in methods locally and
set the values in methods -->
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句