标签:
1.MainActivity.java
关键点:
1>MainActivity extends TabActivity implements OnClickListener,OnCheckedChangeListener
2> localTabHost.addTab(buildTabSpec("new", R.string.form_tab_new,
R.drawable.main_tab_bg_selector, new Intent(this,
HellowordActivity.class)));
3>mR1 = ((RadioButton) findViewById(R.id.radio_button0));
mR1.setOnCheckedChangeListener(this);
4>
<string name="form_tab_new">最新</string>
<string name="form_tab_hot">热门</string>
<string name="form_tab_get">获取积分</string>
<string name="form_tab_soft">应用</string>
5>
main_tab_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/main_tab_bg" android:state_checked="true" />
</selector>
6>
tab_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/toptab_normal" android:state_checked="false"/>
<item android:drawable="@drawable/toptab_select" android:state_checked="true"/>
</selector>
7>colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color> <!-- 白色 -->
<color name="bg">#eeeef4</color>
</resources>
8>toptab_select 、 toptab_normal 、 main_tab_bg
package com.example.tabhostdemo; import android.os.Bundle; import android.app.Activity; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.view.Menu; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TabHost; import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends TabActivity implements OnClickListener,OnCheckedChangeListener{ private Button back; private TabHost mTabHost; private RadioButton mR1; private RadioButton mR2; private RadioButton mR3; private RadioGroup main_radio; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); setupIntent(); } private void setupIntent() { this.mTabHost = getTabHost(); TabHost localTabHost = this.mTabHost; localTabHost.addTab(buildTabSpec("new", R.string.form_tab_new, R.drawable.main_tab_bg_selector, new Intent(this, HellowordActivity.class))); localTabHost.addTab(buildTabSpec("hot", R.string.form_tab_hot, R.drawable.main_tab_bg_selector, new Intent(this, HellowordActivity2.class))); localTabHost.addTab(buildTabSpec("get", R.string.form_tab_get, R.drawable.main_tab_bg_selector, new Intent(this, HellowordActivity.class))); } private void initView() { mR1 = ((RadioButton) findViewById(R.id.radio_button0)); mR1.setOnCheckedChangeListener(this); this.mR1.setTextColor(Color.RED); mR2 = ((RadioButton) findViewById(R.id.radio_button1)); mR2.setOnCheckedChangeListener(this); mR3 = ((RadioButton) findViewById(R.id.radio_button2)); mR3.setOnCheckedChangeListener(this); main_radio = (RadioGroup) findViewById(R.id.main_radio); back = (Button) findViewById(R.id.back); back.setOnClickListener(this); } private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, final Intent content) { return this.mTabHost .newTabSpec(tag) .setIndicator(getString(resLabel), getResources().getDrawable(resIcon)) .setContent(content); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { System.out.println("isChecked"); switch (buttonView.getId()) { case R.id.radio_button0: this.mTabHost.setCurrentTabByTag("new"); this.mR1.setTextColor(Color.RED); this.mR2.setTextColor(Color.BLACK); this.mR3.setTextColor(Color.BLACK); break; case R.id.radio_button1: this.mR1.setTextColor(Color.BLACK); this.mR3.setTextColor(Color.BLACK); this.mR2.setTextColor(Color.RED); this.mTabHost.setCurrentTabByTag("hot"); break; case R.id.radio_button2: this.mR1.setTextColor(Color.BLACK); this.mR2.setTextColor(Color.BLACK); this.mR3.setTextColor(Color.RED); this.mTabHost.setCurrentTabByTag("get"); break; // case R.id.radio_button2: // // mPopupWindow.showAsDropDown(main_radio, 0, 0); // break; } } } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.back: finish(); break; default: break; } } }
2.HellowordActivity.java
HellowordActivity2.java 和 HellowordActivity.java 一样
package com.example.tabhostdemo; import android.app.Activity; import android.os.Bundle; public class HellowordActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); } }
3.
标签:
原文地址:http://www.cnblogs.com/childhooding/p/4461849.html