0

我有我的代碼,其中,我有2個複選框。當其中一個被選中時,另一個未被選中,反之亦然。它工作正常,除了當我點擊這個片段上的後退按鈕時,它以某種方式保存了最後選中的複選框的狀態(不會像更新內容那樣按照預期完成,只有當我點擊完成按鈕時才能完成)。另外,當我提到我的頁面或更改我的平板電腦方向時,它會恢復爲默認頁面(在本例中爲個人列表)。我想讓它工作,不僅保存所選複選框的狀態,而且也保存相關的片段內容。另外,點擊後,它不應該保存任何東西,除非我點擊完成按鈕。我知道這是在共享管理者手中完成的,但我不知道如何去做,以及在哪裏打電話,有沒有人有線索?這裏是我的代碼相同:如何保存所選複選標記的狀態?

public class Listfragment extends Fragment implements OnClickListener,OnCheckedChangeListener { 
     public final static String TAG_MANAGE_LIST_CATEGORIES_FRAGMENT = "Listfragment"; 
     private boolean mIsPersonal = true; 
     private boolean mIsShared = true; 
     public static final String ANIMATION = "animation"; 
     private boolean mShouldbeon; 
     private boolean mInitialShouldbeon; 
     private boolean mShouldbeon1; 
     private boolean mInitialShouldbeon1; 
     protected Button mPreferencesDoneButton; 
     private SharedPreferences sp; 
     final boolean isPersonal = true; 
     final boolean isShared = true; 
     Button sharedbutton; 
     Button personalbutton; 
     private ListsFragment mListsFragment; 
     ToggleButton one; 
     ToggleButton two; 


     @Override 
     public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { 
      final View view = inflater.inflate(R.layout.fragment_manage_lists, container, false); 
      final Bundle arguments = getArguments(); 
      final ListActivity activity = (ListActivity) getActivity(); 


      return view; 
     } 
     protected void setupClickListeners() { 
      mIsPersonal = isPersonal; 
      mPreferencesDoneButton = (Button) getActivity().findViewById(R.id.button_done); 
      Typeface face = Typeface.createFromAsset(mPreferencesDoneButton.getContext().getAssets(), 
        "fonts/proxima-nova-regular.ttf"); 
      mPreferencesDoneButton.setTypeface(face); 
      mPreferencesDoneButton.setOnClickListener(this); 
      mPreferencesDoneButton.setEnabled(((ListActivity) getActivity()).isDoneButtonEnabled()); 
     } 
     @Override 
     public void onActivityCreated(final Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 

      final TextView titleView = (TextView) getActivity().findViewById(R.id.actionbar_title); 
      titleView.setText(R.string.manage_dashboard_lists); 

      initListfragment(); 

     } 

     private void initListfragment() { 
      populateData(); 
      setupClickListeners(); 
      sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); 

      personalbutton = (Button) getActivity().findViewById(R.id.button_personal_list); 
      personalbutton.setOnClickListener(this); 
      sharedbutton = (Button) getActivity().findViewById(R.id.button_shared_list); 
      sharedbutton.setOnClickListener(this); 
      one = (ToggleButton) getView().findViewById(R.id.personal_list_toggle_control); 
      one.setOnCheckedChangeListener(this); 
      Log.d("LISTY", "1"); 
      boolean toogle = sp.getBoolean("toggle", true); 
      two = (ToggleButton) getView().findViewById(R.id.shared_list_toggle_control); 
      two.setOnCheckedChangeListener(this); 
      Log.d("LISTY", "2"); 

