标签TabHost,实现一个标签对应一个activity

第一个tab

第三个标签activity

第二个activity标签

//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;
			}
		}
	}
//activity_main.xml



    

        

            

            

你可能感兴趣的:(UI设计)