Android——从内部选项卡活动更改选项卡可见性
我目前正在尝试从其他活动内部更改选项卡——这很简单。但是,我在尝试设置其他选项卡的可见性时遇到了麻烦。
本质上,我有一个应用程序加载到一个选项卡(登录页面),并且其他选项卡在用户登录之前不可见。
当用户登录时,我想让其他选项卡可见而登录选项卡不可见。
如果有一个简单的方法可以做到这一点,请让我知道它 - 我目前正在绕圈:
代码片段:
在 TabHost 的 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 的额外方法:
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 如下:
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)
其显然,异常来自不可见/可见设置。
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 Snippets:
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);
....
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);
}
The Tab Activity:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道
tabHost.getTabWidget().getChildAt(0)
是否是正确的方法。您是否尝试过为选项卡提供单独的 ID?或者,如果您想尝试跟踪错误的确切位置,请抛出一堆断点并在调试模式下运行它。
哦
Button btn = (Button)this.findViewById(R.id.login_button);
我相信没有“this”也可以(虽然我可能是错的......fwiw我从未使用过this
在按钮声明中)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.
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 usedthis
in a button declaration)