Android的 - 来自内部的标签更改活动标签可视性 [英] Android -- Changing Tab Visibility from Inside Tab Activities

查看:191
本文介绍了Android的 - 来自内部的标签更改活动标签可视性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试图从其他活动的内部更改选项卡 - 这一点是直线前进。但是,试图设置其他标签的可见性时,我遇到了麻烦。

I'm currently attempting to change tabs from inside of other activities -- that much is straight forward. However, I'm having trouble when attempting to set the visibility of other tabs.

从本质上讲,我有一个应用程序加载到一个标签页(登录页)以及其它选项卡是不可见的,直到用户登录。

Essentially, I have an application load to a tab (login page) and the other tabs are invisible until the user logs in.

当用户登录时,我想让​​其他选项卡可见,登录选项卡不可见。

When the user logs in, I want to make the other tabs visible and the login tab invisible.

如果有这样做的一个简单的方法,请让我意识到这一点 - 我现在手忙脚乱:

If there is a simple way of doing this, please make me aware of it--I'm currently running around in circles:

code代码片段:

在TabHost的OnCreate:

In the TabHost's OnCreate:

.....

tabHost.setCurrentTab(3);

tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
tabHost.getTabWidget().getChildAt(1).setVisibility(View.GONE);
tabHost.getTabWidget().getChildAt(2).setVisibility(View.GONE);

....

该TabHost的额外的方法:

The TabHost's Extra Methods:

public void switchTab(int tab){
    tabHost.setCurrentTab(tab);
}

public void visibleTabs(){
    tabHost.getTabWidget().getChildAt(0).setVisibility(View.VISIBLE);
    tabHost.getTabWidget().getChildAt(1).setVisibility(View.VISIBLE);
    tabHost.getTabWidget().getChildAt(2).setVisibility(View.VISIBLE);
    tabHost.getTabWidget().getChildAt(3).setVisibility(View.GONE);

}

public void invisibleTabs(){

    tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
    tabHost.getTabWidget().getChildAt(1).setVisibility(View.GONE);
    tabHost.getTabWidget().getChildAt(2).setVisibility(View.GONE);

}

标签活动:

public class LoginActivity extends Activity {



EditText txt_username;
EditText txt_password;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



   setContentView(R.layout.login);


    Button btn = (Button)this.findViewById(R.id.login_button);

    final EditText txt_username = (EditText) findViewById(R.id.txt_username);
    final EditText txt_password = (EditText) findViewById(R.id.txt_username);


    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            String username = txt_username.getText().toString();
            String password = txt_password.getText().toString(); 

            switchTabInActivity(0);

        }
    });


}

public void switchTabInActivity(long indexTabToSwitchTo){

    waiter ParentActivity = (waiter) this.getParent();
    ParentActivity.visibleTabs();
    ParentActivity.switchTab(0);

}


}

LogCat中如下:

LogCat is As Follows:

04-12 19:37:49.825: ERROR/AndroidRuntime(2744): FATAL EXCEPTION: main

04-12 19:37:49.825: ERROR/AndroidRuntime(2744): java.lang.NullPointerException

04-12 19:37:49.825: ERROR/AndroidRuntime(2744):     at android.waiter.waiter.visibleTabs(waiter.java:130)

04-12 19:37:49.825: ERROR/AndroidRuntime(2744):     at android.waiter.LoginActivity.switchTabInActivity(LoginActivity.java:56)

04-12 19:37:49.825: ERROR/AndroidRuntime(2744):     at android.waiter.LoginActivity$1.onClick(LoginActivity.java:44)

其明显的异常是从无形/可见设置的到来。

Its obvious that the exception is coming from the invisible/visible settings.

推荐答案

我不知道,如果 tabHost.getTabWidget()。getChildAt(0)才是正道去做吧。您是否尝试过给标签的个人ID?

I don't know if tabHost.getTabWidget().getChildAt(0) is the right way to do it. Have you tried giving the tabs an individual ID?

另外,如果你想尝试和跟踪误差的确切位置抛出一堆断点,并在调试模式下运行它。

alternatively, if you want to try and track the exact location of the error throw a bunch of breakpoints and run it in Debug mode.

按钮BTN =(按钮)this.findViewById(R.id.login_button); 我相信没有可以做一个本(虽然我可能是错的。 ... FWIW我从来没有使用这个的一个按钮声明)

Oh Button btn = (Button)this.findViewById(R.id.login_button); I believe can do without a "this" (although I could be wrong.... fwiw i never used this in a button declaration)

这篇关于Android的 - 来自内部的标签更改活动标签可视性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