Play Sound on Tab Click

若如初见. 提交于 2019-12-11 18:19:15

问题


On an Android phone from AT&T, you can hear a click sound when navigating through tabs. How do I get the Tab widget to play a sound on the click of a tab?

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        //PLAY SOUND HERE
        MediaPlayer tabClick = MediaPlayer.create(TabBarActivity.this, R.Raw.gling_click);
        tabClick.start();
    }
)};

回答1:


step 1 from the tutorial is this:

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)  
{  
    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_blue_matte);  
}  
    tabHost.getTabWidget().setCurrentTab(1);  
    tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.bg_green_matte);  

    //NEW CODE HERE **
    tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            //PLAY SOUND HERE
        }

    )};

See where it says NEW CODE HERE **? You want to add an OnTabChangedListener to the tabHost. This is an object that is "listening" for the user to change tabs. When a tab changes, the onTabChanged(String tabID) method is run.

Add the onTabChangedListener after tutorial step 1; It's within the onCreate() method.

Then, get on google and look up android play a sound and get that code into where it says //PLAY SOUND HERE.



来源:https://stackoverflow.com/questions/10237097/play-sound-on-tab-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!