界面布局activity_lan_qiu,代码xml设置如下:

<?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="首页"

                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="社区"

                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="运动"

                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="个人"

                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 类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;


    /**

     * 定位监听

     */

    AMapLocationListener locationListener = new AMapLocationListener() {

        @Override

        public void onLocationChanged(AMapLocation location) {

            if (null != location) {


                StringBuffer sb = new StringBuffer();

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

                    //解析定位结果,

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

                } else {

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

                    sb.append("错误码:" + location.getErrorCode() + "\n");

                }

                Log.e("解析定位结果", city);

                iv_back.setText(location.getCity());

            } else {

                iv_back.setText("定位失败");

            }

            if (aa) {

                XinzhiTianqiData();

                aa = false;

            }

        }

    };



    /**

     * 开始定位

     *

     * @author hongming.wang

     * @since 2.8.0

     */

    private void startLocation() {

        //根据控件的选择,重新设置定位参数

//        resetOption();

        // 设置定位参数

        locationClient.setLocationOption(locationOption);

        // 启动定位

        locationClient.startLocation();

    }


    /**

     * 默认的定位参数

     *

     * @author hongming.wang

     * @since 2.8.0

     */

    private AMapLocationClientOption getDefaultOption() {

        AMapLocationClientOption mOption = new AMapLocationClientOption();

        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式

        mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭

        mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效

        mOption.setInterval(2000);//可选,设置定位间隔。默认为2秒

        mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true

        mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false

        mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用

        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP

        mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false

        mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差

        mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true

        return mOption;

    }


    /**

     * 初始化定位

     *

     * @author hongming.wang

     * @since 2.8.0

     */

    private void initLocation() {

        //初始化client

        locationClient = new AMapLocationClient(getApplicationContext());

        locationOption = getDefaultOption();

        //设置定位参数

        locationClient.setLocationOption(locationOption);

        // 设置定位监听

        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);

    }


//选中首页界面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事件,处理跳转到相应界面

    @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退到后台,我们去玩其他的app,过一段时间回来,这个时候我们的app已经被销毁,我们按多任务键切换回来,发现界面上多个Fragment出现了重叠的情况,这是因为多个Fragment同时显示了,出现了重叠的情况,解决的办法如下:重写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);

    }

}