android radiobutton切换,RadioGroup结合RadioButton使用切换Fragment片段

2023-05-16

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/white"

android:orientation="vertical">

android:id="@+id/fragment_container"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:background="@color/white">

android:id="@+id/radioGroup"

android:layout_width="match_parent"

android:layout_height="40dp"

android:orientation="horizontal"

android:background="@color/colorPrimary"

>

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"

/>

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"

/>

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"

/>

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"

/>

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

}

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android radiobutton切换,RadioGroup结合RadioButton使用切换Fragment片段 的相关文章

  • 如何在 Android 中获取像素颜色

    我正在使用 Intent 调用并显示图库中的图像 现在我可以使用以下命令在 TextView 中获取图像的坐标 final TextView textView TextView findViewById R id textView fina
  • 排除jar中的文件进入apk

    我最近添加了一些新的罐子到我的android项目 一些 jar 包含 version properties 其中之一甚至包含 README TXT 我如何告诉 eclipse adt ant 排除文件进入 apk 显然我可以解压 apk 删
  • 如何将 JRadioButton 组与模型一起使用

    有没有什么方法可以将一组 JRadioButtons 与数据模型关联起来 以便更容易判断选择了哪个按钮 如果有 在理想的世界中 我想将一组 N 个单选按钮与一个enum类有一个NONEvalue 和与每个单选按钮关联的一个值 我解决了我自己
  • 谷歌地图 API 的替代品 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 Locked 这个问题及其答案是locked help locked posts因为这个问题是题外话 但却具有历史意义 目前不接受新的答案
  • Eclipse 运行时 Dalvik 错误

    当我要运行任何程序时 会显示此对话框 在 Eclipse 中 这些错误显示在控制台中 2013 02 25 19 05 09 Dex Loader Unable to execute dex Target out of range 0000
  • Android Studio 3.1.3不显示布局设计预览

    自从我上次在 android studio 中创建一个新项目以来已经有一段时间了 今天当我这样做时 android studio 没有从布局预览中显示任何内容 它是一个空白窗口 并显示 没有可显示的内容 我已经尝试过了 更新了sdks工具
  • 如何发送超过160个字符的短信?

    如何在 Android 中发送大短信 我用了 SmsManager sms SmsManager getDefault sms sendTextMessage contactNos j null msgs i sentPI delivere
  • 使用数据绑定更新对象类型 LiveData

    我想通过与 livedata 的数据绑定来更新视图 让我们看一下场景 数据类 data class Movie var name String var createdAt String 视图模型 class MyViewModel View
  • 安卓写入文件

    经过几周的不编程之后 我决定完成我的应用程序 上次我无法进行文件写入和读取 现在我想做 我也许可以使用数据库 但这似乎容易得多 我已经发现this http narenst wordpress com 2010 01 25 android
  • Android 和 Facebook 共享意图

    我正在开发一个 Android 应用程序 并且有兴趣了解如何使用 Android 的共享意图在应用程序内更新应用程序用户的状态 浏览过 Facebook 的 SDK 后 这似乎很容易做到 但是我很想允许用户通过常规的共享意图弹出窗口来做到这
  • 如何从 Android 服务获取应用程序上下文?

    我有一个正在运行并监听麦克风输入的 Android 服务 我希望它在满足特定条件时启动一项活动 为了创建意图 我需要应用程序上下文 我怎么才能得到它 Intent i new Intent ctx SONR class i addFlags
  • GPSTracker 类不工作

    我尝试在我的应用程序中使用我在网上找到的 GPSTracker 类 并且我之前让它工作过 但现在似乎莫名其妙地不起作用 public class GPSTracker extends Service implements LocationL
  • 动态添加的 RemoteView 上的布局权重

    在我的小部件中 我使用以下内容将项目 R layout widget item 动态添加到我的主小部件布局中定义的 LinearLayout 中 Main widget layout RemoteViews views new Remote
  • 在 Volley 中更新 UI 最有效的方法是什么

    最近我在 android 中使用 Volley 库 它工作得很好 但我想知道更新 UI 的最有效方法 我有一个包含所有 Volley 方法的 Utils 类 现在我传递了所有视图将作为参数更新 但我读到我可以在活动中实现侦听器 然后将它们作
  • 带有选项卡和 ActivityGroup 的后退按钮行为

    我有一个活动 Main 显示如下选项卡 private void initTabs mTabHost getTabHost The activity TabHost Intent intent intent new Intent setCl
  • 为什么 CheckBox 检查不能以编程方式与 Kotlin 一起使用?

    我想这个问题以前可能有人问过 但这个问题也发生在我身上 所以我在这里再次询问 看看我们能否找到解决方案 所以基本上问题是以编程方式检查复选框不与 Kotlin 代码一起工作 为了解释一下 我正在分享我的代码和问题的屏幕截图 filterCo
  • 滑动抽屉上的按钮? - 安卓

    好吧 我已经在我正在构建的 Android 应用程序中的滑动抽屉上实现了一个按钮 唯一的问题是 当我按下按钮时 整个滑动抽屉都会被按下并向上滑动 我知道我可以在 XML 中禁用 按向上滑动 但这似乎不起作用 因为滑动抽屉仍然在没有向上滑动的
  • FirebaseAuth.getInstance().signOut() 不注销

    我尝试从 firebase 注销用户 但在关闭应用程序并再次打开后 用户仍然处于连接状态 我尝试从 firebase 定期注销用户 但没有解决问题 我想知道是什么导致了这个问题 logout setOnClickListener new V
  • okHttp3 java.lang.NoSuchMethodError:没有虚拟方法 setCallWebSocket

    我已从 okhttp Retrofit 更新到 okhttp3 Retrofit2 但我的应用程序因此异常而无法启动 FATAL EXCEPTION EventThread Process appli speaky com PID 1470
  • 如何使用 androidX 不破坏片段

    现在 我尝试 androidX 导航和底部导航栏 当我像下面一样使用它时 supportFragmentManager getSupportFragmentManager navHostFragment NavHostFragment su

随机推荐