实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.dream.myqiyi; import com.dream.myqiyi.account.AccountActivity; import com.dream.myqiyi.channel.ChannelActivity; import com.dream.myqiyi.home.HomeActivity; import com.dream.myqiyi.more.MoreActivity; import com.dream.myqiyi.search.SearchActivity; import com.minisea.cookbook.R; import com.umeng.content.UmengAction; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.TabHost; import android.widget.TextView; public class MainActivity extends TabActivity implements OnClickListener { public static String TAB_TAG_HOME = "home"; public static String TAB_TAG_CHANNEL = "channel"; public static String TAB_TAG_ACCOUNT = "account"; public static String TAB_TAG_SEARCH = "search"; public static String TAB_TAB_MORE = "more"; public static TabHost mTabHost; static final int COLOR1 = Color.parseColor("#787878"); static final int COLOR2 = Color.parseColor("#ffffff"); ImageView mBut1, mBut2, mBut3, mBut4, mBut5; TextView mCateText1, mCateText2, mCateText3, mCateText4, mCateText5; Intent mHomeItent, mChannelIntent, mSearchIntent, mAccountIntent, mMoreIntent; int mCurTabId = R.id.channel1; // Animation private Animation left_in, left_out; private Animation right_in, right_out; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.main); prepareAnim(); prepareIntent(); setupIntent(); prepareView(); } private void prepareAnim() { left_in = AnimationUtils.loadAnimation(this, R.anim.left_in); left_out = AnimationUtils.loadAnimation(this, R.anim.left_out); right_in = AnimationUtils.loadAnimation(this, R.anim.right_in); right_out = AnimationUtils.loadAnimation(this, R.anim.right_out); } private void prepareView() { mBut1 = (ImageView) findViewById(R.id.imageView1); mBut2 = (ImageView) findViewById(R.id.imageView2); mBut3 = (ImageView) findViewById(R.id.imageView3); mBut4 = (ImageView) findViewById(R.id.imageView4); mBut5 = (ImageView) findViewById(R.id.imageView5); findViewById(R.id.channel1).setOnClickListener(this); findViewById(R.id.channel2).setOnClickListener(this); findViewById(R.id.channel3).setOnClickListener(this); findViewById(R.id.channel4).setOnClickListener(this); findViewById(R.id.channel5).setOnClickListener(this); mCateText1 = (TextView) findViewById(R.id.textView1); mCateText2 = (TextView) findViewById(R.id.textView2); mCateText3 = (TextView) findViewById(R.id.textView3); mCateText4 = (TextView) findViewById(R.id.textView4); mCateText5 = (TextView) findViewById(R.id.textView5); UmengAction.initTimer(this, 30); } private void prepareIntent() { mHomeItent = new Intent(this, HomeActivity.class); mChannelIntent = new Intent(this, ChannelActivity.class); mAccountIntent = new Intent(this, AccountActivity.class); mSearchIntent = new Intent(this, SearchActivity.class); mMoreIntent = new Intent(this, MoreActivity.class); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { mBut1.performClick(); return true; } return super.onKeyDown(keyCode, event); } private void setupIntent() { mTabHost = getTabHost(); mTabHost.addTab(buildTabSpec(TAB_TAG_HOME, R.string.category_home, R.drawable.icon_1_n, mHomeItent)); mTabHost.addTab(buildTabSpec(TAB_TAG_CHANNEL, R.string.category_channel, R.drawable.icon_2_n, mChannelIntent)); mTabHost.addTab(buildTabSpec(TAB_TAG_SEARCH, R.string.category_search, R.drawable.icon_3_n, mSearchIntent)); mTabHost.addTab(buildTabSpec(TAB_TAG_ACCOUNT, R.string.category_account, R.drawable.icon_4_n, mAccountIntent)); mTabHost.addTab(buildTabSpec(TAB_TAB_MORE, R.string.category_more, R.drawable.icon_5_n, mMoreIntent)); } private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, final Intent content) { return mTabHost .newTabSpec(tag) .setIndicator(getString(resLabel), getResources().getDrawable(resIcon)) .setContent(content); } public static void setCurrentTabByTag(String tab) { mTabHost.setCurrentTabByTag(tab); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (mCurTabId == v.getId()) { return; } mBut1.setImageResource(R.drawable.icon_1_n); mBut2.setImageResource(R.drawable.icon_2_n); mBut3.setImageResource(R.drawable.icon_3_n); mBut4.setImageResource(R.drawable.icon_4_n); mBut5.setImageResource(R.drawable.icon_5_n); mCateText1.setTextColor(COLOR1); mCateText2.setTextColor(COLOR1); mCateText3.setTextColor(COLOR1); mCateText4.setTextColor(COLOR1); mCateText5.setTextColor(COLOR1); int checkedId = v.getId(); final boolean o; if (mCurTabId < checkedId) o = true; else o = false; if (o) mTabHost.getCurrentView().startAnimation(left_out); else mTabHost.getCurrentView().startAnimation(right_out); switch (checkedId) { case R.id.channel1: mTabHost.setCurrentTabByTag(TAB_TAG_HOME); mBut1.setImageResource(R.drawable.icon_1_c); mCateText1.setTextColor(COLOR2); break; case R.id.channel2: mTabHost.setCurrentTabByTag(TAB_TAG_CHANNEL); mBut2.setImageResource(R.drawable.icon_2_c); mCateText2.setTextColor(COLOR2); break; case R.id.channel3: mTabHost.setCurrentTabByTag(TAB_TAG_SEARCH); mBut3.setImageResource(R.drawable.icon_3_c); mCateText3.setTextColor(COLOR2); break; case R.id.channel4: mTabHost.setCurrentTabByTag(TAB_TAG_ACCOUNT); mBut4.setImageResource(R.drawable.icon_4_c); mCateText4.setTextColor(COLOR2); break; case R.id.channel5: mTabHost.setCurrentTabByTag(TAB_TAB_MORE); mBut5.setImageResource(R.drawable.icon_5_c); mCateText5.setTextColor(COLOR2); break; default: break; } if (o) mTabHost.getCurrentView().startAnimation(left_in); else mTabHost.getCurrentView().startAnimation(right_in); mCurTabId = checkedId; } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论