您好,登錄后才能下訂單哦!
代碼如下:
布局代碼:
package com.example.tabhost;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
public class MainActivity extends TabActivity implements OnTabChangeListener {
private TabSpec ts1,ts2,ts3;//實(shí)例化3個(gè)分頁(yè)
private TabHost tableHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tableHost=this.getTabHost();//實(shí)例化一個(gè)TableHost
//利用LayoutInflater將布局與分頁(yè)菜單一起顯示
LayoutInflater.from(this).inflate(R.layout.activity_main, tableHost.getTabContentView());
ts1=tableHost.newTabSpec("tabOne");//實(shí)例化一個(gè)分頁(yè)
ts1.setIndicator("Tab1");//設(shè)置此分頁(yè)顯示的標(biāo)題
ts1.setContent(R.id.btn);//設(shè)置此分頁(yè)的資源id
ts2=tableHost.newTabSpec("tabTwo");
//設(shè)置此分頁(yè)顯示的標(biāo)題和圖標(biāo)
ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.hagar3));
ts2.setContent(R.id.et);
ts3=tableHost.newTabSpec("TabThree");
ts3.setIndicator("tab3");
ts3.setContent(R.id.mylayout);//設(shè)置此分頁(yè)的布局id
tableHost.addTab(ts1);//菜單中添加ts1分頁(yè)
tableHost.addTab(ts2);
tableHost.addTab(ts3);
tableHost.setOnTabChangedListener(this);
}
public void onTabChanged(String tabId)
{
if(tabId.equals("tabOne"))
{
Toast.makeText(this, "分頁(yè)1", Toast.LENGTH_LONG).show();
}
if(tabId.equals("tabTwo"))
{
Toast.makeText(this, "分頁(yè)2", Toast.LENGTH_LONG).show();
}
if(tabId.equals("tabThree"))
{
Toast.makeText(this, "分頁(yè)3", Toast.LENGTH_LONG).show();
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。