问题
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