码迷,mamicode.com
首页 > 其他好文 > 详细

tablehost

时间:2015-12-21 12:06:24      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:

返回键的拦截

@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 + 2000 > 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(0);
        
        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, 0);

        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 3:

            PrefUtils.putString(this, "find_web_index", "2");
            changePage(2);

            break;
        case 4:

            PrefUtils.putString(this, "find_web_index", "3");
            changePage(2);
            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(0);
            break;
        case R.id.below_message_layout:
            changePage(1);
            
            break;
            
        case R.id.below_find_layout:
            changePage(2);
            
            break;
            
        case R.id.below_personage_layout:
            changePage(3);
            
            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, 0);
    
            final int mTotalMsg = unReadMsgCount + noViewCount;
    
            runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                if (mTotalMsg > 0) {
                    
                    if (mTotalMsg > 100) {
                        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 0:
            mBelowHomeIv.setSelected(true);
            mBelowHomeTv.setSelected(true);

            tabHost.setCurrentTabByTag("首页");
            
            break;
        case 1:
            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 2:
            mBelowFindIv.setSelected(true);
            mBelowFindTv.setSelected(true);
            tabHost.setCurrentTabByTag("发现");
            
            break;
        case 3:
            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="1"
                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="1" >

                <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="0"
                            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="1"
                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="1"
                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

标签:

原文地址:http://www.cnblogs.com/wikiki/p/5062682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!