日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

RadioGroup结合RadioButton使用切换Fragment片段

發(fā)布時(shí)間:2025/3/21 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RadioGroup结合RadioButton使用切换Fragment片段 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

界面布局activity_lan_qiu,代碼xml設(shè)置如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:orientation="vertical">

? ? <include layout="@layout/tool_bar"/>


? ? <LinearLayout

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="match_parent"

? ? ? ? android:background="@color/white"

? ? ? ? android:orientation="vertical">


? ? ? ? <FrameLayout

? ? ? ? ? ? android:id="@+id/fragment_container"

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="0dp"

? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? android:background="@color/white">


? ? ? ? </FrameLayout>


? ? ? ? <RadioGroup

? ? ? ? ? ? android:id="@+id/radioGroup"

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="40dp"

? ? ? ? ? ? android:orientation="horizontal"

? ? ? ? ? ? android:background="@color/colorPrimary"

? ? ? ? ? ? >


? ? ? ? ? ? <RadioButton

? ? ? ? ? ? ? ? android:text="首頁(yè)"

? ? ? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? ? ? android:id="@+id/btn_homepage"

? ? ? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? ? ? android:button="@null"

? ? ? ? ? ? ? ? android:gravity="center"

? ? ? ? ? ? ? ? android:textColor="@color/selector_font_color"

? ? ? ? ? ? ? ? android:textSize="18sp"

? ? ? ? ? ? ? ? />


? ? ? ? ? ? <RadioButton

? ? ? ? ? ? ? ? android:text="社區(qū)"

? ? ? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? ? ? android:id="@+id/btn_message"

? ? ? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? ? ? android:button="@null"

? ? ? ? ? ? ? ? android:gravity="center"

? ? ? ? ? ? ? ? android:textColor="@color/selector_font_color"

? ? ? ? ? ? ? ? android:textSize="18sp"

? ? ? ? ? ? ? ? />


? ? ? ? ? ? <RadioButton

? ? ? ? ? ? ? ? android:text="運(yùn)動(dòng)"

? ? ? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? ? ? android:id="@+id/btn_service"

? ? ? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? ? ? android:button="@null"

? ? ? ? ? ? ? ? android:gravity="center"

? ? ? ? ? ? ? ? android:textColor="@color/selector_font_color"

? ? ? ? ? ? ? ? android:textSize="18sp"

? ? ? ? ? ? ? ? />


? ? ? ? ? ? <RadioButton

? ? ? ? ? ? ? ? android:text="個(gè)人"

? ? ? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? ? ? android:id="@+id/btn_my"

? ? ? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? ? ? android:button="@null"

? ? ? ? ? ? ? ? android:gravity="center"

? ? ? ? ? ? ? ? android:textColor="@color/selector_font_color"

? ? ? ? ? ? ? ? android:textSize="18sp"

? ? ? ? ? ? ? ? />

? ? ? ? </RadioGroup>


? ? </LinearLayout>

</LinearLayout>


android java 類(lèi)LanQiuActivity 代碼如下:


