对不起我的英语。很多东西要学。
我的应用程序中有一个 TabActivity。我希望其中一个按钮在单击时显示上下文菜单。
我设法在单击其中一个选项卡时调用了一个方法。但同时标签调用和 Activity 。这个关闭了当前的那个,它没有给人留下好印象。
一个例子:
将标签添加到标签主机
intent = new Intent().setClass(this, NewsActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec = tabHost.newTabSpec(TAB_NEWS).setIndicator(getString(R.string.tab_news), res.getDrawable(R.drawable.ic_tab_news)).setContent(intent);
tabHost.addTab(spec);
如果我没有通过并且 Intent 给出错误。
intent = new Intent().setClass(this, NewsActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec = tabHost.newTabSpec(TAB_NEWS).setIndicator(getString(R.string.tab_news), res.getDrawable(R.drawable.ic_tab_news)).setContent(intent);
tabHost.addTab(spec);
要调用方法,我使用选项卡上的下一个监听器
tabHost.getChildAt(1).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Somethig to do
e
return false;
}
});
非常感谢。
最佳答案
在选项卡的 OnTouchListener 中,首先检查触摸事件是否为单击,而不仅仅是任何触摸。如果您不希望选项卡在您单击时更改,请在 onTouch 中返回 true。 示例:
tabHost.getChildAt(1).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_UP)
{
//Something to do
return true; // do this if you dont want the tab to change
}
return false;
}
});
关于android - 单击选项卡 Activity 的选项卡而不是调用 Activity 时如何调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10204840/