返回键的拦截

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN) { if (firstTime + 2000 > System.currentTimeMillis()) {
super.onBackPressed();
} else {
ToastUtils.showShort(this, "再按一次退出程序");
}
firstTime = System.currentTimeMillis(); return false;
}
return super.dispatchKeyEvent(event);
}
package com.xinhua.schome.activity;

import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost; import com.easemob.EMConnectionListener;
import com.easemob.EMError;
import com.easemob.EMEventListener;
import com.easemob.EMNotifierEvent;
import com.easemob.chat.EMChatManager;
import com.xinhua.schome.R;
import com.xinhua.schome.base.App;
import com.xinhua.schome.base.AppManager;
import com.xinhua.schome.common.MyConstants;
import com.xinhua.schome.event.BaseEvent;
import com.xinhua.schome.event.GetMsgEvent;
import com.xinhua.schome.event.LoginChangeCityEvent;
import com.xinhua.schome.event.MainEvent;
import com.xinhua.schome.event.UpdateCityEvent;
import com.xinhua.schome.utils.AppHelper;
import com.xinhua.schome.utils.PrefUtils;
import com.xinhua.schome.utils.ToastUtils;
import com.xinhua.schome.webactivity.FindWebActivity;
import com.xinhua.schome.webactivity.MainWebActivity;
import com.xinhua.schome.webactivity.UserCenterWebActivity;
import com.xinhua.schome.webactivity.MainWebActivity.MyConnectionListener; import de.greenrobot.event.EventBus; public class MainActivity extends TabActivity implements OnClickListener,EMEventListener { private static long firstTime; private TabHost tabHost; private ImageView mBelowHomeIv;
private ImageView mBelowMessageIv;
private ImageView mBelowFindIv;
private ImageView mBelowPersonageIv;
private TextView mBelowHomeTv;
private TextView mBelowMessageTv;
private TextView mBelowFindTv;
private TextView mBelowPersonageTv;
private FrameLayout mMainFrame;
private LinearLayout mHomeLayout;
private LinearLayout mMessageLayout;
private LinearLayout mFindLayout;
private LinearLayout mPersonageLayout; private String mobile;
private TextView mMessageNumTv; private MyConnectionListener connectionListener; @Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
System.out.println("onNewIntent");
super.onNewIntent(intent);
} @Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN) { if (firstTime + > System.currentTimeMillis()) {
super.onBackPressed();
} else {
ToastUtils.showShort(this, "再按一次退出程序");
}
firstTime = System.currentTimeMillis(); return false;
}
return super.dispatchKeyEvent(event);
} @Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0); setContentView(R.layout.activity_new_main); AppManager.getInstance().addActivity(this); tabHost = this.getTabHost();
tabHost.addTab(tabHost.newTabSpec("首页").setIndicator("首页")
.setContent(new Intent(this, MainWebActivity.class)));
tabHost.addTab(tabHost.newTabSpec("消息").setIndicator("消息")
.setContent(new Intent(this, MessageActivity.class)));
tabHost.addTab(tabHost.newTabSpec("发现").setIndicator("发现")
.setContent(new Intent(this, FindWebActivity.class)));
// tabHost.addTab(tabHost.newTabSpec("个人").setIndicator("个人").setContent(new
// Intent(this, UserCenterWebActivity.class))); tabHost.addTab(tabHost.newTabSpec("个人").setIndicator("个人")
.setContent(new Intent(this, MyProfileActivity.class))); initView(); changePage(); EventBus.getDefault().register(this); connectionListener = new MyConnectionListener();
EMChatManager.getInstance().addConnectionListener(connectionListener); } /**
* 连接监听listener
*
*/
public class MyConnectionListener implements EMConnectionListener { @Override
public void onConnected() { } @Override
public void onDisconnected(final int error) { final String st1 = getResources().getString(
R.string.can_not_connect_chat_server_connection);
final String st2 = getResources().getString(
R.string.the_current_network);
runOnUiThread(new Runnable() { @Override
public void run() {
if (error == EMError.USER_REMOVED) {
// 显示帐号已经被移除
// showAccountRemovedDialog();
} else if (error == EMError.CONNECTION_CONFLICT) {
// 显示帐号在其他设备登陆dialog
showConflictDialog();
} else {
/*
* chatHistoryFragment.errorItem.setVisibility(View.VISIBLE
* ); if (NetUtils.hasNetwork(MainWebActivity.this))
* chatHistoryFragment.errorText.setText(st1); else
* chatHistoryFragment.errorText.setText(st2);
*/ }
} });
}
} /**
* 显示帐号在别处登录dialog
*/
private void showConflictDialog() { // TODO Auto-generated method stub
ToastUtils.showShort(MainActivity.this, "此帐号已经在其他移动设备登录,请注意帐号安全"); AppManager.getInstance().finishAllActivityExcept(MainActivity.class); PrefUtils.putString(MainActivity.this, MyConstants.PREF_KEY_SID, "");
PrefUtils.putString(MainActivity.this,
MyConstants.PREF_KEY_USER_JSON, "");
PrefUtils.putInt(this, MyConstants.KEY_NO_VIEW_COUNT, ); Intent intent = new Intent(MainActivity.this, LoginActivity.class); if (App.getUser() != null) {
mobile = App.getUser().getMobile();
intent.putExtra("Mobile", mobile);
} startActivity(intent); App.setUser(null); EMChatManager.getInstance().logout(); } private void initView() { mBelowHomeIv = (ImageView) findViewById(R.id.below_home_iv);
mBelowMessageIv = (ImageView) findViewById(R.id.below_message_iv);
mBelowFindIv = (ImageView) findViewById(R.id.below_find_iv);
mBelowPersonageIv = (ImageView) findViewById(R.id.below_personage_iv); mBelowHomeTv = (TextView) findViewById(R.id.below_home_tv);
mBelowMessageTv = (TextView) findViewById(R.id.below_message_tv);
mBelowFindTv = (TextView) findViewById(R.id.below_find_tv);
mBelowPersonageTv = (TextView) findViewById(R.id.below_personage_tv); mMainFrame = (FrameLayout) findViewById(R.id.main_frame); mHomeLayout = (LinearLayout) findViewById(R.id.below_home_layout);
mMessageLayout = (LinearLayout) findViewById(R.id.below_message_layout);
mFindLayout = (LinearLayout) findViewById(R.id.below_find_layout);
mPersonageLayout = (LinearLayout) findViewById(R.id.below_personage_layout);
mMessageNumTv = (TextView) findViewById(R.id.message_num_tv);
mHomeLayout.setOnClickListener(this);
mMessageLayout.setOnClickListener(this);
mFindLayout.setOnClickListener(this);
mPersonageLayout.setOnClickListener(this); } public void onEventMainThread(MainEvent event) { switch (event.what) {
case : PrefUtils.putString(this, "find_web_index", "");
changePage(); break;
case : PrefUtils.putString(this, "find_web_index", "");
changePage();
break; default:
break;
} } public void onEvent(BaseEvent event) {
if (event instanceof GetMsgEvent) {
updateBadgeView();
} } @Override
protected void onDestroy() {
// TODO Auto-generated method stub
EventBus.getDefault().unregister(this);
AppManager.getInstance().finishActivity(this);
super.onDestroy();
} /**
* 连续按两次返回键就退出
*/
@Override
public void onBackPressed() { } @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.below_home_layout:
changePage();
break;
case R.id.below_message_layout:
changePage(); break; case R.id.below_find_layout:
changePage(); break; case R.id.below_personage_layout:
changePage(); break; default:
break;
}
} private void clearSelection() {
// TODO Auto-generated method stub
mBelowHomeIv.setSelected(false);
mBelowMessageIv.setSelected(false);
mBelowFindIv.setSelected(false);
mBelowPersonageIv.setSelected(false); mBelowHomeTv.setSelected(false);
mBelowMessageTv.setSelected(false);
mBelowFindTv.setSelected(false);
mBelowPersonageTv.setSelected(false);
} private void updateBadgeView() { if (App.getUser() == null) { mMessageNumTv.setVisibility(View.GONE); }else{ int unReadMsgCount = EMChatManager.getInstance().getUnreadMsgsCount(); int noViewCount = PrefUtils.getInt(getApplicationContext(),
MyConstants.KEY_NO_VIEW_COUNT, ); final int mTotalMsg = unReadMsgCount + noViewCount; runOnUiThread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
if (mTotalMsg > ) { if (mTotalMsg > ) {
mMessageNumTv.setText("");
mMessageNumTv.setVisibility(View.VISIBLE);
}else{
mMessageNumTv.setText(mTotalMsg + "");
mMessageNumTv.setVisibility(View.VISIBLE);
} } else {
mMessageNumTv.setVisibility(View.GONE);
}
}
}); } } private void changePage(int i) { clearSelection(); switch (i) {
case :
mBelowHomeIv.setSelected(true);
mBelowHomeTv.setSelected(true); tabHost.setCurrentTabByTag("首页"); break;
case :
mBelowMessageIv.setSelected(true);
mBelowMessageTv.setSelected(true);
AppManager.getInstance().change_message_fragment = true; tabHost.setCurrentTabByTag("消息"); // PrefUtils.putBoolean(getApplicationContext(), MyConstants.KEY_CHANGE_MESSAGE_FRAGMENT, true); break;
case :
mBelowFindIv.setSelected(true);
mBelowFindTv.setSelected(true);
tabHost.setCurrentTabByTag("发现"); break;
case :
mBelowPersonageIv.setSelected(true);
mBelowPersonageTv.setSelected(true);
tabHost.setCurrentTabByTag("个人"); break; default:
break;
}
} @Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("Main------onResume");
updateBadgeView(); EMChatManager.getInstance().registerEventListener(
this,
new EMNotifierEvent.Event[] {
EMNotifierEvent.Event.EventNewMessage,
EMNotifierEvent.Event.EventOfflineMessage,
EMNotifierEvent.Event.EventConversationListChanged }); super.onResume();
} @Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
EMChatManager.getInstance().unregisterEventListener(this);
} @Override
public void onEvent(EMNotifierEvent arg0) {
// TODO Auto-generated method stub
updateBadgeView();
} }
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" /> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:visibility="gone" /> <include layout="@layout/include_divider" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="3dp"
android:paddingTop="3dp" > <LinearLayout
android:id="@+id/below_home_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=""
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/below_home_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_below_home_selector" /> <TextView
android:id="@+id/below_home_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:textColor="@drawable/find_teach_tv_color_selector" />
</LinearLayout> <RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="" > <LinearLayout
android:id="@+id/below_message_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <ImageView
android:id="@+id/below_message_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/bg_below_message_selector" /> <TextView
android:id="@+id/message_num_tv"
android:layout_width="15dp"
android:layout_height="15dp"
android:background="@drawable/shape_circle_red_bord"
android:gravity="center"
android:text=""
android:layout_toRightOf="@id/below_message_iv"
android:textColor="@color/white"
android:visibility="invisible"
android:textSize="8sp" /> </RelativeLayout> <TextView
android:id="@+id/below_message_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="消息"
android:textColor="@drawable/find_teach_tv_color_selector" />
</LinearLayout>
</RelativeLayout> <LinearLayout
android:id="@+id/below_find_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=""
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/below_find_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_below_find_selector" /> <TextView
android:id="@+id/below_find_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发现"
android:textColor="@drawable/find_teach_tv_color_selector" />
</LinearLayout> <LinearLayout
android:id="@+id/below_personage_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=""
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/below_personage_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_below_personage_selector" /> <TextView
android:id="@+id/below_personage_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="个人"
android:textColor="@drawable/find_teach_tv_color_selector" />
</LinearLayout>
</LinearLayout>
</LinearLayout> </TabHost>

