点击我当前选中的标签时,我正试图获取Click事件 TabActivity。
我尝试了下面的代码但是当我点击一个标签时,其他标签无法正常工作/点击。
setupTab(new TextView(this), "Map");
setupTab(new TextView(this), "Attacks");
setupTab(new TextView(this), "Profile");
setupTab(new TextView(this), "Headquater");
int numberOfTabs = tabHost.getTabWidget().getChildCount();
for(int t=0; t<numberOfTabs; t++){
getTabWidget().getChildAt(t).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(tabHost.getCurrentTab() == 0){
Toast.makeText(TabContext, ""+"i m in on clickkkkk" ,1500).show();
getTabHost().setCurrentTab(0);
}
if(tabHost.getCurrentTab() == 1){
Toast.makeText(TabContext, ""+"i m in on clickkkkk....$#@$#$" ,1500).show();
getTabHost().setCurrentTab(1);
}
}
});
}
答案 0 :(得分:9)
我需要仅针对一个标签检测第二次点击,因此this answer为我工作。诀窍是为孩子设置OnClick,而不是为TabHost本身设置。
getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TAG", "Clicked tab : "+getTabHost().getCurrentTabTag());
tabHost.setCurrentTab(0);
}
});
答案 1 :(得分:3)
我使用了捷克语解决方案,但稍作修改,因为向子级而不是向标签主机本身添加监听器会导致tabhost侦听器混淆读取代码中的注释以获得更好的分类
mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// re enforce selecting targeted tab to re apply main listener to default behaviour
mTabHost.setCurrentTab(0);
// display current tab tag in console
Log.i("MainActivity", "Clicked tab : "+mTabHost.getCurrentTabTag());
// identify targeted fragment
Fragment currentFragment = new "FragmentClassName"();
// execute navigation (fragment transaction)
fragmentManager.beginTransaction()
.replace(R.id.content_frame, currentFragment)
.commit();
// re enforce selecting targeted tab to re apply main listener to default behaviour
mTabHost.setCurrentTab(0);
}
});
答案 2 :(得分:2)
在 onCreate 方法内:
for(int i = 0; i < TOTAL_TAB; i++) {
tabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
tabHost.getTabWidget().getChildAt(i).setTag(i+"");
}
您的 听众 :
@Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_DOWN == event.getAction()) {
previousTab = tabHost.getCurrentTab();
currentTab = Integer.parseInt((String)v.getTag());
if(previousTab == currentTab) {
if(currentTab == 0){
Log.i("log", "0th same tab selected");
}else if(currentTab == 1){
Log.i("log", "1st same tab selected");
}else if(currentTab == 2){
Log.i("log", "2nd same tab selected");
}else if(currentTab == 3){
Log.i("log", "3rd same tab selected");
}
return true;
}
}
return false;
}