public class LanQiuActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {


? ? private RadioGroup radioGroup;

? ? private RadioButton btn_homepage, btn_message, btn_service, btn_my;


? ? public static final String fragment1Tag = "fragment1";

? ? public static final String fragment2Tag = "fragment2";

? ? public static final String fragment3Tag = "fragment3";

? ? public static final String fragment4Tag = "fragment4";

? ? private AMapLocationClient locationClient = null;

? ? private AMapLocationClientOption locationOption = null;

? ? private TextView tvshare;

? ? private TextView iv_back;

? ? String city;

? ? public static boolean aa = true;

? ? private Toolbar toolbar;


? ? /**

? ? ?* 定位監(jiān)聽(tīng)

? ? ?*/

? ? AMapLocationListener locationListener = new AMapLocationListener() {

? ? ? ? @Override

? ? ? ? public void onLocationChanged(AMapLocation location) {

? ? ? ? ? ? if (null != location) {


? ? ? ? ? ? ? ? StringBuffer sb = new StringBuffer();

? ? ? ? ? ? ? ? if (location.getErrorCode() == 0) {

? ? ? ? ? ? ? ? ? ? //解析定位結(jié)果,

? ? ? ? ? ? ? ? ? ? city = location.getLatitude() + ":" + location.getLongitude();

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? sb.append("定位失敗" + "\n");

? ? ? ? ? ? ? ? ? ? sb.append("錯(cuò)誤碼:" + location.getErrorCode() + "\n");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? Log.e("解析定位結(jié)果", city);

? ? ? ? ? ? ? ? iv_back.setText(location.getCity());

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? iv_back.setText("定位失敗");

? ? ? ? ? ? }

? ? ? ? ? ? if (aa) {

? ? ? ? ? ? ? ? XinzhiTianqiData();

? ? ? ? ? ? ? ? aa = false;

? ? ? ? ? ? }

? ? ? ? }

? ? };



? ? /**

? ? ?* 開(kāi)始定位

? ? ?*

? ? ?* @author hongming.wang

? ? ?* @since 2.8.0

? ? ?*/

? ? private void startLocation() {

? ? ? ? //根據(jù)控件的選擇,重新設(shè)置定位參數(shù)

// ? ? ? ?resetOption();

? ? ? ? // 設(shè)置定位參數(shù)

? ? ? ? locationClient.setLocationOption(locationOption);

? ? ? ? // 啟動(dòng)定位

? ? ? ? locationClient.startLocation();

? ? }


? ? /**

? ? ?* 默認(rèn)的定位參數(shù)

? ? ?*

? ? ?* @author hongming.wang

? ? ?* @since 2.8.0

? ? ?*/

? ? private AMapLocationClientOption getDefaultOption() {

? ? ? ? AMapLocationClientOption mOption = new AMapLocationClientOption();

? ? ? ? mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可選,設(shè)置定位模式,可選的模式有高精度、僅設(shè)備、僅網(wǎng)絡(luò)。默認(rèn)為高精度模式

? ? ? ? mOption.setGpsFirst(false);//可選,設(shè)置是否gps優(yōu)先,只在高精度模式下有效。默認(rèn)關(guān)閉

? ? ? ? mOption.setHttpTimeOut(30000);//可選,設(shè)置網(wǎng)絡(luò)請(qǐng)求超時(shí)時(shí)間。默認(rèn)為30秒。在僅設(shè)備模式下無(wú)效

? ? ? ? mOption.setInterval(2000);//可選,設(shè)置定位間隔。默認(rèn)為2秒

? ? ? ? mOption.setNeedAddress(true);//可選,設(shè)置是否返回逆地理地址信息。默認(rèn)是true

? ? ? ? mOption.setOnceLocation(false);//可選,設(shè)置是否單次定位。默認(rèn)是false

? ? ? ? mOption.setOnceLocationLatest(false);//可選,設(shè)置是否等待wifi刷新,默認(rèn)為false.如果設(shè)置為true,會(huì)自動(dòng)變?yōu)閱未味ㄎ?#xff0c;持續(xù)定位時(shí)不要使用

? ? ? ? AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可選, 設(shè)置網(wǎng)絡(luò)請(qǐng)求的協(xié)議。可選HTTP或者HTTPS。默認(rèn)為HTTP

? ? ? ? mOption.setSensorEnable(false);//可選,設(shè)置是否使用傳感器。默認(rèn)是false

? ? ? ? mOption.setWifiScan(true); //可選,設(shè)置是否開(kāi)啟wifi掃描。默認(rèn)為true,如果設(shè)置為false會(huì)同時(shí)停止主動(dòng)刷新,停止以后完全依賴于系統(tǒng)刷新,定位位置可能存在誤差

? ? ? ? mOption.setLocationCacheEnable(true); //可選,設(shè)置是否使用緩存定位,默認(rèn)為true

? ? ? ? return mOption;

? ? }


? ? /**

? ? ?* 初始化定位

? ? ?*

? ? ?* @author hongming.wang

? ? ?* @since 2.8.0

? ? ?*/

? ? private void initLocation() {

? ? ? ? //初始化client

? ? ? ? locationClient = new AMapLocationClient(getApplicationContext());

? ? ? ? locationOption = getDefaultOption();

? ? ? ? //設(shè)置定位參數(shù)

? ? ? ? locationClient.setLocationOption(locationOption);

? ? ? ? // 設(shè)置定位監(jiān)聽(tīng)

? ? ? ? locationClient.setLocationListener(locationListener);

// ? ? ? ?XinzhiTianqiData();

? ? }


? ? private void XinzhiTianqiData() {

? ? ? ? RequestParams params = new RequestParams();

? ? ? ? params.put("location", city);

? ? ? ? Log.e("天氣XinzhiTianqiData", "天氣XinzhiTianqiData");

? ? ? ? AsyncHttpClientUtil.getInstance().get(Constants.XinzhiTianqi, params, new AsyncHttpResponseHandler() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

? ? ? ? ? ? ? ? String s = new String(responseBody);

// ? ? ? ? ? ? ? ?Log.e("天氣", ""+s);

? ? ? ? ? ? ? ? XinzhiModle xinzhiModle = GsonUtils.parseJSON(s, XinzhiModle.class);

? ? ? ? ? ? ? ? List<XinzhiModle.ResultsBean> results = xinzhiModle.getResults();

? ? ? ? ? ? ? ? String text = results.get(0).getNow().getText();

? ? ? ? ? ? ? ? tvshare.setText("天氣: " + text);

? ? ? ? ? ? }


? ? ? ? ? ? @Override

? ? ? ? ? ? public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

? ? ? ? ? ? }

? ? ? ? });

