底線是,有一個主要活動,並且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;
}
}
在'sharedPreferences' radiobutton'的'保存位置。 – Wizard
需要一個使用示例 – Zdenek
https://developer.android.com/training/basics/data-storage/shared-preferences.html – Wizard