      if(toogle){ 
       one.setChecked(true); 
       Log.d("LISTY", "3"); 
      } 
       else{ 
       two.setChecked(true); 
       Log.d("LISTY", "4"); 
       } 


     } 
     protected void populateData() { 
      SharedPreferencesManager.getInstance().updateIsUserPreferencesUpdated(false); 

     } 

     @Override 
     public void onClick(final View view) { 
      final ListActivity activity = (ListActivity) getActivity(); 

      switch (view.getId()) { 
       case R.id.button_personal_list: 
        mIsPersonal = true; 
        mIsShared = false; 
        one.setChecked(true); 
        Log.d("LISTY", "5"); 

        sp.edit().putBoolean("toggle", mIsPersonal).commit(); 
        Log.d("listdash", "Personal List is selected in onclick : "+mIsPersonal); 

        return; 

       case R.id.button_shared_list: 
        mIsPersonal = false; 
        mIsShared = true; 
        two.setChecked(true); 
        sp.edit().putBoolean("toggle", !mIsShared).commit(); 
        Log.d("listdash", "Shared List is selected in onclick : "+mIsShared); 
        Log.d("LISTY", "6"); 
        return; 


       case R.id.button_done: 
        Log.d("LISTY", "7"); 
        boolean tabletSize = Utils.isTablet(activity); 
        int layoutType = Application.getAppResources().getInteger(R.integer.layout_type); 

          if(one.isChecked()){ 
           DashboardFragment.getInstance().getListsFragment().setIsPersonal(ListsFragment.LIST_TYPE.PERSONAL); 
           Log.d("LISTY", "8"); 
          }else{ 
           DashboardFragment.getInstance().getListsFragment().setIsPersonal(ListsFragment.LIST_TYPE.SHARED); 
           Log.d("LISTY", "9"); 

        } 
        break; 

       default: 

        break; 
      } 

      activity.onBackPressed(); 
      Log.d("LISTY", "10"); 
     } 


     protected void toggleDoneButton() { 
      boolean isUserPreferencesUpdated = SharedPreferencesManager.getInstance().isUserPreferencesUpdated(); 
      Log.d("LISTY", "13"); 
      boolean isDoneEnabled = (
        mShouldbeon != mInitialShouldbeon || mShouldbeon1 != mInitialShouldbeon1 
        || isUserPreferencesUpdated); 
      mPreferencesDoneButton.setEnabled(isDoneEnabled); 
      Log.d("LISTY", "14"); 

     } 

     public void onUpdate() { 
      mListsFragment.onUpdate(); 
      Log.d("LISTY", "15"); 
     } 


     @Override 
     public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { 

      if(isChecked){ 
       if (buttonView == one) { 
         two.setChecked(false); 
         one.setEnabled(false); 
         two.setEnabled(true); 
         Log.d("LISTY", "18"); 
         personalbutton.setEnabled(false); 
         sharedbutton.setEnabled(true); 
         sp.edit().putBoolean("toggle", true).commit(); 
         Log.d("listdash", "Personal List is selected in onchecked : "+mIsPersonal +one +two); 

        } 
       else if (buttonView == two) { 
         one.setChecked(false); 
         two.setEnabled(false); 
         one.setEnabled(true); 
         Log.d("LISTY", "19"); 
         personalbutton.setEnabled(true); 
         sharedbutton.setEnabled(false); 
         sp.edit().putBoolean("toggle", false).commit(); 
         Log.d("listdash", "Shared List is selected in oncheck : "+mIsShared +one +two); 

        } 
       } 
      switch (buttonView.getId()) { 

       case R.id.personal_list_toggle_control: 

        mShouldbeon = isChecked; 
        Log.d("LISTY", "20"); 

        break; 
       case R.id.shared_list_toggle_control: 

        mShouldbeon1 = isChecked; 
        Log.d("LISTY", "21"); 

        break; 

       default: 
        break; 
      } 
      toggleDoneButton(); 
     } 

謝謝! P.S:如果你能解釋我的代碼,這將是非常好的。

回答

-3

可以放在你的onResume代碼,那麼你的應用程序會根據自己的需要

@Override 
public void onResume(){ 
    super.onResume(); 
    // put your code here... 

} 
+0

什麼代碼工作?你能展示我的方面嗎? – 2014-10-20 15:13:44