? ? }


? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? if (savedInstanceState == null) {

? ? ? ? ? ? FragmentManager fragmentManager = getSupportFragmentManager();

? ? ? ? ? ? Fragment fragment = new HomeFragment();

? ? ? ? ? ? fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment, fragment1Tag).commit();

? ? ? ? }

? ? ? ? setContentView(R.layout.activity_lan_qiu);

? ? ? ? toolbar = (Toolbar) findViewById(R.id.tool_bar);

? ? ? ? TextView tool_bar_title = (TextView)findViewById(R.id.tool_bar_title);

? ? ? ? tool_bar_title.setVisibility(View.GONE);

? ? ? ? tvshare = (TextView)findViewById(R.id.tv_share1);

? ? ? ? iv_back = (TextView)findViewById(R.id.iv_back1);

? ? ? ? iv_back.setVisibility(View.VISIBLE);

? ? ? ? tvshare.setVisibility(View.VISIBLE);

? ? ? ? iv_back.setTextSize(22);

? ? ? ? tvshare.setTextSize(22);

? ? ? ? radioGroup = (RadioGroup) this.findViewById(R.id.radioGroup);

? ? ? ? btn_homepage = (RadioButton) this.findViewById(R.id.btn_homepage);

? ? ? ? btn_homepage.setSelected(true);

? ? ? ? btn_message = (RadioButton) this.findViewById(R.id.btn_message);

? ? ? ? btn_service = (RadioButton) this.findViewById(R.id.btn_service);

? ? ? ? btn_my = (RadioButton) this.findViewById(R.id.btn_my);

? ? ? ? initLocation();

? ? ? ? startLocation();

? ? ? ? radioGroup.setOnCheckedChangeListener(this);

? ? }



? ? public static void start(Context context, SzBean szBean) {

? ? ? ? Intent starter = new Intent(context, MainActivity.class);

? ? ? ? starter.putExtra(ParamsKey.EXTRA_SZBEAN, szBean);

? ? ? ? context.startActivity(starter);

? ? }


//選中首頁(yè)界面RadioButton處理事件?

? ? private void HomePageSelect() {

? ? ? ? btn_homepage.setSelected(true);

? ? ? ? btn_message.setSelected(false);

? ? ? ? btn_service.setSelected(false);

? ? ? ? btn_my.setSelected(false);

? ? }


//選中信息界面RadioButton處理事件?

? ? private void MessageSelect() {

? ? ? ? btn_homepage.setSelected(false);

? ? ? ? btn_message.setSelected(true);

? ? ? ? btn_service.setSelected(false);

? ? ? ? btn_my.setSelected(false);

? ? }


//選中記錄RadioButton處理事件?

? ? private void RecordSelect() {

? ? ? ? btn_homepage.setSelected(false);

? ? ? ? btn_message.setSelected(false);

? ? ? ? btn_service.setSelected(true);

? ? ? ? btn_my.setSelected(false);

? ? }


//選中我的界面RadioButton處理事件?

? ? private void MySelect() {

? ? ? ? btn_homepage.setSelected(false);

? ? ? ? btn_message.setSelected(false);

? ? ? ? btn_service.setSelected(false);

? ? ? ? btn_my.setSelected(true);

? ? }



//在RadioGroup中切換改變選中RadioButton事件,處理跳轉(zhuǎn)到相應(yīng)界面

? ? @Override

