Android 实现点击两个按钮,原地切换界面

布局代码
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/console_line_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioGroup android:id="@+id/main_tab_group" android:background="@color/white" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_tab_useCabage" android:background="#00D4A0" android:padding="10dp" android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" android:checked="true" android:textSize="16sp" android:button="@null" android:text="消费" /> <RadioButton android:id="@+id/rb_tab_chongzhiCabage" android:padding="10dp" android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" android:button="@null" android:textSize="16sp" android:text="充值" /> </RadioGroup> </LinearLayout> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" android:visibility="gone" /> </LinearLayout> </TabHost>
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364逻辑代码
布局主要采用的TabHost+RadioButon实现切换FrameLayout来切换内容页面。在主代码中让Activity extends TabActivity ,这样主要使用其getTabHost()方法;通过RadioButton点击事件中设置tabHost.setCurrentTabByTag(“use”);通过Tag值关联到TabHost的创建的新的类页面:tabHost.addTab(tabHost.newTabSpec(“use”).setIndicator(“use”).setContent(new Intent(this, XiaoFeiCabageActivity.class)));从而实现了点击按钮,“原地”切换页面的功能,public class XiaoFeiWater extends TabActivity implements OnClickListener { TabHost tabHost; private RadioButton rb_tab_useCabage,rb_tab_chongzhiCabage; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_xiaofei_water); initTab(); init(); } /** * 初始化数据 */ private void init() { rb_tab_useCabage = (RadioButton) findViewById(R.id.rb_tab_useCabage); rb_tab_chongzhiCabage = (RadioButton) findViewById(R.id.rb_tab_chongzhiCabage); rb_tab_useCabage.setOnClickListener(this); rb_tab_chongzhiCabage.setOnClickListener(this); } /** * 初始化Tab */ private void initTab() { tabHost=getTabHost(); tabHost.addTab(tabHost.newTabSpec("use").setIndicator("use") .setContent(new Intent(this, XiaoFeiCabageActivity.class))); tabHost.addTab(tabHost.newTabSpec("chongzhi").setIndicator("chongzhi") .setContent(new Intent(this, ChongZhiCabageActivity.class))); } /** * 点击事件 * @param v */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.rb_tab_useCabage: tabHost.setCurrentTabByTag("use"); break; case R.id.rb_tab_chongzhiCabage: tabHost.setCurrentTabByTag("chongzhi"); break; default: break; } }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455相关知识
Android 实现点击两个按钮,原地切换界面
Android 实现应用内语言切换(包括不重启应用方式)
Android Studio移动应用开发——onCreate、onSaveInstanceState、onRestoreInstance一个参数和两个参数
Android Studio实现简单的购物商城界面
用JavaScript实现图片切换功能:项目实例解析
揭秘Android系统中的小花界面:探索独特设计背后的秘密与挑战
Android百度地图 关于visibility=“gone”的奇葩问题
移动应用开发做登录界面(app手机登录界面)
Android入门开发经验
【朝花夕拾】Android性能篇之(六)Android进程管理机制
网址: Android 实现点击两个按钮,原地切换界面 https://www.huajiangbk.com/newsview2442058.html
| 上一篇: 一个反常识的真相:疯狂挣钱=不断 |
下一篇: 花椒树种植技术3要点,让您种植花 |
推荐分享
- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039