tablehost的更多相关文章

  1. C#用扩展方法进行自动生成添加删除对象转换的功能

    public static class ExtendedModel { #region 实体类的增删改查 #region 添加 public static string AddStr(this obj ...

  2. Android工作学习第5天之TabHost实现菜单栏底部显示

    TabHost是一个装载选项卡窗口的容器,实现分模块显示的效果.像新浪微博客户端.微信客户端都是使用tabehost组件来开发的. TabHost的组成: |---TabWidget:实现标签栏,可供 ...

  3. 动软MySQL存储过程模板

    <#@ template language="c#" HostSpecific="True" #><#@ output extension= ...

  4. 更改动软代码生成器模板 验证Model数据合法性

    1.第一个模板 判断字段是否为空 类 IsNullableType.cmt static public partial class CommonType { public static bool Is ...

  5. 手机大数据_SQL映射对象_动软_代码模板_Models

    <#@ template language="c#" HostSpecific="True" #> <#@ output extension= ...

  6. 动软Model 模板 生成可空类型字段

    动软代码 生成可空类型 <#@ template language="c#" HostSpecific="True" #> <#@ outpu ...

  7. 动软代码生成与 EntityFramework 实体生成模板

    有用到EntityFrameWork的同学们,可以用用. 实体工程中添加EF6的dll 还有 ValidBox4Mvc.ValidRules.dll应用到项目中,此dll下载地址:http://www ...

  8. MVC使用的MetaModel代码生成器模板

    代码生成器能使从一些重复的工作中缓解下来 在最近开发MVC项目中使用到了MetaModel用来设定Model的显示名称,数据限制的代码生成模板,自己第一做代码生成模板还有很多缺陷. 下面是模板代码: ...

  9. Android ActionBar通过Tab进行不同的Fragment之间的交换

    ActionBar的使用常见于4.0系统,其Tab的使用挺广泛的. 在ActionBar中添加标签(Tabs),每个标签对应的是一个Fragment,点击不同的Tab时,就会切换到对应的Fragmen ...

随机推荐

  1. CodeForces 705C Thor

    开30W个vector将数字归类,每一类数字开一个指针P,记录已经阅读到哪一个了,还可以开一个优先队列维护这些指针P. #pragma comment(linker, "/STACK:102 ...

  2. .net技术深入

    程序集(Assembly),可以看做是一堆相关类打一个包,相当于java中的jar包(*).打包的目的:程序中只引用必须的程序集,减小程序的尺寸:一些程序集内部的类不想让其他程序集调用. 我们调用的类 ...

  3. Git操作基本命令

    分支开发原则: 如果本地dev分支有修改,则需要先切换到master分支,把本地分支的修改merge回master(git merge dev),然后在master上把合并后的内容push到maste ...

  4. jQuery切换网页皮肤并保存到Cookie示例代码

    经过使用,靠谱! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  5. js--闭包的理解

    从技术上来讲,在JS中,每个function都是闭包,因为它总是能访问在它外部定义的数据. 当该内部函数在外部函数外被调用,就生成了闭包. 函数内部可以直接读取全局变量. 闭包就是能够读取其他函数内部 ...

  6. HttpWebRequest的简单使用

    新建新的空网站和一个default.aspx页面测试,实验例子: using System; using System.Collections.Generic; using System.IO; us ...

  7. MySQL数据类型和属性

    日期和时间数据类型 MySQL数据类型 含义 date 3字节,日期,格式:2014-09-18 time 3字节,时间,格式:08:42:30 datetime 8字节,日期时间,格式:2014-0 ...

  8. ERP流程图

  9. [ An Ac a Day ^_^ ] hdu 2553 N皇后问题 搜索

    曾经想过一天一AC 坚持下来的确不容易额 (我是没坚持下来 尽量以后坚持…… 经典的N皇后问题 搜索的入门问题 学了这么久竟然一直没敲过 今天敲一下…… 这道题也不是很简单额 纯暴力就超时了 要打一下 ...

  10. 带宽 VS CDN (转载)

    并发的影响因素:带宽.web server(含php).static server.数据库 带宽价格: 静态IP企业宽带 5M 10M 50M 100M 200M 盛大云 华东双线 216/月 396 ...