? ? public void onCheckedChanged(RadioGroup radioGroup, int i) {

? ? ? ? FragmentManager fm = getSupportFragmentManager();

? ? ? ? FragmentTransaction ft = fm.beginTransaction();

? ? ? ? Fragment fragment1 = fm.findFragmentByTag(fragment1Tag);

? ? ? ? Fragment fragment2 = fm.findFragmentByTag(fragment2Tag);

? ? ? ? Fragment fragment3 = fm.findFragmentByTag(fragment3Tag);

? ? ? ? Fragment fragment4 = fm.findFragmentByTag(fragment4Tag);

? ? ? ? if (fragment1 != null) {

? ? ? ? ? ? ft.hide(fragment1);

? ? ? ? }

? ? ? ? if (fragment2 != null) {

? ? ? ? ? ? ft.hide(fragment2);

? ? ? ? }

? ? ? ? if (fragment3 != null) {

? ? ? ? ? ? ft.hide(fragment3);

? ? ? ? }

? ? ? ? if (fragment4 != null) {

? ? ? ? ? ? ft.hide(fragment4);

? ? ? ? }

? ? ? ? switch (i) {

? ? ? ? ? ? case R.id.btn_homepage:

? ? ? ? ? ? ? ? toolbar.setVisibility(View.VISIBLE);

? ? ? ? ? ? ? ? if (fragment1 == null) {

? ? ? ? ? ? ? ? ? ? fragment1 = new HomeFragment();

? ? ? ? ? ? ? ? ? ? ft.add(R.id.fragment_container, fragment1, fragment1Tag);

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ft.show(fragment1);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? HomePageSelect();

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case R.id.btn_message:

? ? ? ? ? ? ? ? toolbar.setVisibility(View.VISIBLE);

? ? ? ? ? ? ? ? if (fragment2 == null) {

? ? ? ? ? ? ? ? ? ? fragment2 = new BBsFragment();

? ? ? ? ? ? ? ? ? ? ft.add(R.id.fragment_container, fragment2, fragment2Tag);

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ft.show(fragment2);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? MessageSelect();

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case R.id.btn_service:

? ? ? ? ? ? ? ? toolbar.setVisibility(View.VISIBLE);

? ? ? ? ? ? ? ? if (fragment3 == null) {

? ? ? ? ? ? ? ? ? ? fragment3 = new SportFragment();

? ? ? ? ? ? ? ? ? ? ft.add(R.id.fragment_container, fragment3, fragment3Tag);

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ft.show(fragment3);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? RecordSelect();

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case R.id.btn_my:

? ? ? ? ? ? ? ? toolbar.setVisibility(View.GONE);

? ? ? ? ? ? ? ? if (fragment4 == null) {

? ? ? ? ? ? ? ? ? ? fragment4 = new PersonalCenteFragment();

? ? ? ? ? ? ? ? ? ? ft.add(R.id.fragment_container, fragment4, fragment4Tag);

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ft.show(fragment4);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? MySelect();

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? ? ? ft.commit();

? ? }


? ? //app退到后臺(tái),我們?nèi)ネ嫫渌腶pp,過(guò)一段時(shí)間回來(lái),這個(gè)時(shí)候我們的app已經(jīng)被銷(xiāo)毀,我們按多任務(wù)鍵切換回來(lái),發(fā)現(xiàn)界面上多個(gè)Fragment出現(xiàn)了重疊的情況,這是因?yàn)槎鄠€(gè)Fragment同時(shí)顯示了,出現(xiàn)了重疊的情況,解決的辦法如下:重寫(xiě)Activity的onRestoreInstanceState方法

? ? @Override

? ? protected void onRestoreInstanceState(Bundle savedInstanceState) {

? ? ? ? super.onRestoreInstanceState(savedInstanceState);

? ? ? ? for (int i = 0; i < radioGroup.getChildCount(); i++) {

? ? ? ? ? ? RadioButton mTab = (RadioButton) radioGroup.getChildAt(i);

? ? ? ? ? ? FragmentManager fm = getSupportFragmentManager();

? ? ? ? ? ? Fragment fragment = fm.findFragmentByTag((String) mTab.getTag());

? ? ? ? ? ? FragmentTransaction ft = fm.beginTransaction();

? ? ? ? ? ? if (fragment != null) {

? ? ? ? ? ? ? ? if (!mTab.isChecked()) {

? ? ? ? ? ? ? ? ? ? ft.hide(fragment);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? ft.commit();

? ? ? ? }

? ? }


? ? private long mExitTime;



? ? //按back提示再按一次退出


? ? public boolean onKeyDown(int keyCode, KeyEvent event) {

? ? ? ? if (keyCode == KeyEvent.KEYCODE_BACK) {

? ? ? ? ? ? if ((System.currentTimeMillis() - mExitTime) > 2000) {

? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "再按一次退出", Toast.LENGTH_SHORT).show();

? ? ? ? ? ? ? ? mExitTime = System.currentTimeMillis();

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? finish();

? ? ? ? ? ? ? ? System.exit(0);

? ? ? ? ? ? }

? ? ? ? ? ? return true;

? ? ? ? }

? ? ? ? return super.onKeyDown(keyCode, event);

? ? }

}




轉(zhuǎn)載于:https://blog.51cto.com/1206995290qq/1945137

總結(jié)

以上是生活随笔為你收集整理的RadioGroup结合RadioButton使用切换Fragment片段的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。