1

Sorry for my English. Much to learn.

I have a TabActivity in my app. I would like that one of the buttons show a context menu when it is clicked.

I managed to call a method on click one of the tabs. But in the same time the tab calls and activity. This one closes the current one which it doesn't make good impression.

An example:

Adding the tabs to the tabhost

    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);

If I don't pass and intent gives error.

    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);

To Call the method I use the next listener on the tab

tabHost.getChildAt(1).setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            //Somethig to do
            e
            return false;
        }
    });

Thank you very much.

4

1 回答 1

1

In the tab's OnTouchListener, first check if the touch event is a click and not just any touch. If you don't want the tab to change when you click it, return true in onTouch. Example:

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;
        }
    });
于 2012-04-18T07:49:35.947 回答