2017-05-26 93 views
-1

底線是,有一個主要活動,並且4個片段其中一個片段是類似設置的東西。 在具有RadioGroup和3個RadioButton設置的片段標記中,其位置必須對應於起始片段,即第一個,第二個或第三個片段。有必要確保當應用程序關閉時,按下的RadioButtons的位置被保存,並根據位置顯示所需的片段。記住RadioButton的位置

Code活動

public class Main2Activity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    FragmentRoute froute; 
    FragmentSearch fSearch; 
    FragmentHelp fHelp; 
    FragmentSearchRoute fSearchRoute; 
    FragmentSettings fSettings; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main2); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.addDrawerListener(toggle); 
     toggle.syncState(); 
     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     fSearch = new FragmentSearch(); 
     froute = new FragmentRoute(); 
     fHelp = new FragmentHelp(); 
     fSettings = new FragmentSettings(); 
     fSearchRoute = new FragmentSearchRoute(); 
     getFragmentManager().beginTransaction().replace(R.id.container, new FragmentRoute(), "test").commit(); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 


    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 

     FragmentTransaction ft = getFragmentManager().beginTransaction(); 

     int id = item.getItemId(); 

     if (id == R.id.route) { 
      ft.replace(R.id.container, froute); 
     } else if (id == R.id.search) { 
      ft.replace(R.id.container, fSearch); 

     } else if (id == R.id.search_route) { 
      ft.replace(R.id.container, fSearchRoute); 
     } else if (id == R.id.settings) { 
      ft.replace(R.id.container, fSettings); 
     } else if (id == R.id.help) { 
      ft.replace(R.id.container, fHelp); 
     }ft.commit(); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

代碼片段的設置

public class FragmentSettings extends PreferenceFragment { 

    RadioGroup radioGroup; 
    SharedPreferences sharedPreferences; 
    RadioButton radioButton1,radioButton2,radioButton3; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     getActivity().setTitle(R.string.fr5); 
     View rv = inflater.inflate(R.layout.fragment_settings, container, false); 
     radioGroup = (RadioGroup) rv.findViewById(R.id.radioGroup); 
     radioButton1 = (RadioButton) rv.findViewById(R.id.radioButton1); 
     radioButton2 = (RadioButton) rv.findViewById(R.id.radioButton2); 
     radioButton3 = (RadioButton) rv.findViewById(R.id.radioButton3); 


     return rv; 
    } 

} 
+0

在'sharedPreferences' radiobutton'的'保存位置。 – Wizard

+0

需要一個使用示例 – Zdenek

+0

https://developer.android.com/training/basics/data-storage/shared-preferences.html – Wizard

回答

0

1.使用SharedPreferences存儲所選RadioButton位置。

public class FragmentSettings extends PreferenceFragment { 

    RadioGroup radioGroup; 
    RadioButton radioButton1,radioButton2,radioButton3; 

    SharedPreferences sharedPreferences; 
    SharedPreferences.Editor editor; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     getActivity().setTitle(R.string.fr5); 
     View rv = inflater.inflate(R.layout.fragment_settings, container, false); 
     radioGroup = (RadioGroup) rv.findViewById(R.id.radioGroup); 
     radioButton1 = (RadioButton) rv.findViewById(R.id.radioButton1); 
     radioButton2 = (RadioButton) rv.findViewById(R.id.radioButton2); 
     radioButton3 = (RadioButton) rv.findViewById(R.id.radioButton3); 

     sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     editor = sharedPreferences.edit(); 

     // Required to get the checked RadioButton id 
     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 

       int selectedPosition; 

       switch (checkedId) { 
        case R.id.radioButton1: 
         selectedPosition = 1; 
         break; 
        case R.id.radioButton2: 
         selectedPosition = 2; 
         break; 
        case R.id.radioButton3: 
         selectedPosition = 3; 
         break; 
        default: 
         selectedPosition = 1; 
         break; 
       } 

       // Store in SharedPreferences 
       editor.putInt("SELECTED_POSITION", selectedPosition); 
       editor.commit(); 
      } 
     }); 

     return rv; 
    } 
} 

2.在你Main2ActivityonCreate()方法從SharedPreferences得到SELECTED_POSITION值並顯示您所需Fragment

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

    ........ 
    ............... 

    fSearch = new FragmentSearch(); 
    froute = new FragmentRoute(); 
    fHelp = new FragmentHelp(); 
    fSettings = new FragmentSettings(); 
    fSearchRoute = new FragmentSearchRoute(); 

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 

    int selectedPosition = sharedPreferences.getInt("SELECTED_POSITION", 1); 

    switch (selectedPosition) { 
     case 1: 
      // FragmentSearch 
      getFragmentManager().beginTransaction().replace(R.id.container, fSearch).commit();    
      break; 
     case 2: 
      // FragmentRoute 
      getFragmentManager().beginTransaction().replace(R.id.container, froute).commit();    
      break; 
     case 3: 
      // FragmentHelp 
      getFragmentManager().beginTransaction().replace(R.id.container, fHelp).commit();    
      break;      
     default: 
      getFragmentManager().beginTransaction().replace(R.id.container, fSearch).commit(); 
      break; 
    } 

} 

希望這將有助於〜

+0

非常感謝你,我很高興你幫助了我。 – Zdenek

+0

高興地幫助你:) – FAT