zoukankan      html  css  js  c++  java
  • 妙用TabHost——(转载)

    今天又学了一点很重要的知识.就是在应用程序中设置全局变量.

    先来说说为什么要设置全局变量吧.举一个例子,通过是否登录,来设置TabHost的状态.如果登录,则显示PM模块,否则不显示PM模块.
    如图:
    android <wbr>全局变量 <wbr>Tag标签的应用android <wbr>全局变量 <wbr>Tag标签的应用

    好了,我们要怎么弄呢?
    我们有可能在任何一个页面登录,登录后就要去通知TabHost改变状态.比如,我在MoreActivity中的个人中心页面(PersonCenterActivity)登录.当登录成功后,我按返回键,则MainTabActivity中的TabHost就要有PM模块了.
    看思路:
    首先,我们要清楚android的生命周期,MainTabActivity与MoreActivity的什么关系呢?我们点击"更多"按钮时,MainTabActivity结束了吗?现在是运行在MoreActivity上面吗?
    我们可以实验一下:
    在MainTabActivity的onResume()方法中加入 Log.i("TAG","mainTab-->resume");在MoreActivity的onResume()方法中加入Log.i("TAG","More-->resume");
    当我们Lanucher时,看到只打印了mainTab-->resume .点击"更多"按钮时只打印more-->resume.当点击MoreActivity中的个人中心进入个人中心页面.点击返回按钮时,奇怪的事发生了,android <wbr>全局变量 <wbr>Tag标签的应用android <wbr>全局变量 <wbr>Tag标签的应用
    它执行了两个Resume();
    android <wbr>全局变量 <wbr>Tag标签的应用
    而对我们有用的当然是存放TabHost的MainTabActivity的Resume方法了.
    说了这么多,要进入主题了.我们要怎么改变TabHost的项呢?
    一个好的办法就是建一个全局的类AppRuntime来设置它是否要显示.
    看代码:
    public class AppRuntime
    {
    public static boolean Must_Refresh =true;//设置是否要更新Tab
    public static boolean isMust_Refresh() { return Must_Refresh; } public static void setMust_Refresh(boolean must_Refresh) { Must_Refresh = must_Refresh; }
    }
    判断是否登录当然也放里面.(这边是用判断Load到的数据是否为空来判断的)
    protected static int m_UserID = -1;
    protected static ExtJSONObject m_UserData;
    public static int getUserID()
    {
    if (AppRuntime.m_UserID <= 0)
    {
    ExtJSONObject oUserData = AppRuntime.getUserData();
    if (oUserData != null)
    {
    AppRuntime.m_UserID = oUserData.optInt("uid");
    }
    }
    return AppRuntime.m_UserID;
    }
    public static void setUserID(int p_UserID)
    {
    AppRuntime.m_UserID = p_UserID;
    }
    public static ExtJSONObject getUserData()
    {
    return AppRuntime.m_UserData;
    }
    public static void setUserData(ExtJSONObject p_UserData)
    {
    AppRuntime.m_UserID = -1;
    AppRuntime.m_UserData = p_UserData;
    }
    好了.现在返回去看MainTabActivity里怎么处理:
    我们把创建TabHost放在OnResume中,而不放在OnCreate中,原因我刚刚写的.
    @Override
    protected void onResume()
    {
    super.onResume();
    Log.i("TAG","mainTab-->resume");
    if (AppRuntime.isMust_Refresh())
    {
    mUserJsonData = AppRuntime.getUserData();
    if (mUserJsonData != null)
    {
    m_IsLogin = true;
    }else {
    m_IsLogin = false;
    }
    TabHost tabHost = this.getTabHost();
    tabHost.setCurrentTab(0);
    tabHost.clearAllTabs();
    tabHost.setup();
    TabWidget widget = tabHost.getTabWidget();
    widget.setDividerDrawable(R.drawable.split);
    String sNewest = "最新";
    int iNewestId = R.drawable.newesta;
    View newestView = createTabItemView(sNewest, iNewestId);
    TabSpec oTabSpec = tabHost.newTabSpec(sNewest);
    oTabSpec.setIndicator(newestView);
    oTabSpec.setContent(new Intent(this, NewestListActivity.class));
    tabHost.addTab(oTabSpec);
    String sPlate = "板块";
    int iPlateId = R.drawable.platea;
    View plateView = createTabItemView(sPlate, iPlateId);
    tabHost.addTab(tabHost.newTabSpec(sPlate).setIndicator(plateView)
    .setContent(new Intent(this, GroupActivity.class)));
    String sAttention = "关注";
    int iAttentionId = R.drawable.attentiona;
    View attentionView = createTabItemView(sAttention, iAttentionId);
    tabHost.addTab(tabHost.newTabSpec(sAttention)
    .setIndicator(attentionView)
    .setContent(new Intent(this, FavoriteListActivity.class)));
    if (m_IsLogin)
    {
    String sPM = "PM";
    int iPMId = R.drawable.pma;
    View PMView = createTabItemView(sPM, iPMId);
    tabHost.addTab(tabHost.newTabSpec(sPM).setIndicator(PMView)
    .setContent(new Intent(this, PMActivity.class)));
    }
     
    sMore = "更多";
    int iMoreId = R.drawable.morea;
    View moreView = createTabItemView(sMore, iMoreId);
    tabHost.addTab(tabHost.newTabSpec(sMore).setIndicator(moreView)
    .setContent(new Intent(this, MoreActivity.class)));
    // 设置TAB 背景和改变TAB背景
    for (int i = 0; i < widget.getTabCount(); i++)
    {
    View view = widget.getChildTabViewAt(i);
    view.setBackgroundResource(R.drawable.tabback);
    }
    widget.setDividerDrawable(R.drawable.split);
    widget.requestLayout();
    int oIndex = Integer.parseInt(INDEX);
    tabHost.setCurrentTab(oIndex);
    AppRuntime.setMust_Refresh(false);
    }
    }
    看红色的字,是不是有点冲动?
    当OnResume方法执行完了must_refresh 的值永远都是false.放在AppRuntime中,直到我们去改变它.
    我们来改变它吧
    下面的代码是在PersonCenterActivity中的:
    private AlertDialog.Builder LoginDialog()
    {
    AlertDialog.Builder dlg = new AlertDialog.Builder(
    PersonCenterActivity.this);
    dlg.setTitle("登录框");
    dlg.setView(DialogView);
    dialog = dlg.show();
    btn_Pos.setOnClickListener(new OnClickListener()
    {
     
    @Override
    public void onClick(View v)
    {
    mAccount = tName.getText().toString();
    mPwd = tPwd.getText().toString();
    if ("".equals(mAccount.trim()) || "".equals(mPwd.trim()))
    {
    PersonCenterActivity.this.showToast("账号与密码不能为空");
    }
    else
    {
    ActionLogin oActionLogin = new ActionLogin(mAccount,
    mPwd);
    mResult = oActionLogin.doAction();
     
    if (mResult == 1)
    {
    ActionUserDetail oLoadUserDetail = new ActionUserDetail();
    oLoadUserDetail.doAction();
    ExtJSONObject object = oLoadUserDetail
    .getJSONData();
    AppRuntime.setUserData(object);
    AppRuntime.setMust_Refresh(true);
    PersonCenterActivity.this.showToast("登入成功");
    PersonCenterActivity.this.loadData();
    dialog.dismiss();
    }
    else if (mResult == -10)
    {
    PersonCenterActivity.this.showToast("请检查账号");
    return;
    }
    else if (mResult == -1)
    {
     
    PersonCenterActivity.this.showToast("登录失败");
    }
    }
    }
    });
    btn_Nag.setOnClickListener(new OnClickListener()
    {
    @Override
    public void onClick(View v)
    {
    finish();
    }
    });
    return dlg;
     
    }
    看到了吗?这里就又把must_refresh设置回了true.它就可以执行了.这里就是login的状态了.就可以显示出PM模块了.
  • 相关阅读:
    Linux makefile 教程 非常详细,且易懂
    PCRE函数简介和使用示例
    Eclipse常用快捷键汇总
    vc6.0 调用ocx控件
    理解串口流控
    c++ xml markup
    unresolved external symbol __endthreadex错误解决(
    Android自动化~1
    数据库左连接,右连接,内连接,外连接
    Linux常用指令
  • 原文地址:https://www.cnblogs.com/weixiao870428/p/3483597.html
Copyright © 2011-2022 走看看