android的RadioButton切换不同的fragment
来源:互联网 发布:php 比特比交易平台 编辑:程序博客网 时间:2024/05/16 05:17
MainActivity.java
package com.fragmentwechattest;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends Activity implements OnCheckedChangeListener {private FragmentTransaction beginTransaction;private FragmentManager fragmentManager;private WeFragment weFragment;private ContextFragment contextFragment;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RadioGroup radioGroup=(RadioGroup) findViewById(R.id.my_radio_group);radioGroup.setOnCheckedChangeListener(this);fragmentManager = getFragmentManager();weFragment = new WeFragment();contextFragment = new ContextFragment();/*RadioButton radioButton = (RadioButton) radioGroup.getChildAt(0);radioButton.setChecked(true);*/RadioButton radioButton=(RadioButton) findViewById(R.id.my_radiobtn_we);radioButton.setChecked(true);}@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch(checkedId){case R.id.my_radiobtn_we://新开一个事务beginTransaction = fragmentManager.beginTransaction();beginTransaction.replace(R.id.frag_container,weFragment);beginTransaction.commit();break;case R.id.my_radiobtn_context:beginTransaction = fragmentManager.beginTransaction();beginTransaction.replace(R.id.frag_container,contextFragment);beginTransaction.commit();break;default:break;}}}
WeFragment.java
package com.fragmentwechattest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class WeFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubreturn inflater.inflate(R.layout.we_fragment, container, false);}}activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.fragmentwechattest.MainActivity" > <FrameLayout android:id="@+id/frag_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" ></FrameLayout> <RadioGroup android:id="@+id/my_radio_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="5dp" > <RadioButton android:id="@+id/my_radiobtn_we" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:drawableTop="@drawable/we_selector" android:gravity="center" android:text="we" android:button="@null" /> <RadioButton android:id="@+id/my_radiobtn_context" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:drawableTop="@drawable/context_selector" android:gravity="center" android:text="context" android:button="@null" /> <RadioButton android:id="@+id/my_radiobtn_find" android:layout_width="0dp" android:layout_weight="1" android:gravity="center" android:layout_height="wrap_content" android:drawableTop="@drawable/find_selector" android:text="find" android:button="@null" /> <RadioButton android:id="@+id/my_radiobtn_me" android:layout_width="0dp" android:layout_weight="1" android:gravity="center" android:layout_height="wrap_content" android:drawableTop="@drawable/me_selector" android:text="me" android:button="@null" /> </RadioGroup> </LinearLayout>content_frag.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" android:background="#B4CDE6" > <TextView android:id="@+id/my_wechat_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="content" /></LinearLayout>
we_fragment.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" android:background="#f00" > <TextView android:id="@+id/my_wechat_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="wechart" /></LinearLayout>
0 0
- android的RadioButton切换不同的fragment
- RadioGrop嵌套Radiobutton 切换不同的 fragment
- 底部放5个RadioButton,点击切换不同的Fragment
- 不同的fragment切换不同的actionbar.
- Android开发,在Activity中切换不同的Fragment
- android 点击按钮切换不同的fragment布局
- android 点击底部图片进行切换不同的Fragment
- android fragment的切换
- 解决RadioButton+FragmentPagerAdapter+Fragment切换页面数据加载的问题
- Android开发,Slidingmenu+Fragment框架中,通过menu切换不同Fragment卡顿的解决方法
- RadioButton实现Fragment切换
- RadioGroup+RadioButton切换Fragment
- android使用radioButton 切换fragment 解决fragment叠影
- RadioGroup和Fragment相结合实现点击RadioButton进行Fragment的切换
- android不同Fragment有不同的菜单
- Android中横竖屏切换加载不同布局导致fragment数据丢失的问题
- android TabHost的一种用RadioButton来实现的切换
- android 底部tabs切换,支持多层嵌套RadioButton的RadioGroup
- 01课程介绍-DirectX游戏开发初级教程
- ES6学习——新的语法:Tagged Templates
- [NOI2014]魔法森林|动态树
- @synthesize & @property Xcode 4.4之前...
- 后缀数组应用——多个字符串的相关问题
- android的RadioButton切换不同的fragment
- 从电脑与人的角度谈工作
- Java 到底是在哪里被运用的呢?
- RecyclerView 视频播放列表 用到 retrofit-convert,fresco,SurfaceView
- 自定义Framework出现:Could not build module ‘xxx’的错误提示
- 数据库操作增删改查
- java中Math的一些用法
- PHP操作数据库
- Machine Learning 101