第一个tab
<img src="https://img-blog.csdn.net/20141101140826027" height="443" width="265" alt="" />
第三个标签activity
<img src="https://img-blog.csdn.net/20141101140853031" height="443" width="279" alt="" />
第二个activity标签
<img src="https://img-blog.csdn.net/20141101140847883" height="445" width="279" alt="" />
//mainactivity.java
public class MainActivity extends TabActivity implements OnClickListener,
OnCheckedChangeListener {
/** 标签当中的tag 标识符 */
private static final String TAG_FIRST = "1";
private static final String TAG_SECOND = "2";
private static final String TAG_THIRD = "3";
// /**popWindowsLayout 当中的组件引用 */
private Button popBtn;
/** main_activity 当中的组件引用 */
private RadioButton radio_btn1, radio_btn2, radio_btn3, radio_btn4;
private TabHost tabHost;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ExitAllApplication.getInstance().pushApplication(this);
initView();
System.out.println(""+tabHost.getCurrentTab());
}
private void initView() {
//setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 每次跳转重新加载,重新刷新加载
//设置跳转的方式,就不在详细介绍了,跳转页面也不写了,相信大家都会的吧
Intent intent1 = new Intent(this, FirstActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent2 = new Intent(this, SecondActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent3 = new Intent(this, ThirdActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.tabHost =this.getTabHost();
try{
tabHost.addTab(setTabSpec(TAG_FIRST, "first", intent1));
tabHost.addTab(setTabSpec(TAG_SECOND, "second", intent2));
tabHost.addTab(setTabSpec(TAG_THIRD, "third", intent3));
}catch(Exception e){
e.printStackTrace();
}
radio_btn1 = (RadioButton) findViewById(R.id.radiobtn_first);
radio_btn2 = (RadioButton) findViewById(R.id.radiobtn_second);
radio_btn3 = (RadioButton) findViewById(R.id.radiobtn_third);
radio_btn4 = (RadioButton) findViewById(R.id.radiobtn_four);
radio_btn1.setOnCheckedChangeListener(this);
radio_btn2.setOnCheckedChangeListener(this);
radio_btn3.setOnCheckedChangeListener(this);
radio_btn4.setOnCheckedChangeListener(this);
}
/**
* 设置tab标签
* @param tag
* @param resLabel
* @param intent
* @return 返回的是tab类型
*/
public TabHost.TabSpec setTabSpec(String tag, String resLabel,
final Intent intent) {
TabHost mtabhost = this.tabHost;
return mtabhost.newTabSpec(tag).setIndicator(resLabel)
.setContent(intent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.popwindowbtn:
new PopWinShow(this).showPopWindows(v);
break;
}
}
/**
* 隐藏tabWidget原因 我们使用radiobutton去设置 tab tag,代替tabWidiget 这里 我们是利用groupRadio
* 去引用,当点击radiobutton的时候,我们设置该button 对应的tab 标签。利用setCurrenrtabbytag 注:
* 这里我们需要为每一个标签设置一个tag 可以使用常量 也可以使用自定义变量
*/
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//当点击一个按钮之后 ,会设置当前的标签
if (isChecked) {// 判断 如果被点击,就执行 没有被点击就不执行
switch (buttonView.getId()) {
case R.id.radiobtn_first:
this.tabHost.setCurrentTabByTag(TAG_FIRST);
break;
case R.id.radiobtn_second:
this.tabHost.setCurrentTabByTag(TAG_SECOND);
break;
case R.id.radiobtn_third:
this.tabHost.setCurrentTabByTag(TAG_THIRD);
break;
}
}
}
<pre name="code" class="java">
//activity_main.xml
<TabHost 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" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:background="@drawable/title"
android:id="@+id/layout_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:text="@string/title" />
<Button
android:id="@+id/popwindowbtn"
android:background="@drawable/managbtn_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="管理"
/>
</RelativeLayout>
<FrameLayout
android:background="#D3D3D3"
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0.0dp"
android:layout_weight="1" />
<!-- 隐藏原因 我们使用radiobutton去设置 tab tag,代替tabWidiget
这里 我们是利用groupRadio 去引用,当点击radiobutton的时候,我们设置该button 对应的tab 标签。利用setCurrenrtabbytag
注: 这里我们需要为每一个标签设置一个tag 可以使用常量 也可以使用自定义变量
-->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="gone" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:gravity="center_vertical"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/radiobtn_first"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:button="@null"
android:focusable="true"
android:gravity="center"
android:text="@string/first"
/>
<RadioButton
android:id="@+id/radiobtn_second"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:button="@null"
android:focusable="true"
android:gravity="center"
android:text="@string/second"
/>
<RadioButton
android:id="@+id/radiobtn_third"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:button="@null"
android:focusable="true"
android:gravity="center"
android:text="@string/third"
/>
<RadioButton
android:id="@+id/radiobtn_four"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btn_select"
android:button="@null"
android:focusable="true"
android:gravity="center"
android:text="more"
android:visibility="gone"
/>
</RadioGroup>
</LinearLayout>
</TabHost>
</TabHost>
标签TabHost,实现一个标签对应一个activity
最新推荐文章于 2020-05-14 16:47:58 发布