android RadioGroup+TabHost实现导航栏
2014-07-07 15:41
288 查看
1、对于 RadioGroup,是RadioButton的一个组设置,可以实现单选效果。 对于RadioGroup的设置监听setOnCheckedChangeListener(OnCheckedChangeListener listener); OnCheckedChangeListener 是一个接口,接口函数为:
public void onCheckedChanged(RadioGroup group, int checkedId); 设置监听后,实现接口,就可以得到 RadioGroup 的对象和RadioGroup 内的RadioButton的checkedId,从而实现操作。
点击 RadioButton能响应onCheckedChanged,是因为同组内旧checkedId和新的checkedId不同,有时候我们在XML的布局文件中已经把该RadioButton的android:checked = "true"设置了,如果你在初始化定义此RadioButton的setChecked为true,可能就无法响应onCheckedChanged 了。因为我在做程序的时候,为了统一方便,把很多功能都设置在onCheckedChanged中,这样当set变化后,就能自动执行了,所以当我们已经不小心在布局文件中设置了,可以在初始化中再次实现setChecked为true的一些动作了。
我也是在debug的时候发现一些问题,所以要理解接口,只有在CheckedChanged的时候,才会有响应动作。
2、对于TabHost也是同理的,他是管理不同个布局显示的tab控制器。但是会更复杂一些,以为你需要添加布局到TabHost中,void addTab(TabSpec tabSpec),但是要注意的是,在实现addTab时候,已经默认显示第一个tab,如果你要设置显示tab,可以用TabHost对象 setCurrentTab(mCurrentTab),对于TabHost的监听
onTabChanged也是当tab变化是才能够响应的,在onTabChanged完成一些动作是要记住TabHost默认现实第一个tab页,所以当你初始化setCurrentTab(0),其实没有完成onTabChanged中对应id的操作。
-------------------------------------------------------------------
布局文件
<?xml version="1.0" encoding="UTF-8"?>
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<RadioGroup
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:id="@+id/main_radio"
android:background="@drawable/maintab_toolbar_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_button0"
android:tag="radio_button0"
android:layout_marginTop="2.0dip"
android:text="@string/alarm"
android:drawableTop="@drawable/icon_1"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button1"
android:tag="radio_button1"
android:layout_marginTop="2.0dip"
android:text="@string/message"
android:drawableTop="@drawable/icon_2"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button2"
android:tag="radio_button2"
android:layout_marginTop="2.0dip"
android:text="@string/photo"
android:drawableTop="@drawable/icon_3"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button3"
android:tag="radio_button3"
android:layout_marginTop="2.0dip"
android:text="@string/music"
android:drawableTop="@drawable/icon_4"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button4"
android:tag="radio_button4"
android:layout_marginTop="2.0dip"
android:text="@string/setting"
android:drawableTop="@drawable/icon_5"
style="@style/main_tab_bottom" />
</RadioGroup>
</LinearLayout>
</TabHost>
需要注意的是,如果用TabHost这个控件,其中有几个ID是必须这么写的,android:id="@android:id/tabhost ;android:id="@android:id/tabcontent" ;android:id="@android:id/tabs" ;之所以要这么写是因为在TabHost这个类中。需要实例化上述这个ID的控件。看源码:
在TabActivity中有这么个方法:
当内容发生改变时它会调用这个方法,来更新列表或者其他视图,而这个方法中需要实例化TabHost,所以必须通过ID为tabhost实例化。
再看看TabHost这个类中,
这个方法,是在增加选项卡之前由系统调用。在这个方法中需要通过tabs 这个ID实例化一个TabWidget,通过tabcontent这个ID实例化一个FrameLayout,用来放置选项卡内容。所以这两个ID也是固定的。
在上述布局文件中隐藏了系统默认的Widget,取而代之的是带有图片的Button。
看一下主要代码:
package com.iteye.androidtoast;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
public class MainActivity extends TabActivity implements OnCheckedChangeListener{
/** Called when the activity is first created. */
private TabHost mHost;
private RadioGroup radioderGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maintabs);
//实例化TabHost
mHost=this.getTabHost();
//添加选项卡
mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE")
.setContent(new Intent(this,OneActivity.class)));
mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO")
.setContent(new Intent(this,TwoActivity.class)));
mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE")
.setContent(new Intent(this,ThreeActivity.class)));
mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR")
.setContent(new Intent(this,FourActivity.class)));
mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE")
.setContent(new Intent(this,FiveActivity.class)));
radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
radioderGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio_button0:
mHost.setCurrentTabByTag("ONE");
break;
case R.id.radio_button1:
mHost.setCurrentTabByTag("TWO");
break;
case R.id.radio_button2:
mHost.setCurrentTabByTag("THREE");
break;
case R.id.radio_button3:
mHost.setCurrentTabByTag("FOUR");
break;
case R.id.radio_button4:
mHost.setCurrentTabByTag("FIVE");
break;
}
}
}
public void onCheckedChanged(RadioGroup group, int checkedId); 设置监听后,实现接口,就可以得到 RadioGroup 的对象和RadioGroup 内的RadioButton的checkedId,从而实现操作。
点击 RadioButton能响应onCheckedChanged,是因为同组内旧checkedId和新的checkedId不同,有时候我们在XML的布局文件中已经把该RadioButton的android:checked = "true"设置了,如果你在初始化定义此RadioButton的setChecked为true,可能就无法响应onCheckedChanged 了。因为我在做程序的时候,为了统一方便,把很多功能都设置在onCheckedChanged中,这样当set变化后,就能自动执行了,所以当我们已经不小心在布局文件中设置了,可以在初始化中再次实现setChecked为true的一些动作了。
我也是在debug的时候发现一些问题,所以要理解接口,只有在CheckedChanged的时候,才会有响应动作。
2、对于TabHost也是同理的,他是管理不同个布局显示的tab控制器。但是会更复杂一些,以为你需要添加布局到TabHost中,void addTab(TabSpec tabSpec),但是要注意的是,在实现addTab时候,已经默认显示第一个tab,如果你要设置显示tab,可以用TabHost对象 setCurrentTab(mCurrentTab),对于TabHost的监听
mTabHost.setOnTabChangedListener(new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub });
onTabChanged也是当tab变化是才能够响应的,在onTabChanged完成一些动作是要记住TabHost默认现实第一个tab页,所以当你初始化setCurrentTab(0),其实没有完成onTabChanged中对应id的操作。
-------------------------------------------------------------------
布局文件
<?xml version="1.0" encoding="UTF-8"?>
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<RadioGroup
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:id="@+id/main_radio"
android:background="@drawable/maintab_toolbar_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_button0"
android:tag="radio_button0"
android:layout_marginTop="2.0dip"
android:text="@string/alarm"
android:drawableTop="@drawable/icon_1"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button1"
android:tag="radio_button1"
android:layout_marginTop="2.0dip"
android:text="@string/message"
android:drawableTop="@drawable/icon_2"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button2"
android:tag="radio_button2"
android:layout_marginTop="2.0dip"
android:text="@string/photo"
android:drawableTop="@drawable/icon_3"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button3"
android:tag="radio_button3"
android:layout_marginTop="2.0dip"
android:text="@string/music"
android:drawableTop="@drawable/icon_4"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button4"
android:tag="radio_button4"
android:layout_marginTop="2.0dip"
android:text="@string/setting"
android:drawableTop="@drawable/icon_5"
style="@style/main_tab_bottom" />
</RadioGroup>
</LinearLayout>
</TabHost>
需要注意的是,如果用TabHost这个控件,其中有几个ID是必须这么写的,android:id="@android:id/tabhost ;android:id="@android:id/tabcontent" ;android:id="@android:id/tabs" ;之所以要这么写是因为在TabHost这个类中。需要实例化上述这个ID的控件。看源码:
在TabActivity中有这么个方法:
@Override public void onContentChanged() { super.onContentChanged(); mTabHost = (TabHost) findViewById(com.android.internal.R.id.tabhost); if (mTabHost == null) { throw new RuntimeException( "Your content must have a TabHost whose id attribute is " + "'android.R.id.tabhost'"); } mTabHost.setup(getLocalActivityManager()); } private void ensureTabHost() { if (mTabHost == null) { this.setContentView(com.android.internal.R.layout.tab_content); } }
当内容发生改变时它会调用这个方法,来更新列表或者其他视图,而这个方法中需要实例化TabHost,所以必须通过ID为tabhost实例化。
再看看TabHost这个类中,
public void setup() { mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs); if (mTabWidget == null) { throw new RuntimeException( "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'"); } // KeyListener to attach to all tabs. Detects non-navigation keys // and relays them to the tab content. mTabKeyListener = new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: case KeyEvent.KEYCODE_DPAD_LEFT: case KeyEvent.KEYCODE_DPAD_RIGHT: case KeyEvent.KEYCODE_DPAD_UP: case KeyEvent.KEYCODE_DPAD_DOWN: case KeyEvent.KEYCODE_ENTER: return false; } mTabContent.requestFocus(View.FOCUS_FORWARD); return mTabContent.dispatchKeyEvent(event); } }; mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() { public void onTabSelectionChanged(int tabIndex, boolean clicked) { setCurrentTab(tabIndex); if (clicked) { mTabContent.requestFocus(View.FOCUS_FORWARD); } } }); mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent); if (mTabContent == null) { throw new RuntimeException( "Your TabHost must have a FrameLayout whose id attribute is " + "'android.R.id.tabcontent'"); } }
这个方法,是在增加选项卡之前由系统调用。在这个方法中需要通过tabs 这个ID实例化一个TabWidget,通过tabcontent这个ID实例化一个FrameLayout,用来放置选项卡内容。所以这两个ID也是固定的。
在上述布局文件中隐藏了系统默认的Widget,取而代之的是带有图片的Button。
看一下主要代码:
package com.iteye.androidtoast;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
public class MainActivity extends TabActivity implements OnCheckedChangeListener{
/** Called when the activity is first created. */
private TabHost mHost;
private RadioGroup radioderGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maintabs);
//实例化TabHost
mHost=this.getTabHost();
//添加选项卡
mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE")
.setContent(new Intent(this,OneActivity.class)));
mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO")
.setContent(new Intent(this,TwoActivity.class)));
mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE")
.setContent(new Intent(this,ThreeActivity.class)));
mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR")
.setContent(new Intent(this,FourActivity.class)));
mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE")
.setContent(new Intent(this,FiveActivity.class)));
radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
radioderGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio_button0:
mHost.setCurrentTabByTag("ONE");
break;
case R.id.radio_button1:
mHost.setCurrentTabByTag("TWO");
break;
case R.id.radio_button2:
mHost.setCurrentTabByTag("THREE");
break;
case R.id.radio_button3:
mHost.setCurrentTabByTag("FOUR");
break;
case R.id.radio_button4:
mHost.setCurrentTabByTag("FIVE");
break;
}
}
}
相关文章推荐
- Android底部导航栏实现(二)之RadioGroup
- Android开发Tabhost+RadioGroup实现底部菜单栏
- Android (FragmentTabHost+RadioGroup)实现底部bar
- Android RadioGroup+ViewPager+ActionBar实现仿微信6.0界面(底部滑动菜单栏+导航栏)
- Android使用RadioGroup实现底部导航栏
- Android下利用Fragment+RadioGroup和TabHost实现底部选项卡的效果
- Android TabHost + RadioButton实现Tab导航栏
- Android之底部導航欄--RadioGroup、TabHost、Fragment
- Android下利用RadioGroup和RadioButton实现Tabbar的效果
- android自定义RadioGroup可以添加多种布局的实现方法
- TabHost、RadioGroup实现底部菜单导航
- 【Android】TabHost与RadioGroup结合完成的菜单
- Android自定义RadioGroup实现单选完整示例
- 基于RadioGroup的TabHost实现[ 转]
- 如何利用Android TabHost+ActivityGroup+Broadcast实现类似于iPhone的底部菜单
- TabHost、RadioGroup实现底部菜单导航
- android的tabhost+RadioGroup+PopupWindow
- android的tabhost+RadioGroup+PopupWindow
- Android笔记:通过RadioGroup/RadioButton自定义tabhost的简单方法
- Android学习-----对RadioGroup和TabHost监听的理解