是否可以为导航抽屉按钮添加声音?

时间:2014-12-04 05:02:45

标签: android button audio navigation drawer

可以在导航抽屉按钮上添加声音吗?我设法通过这样做将声音添加到普通按钮中;

final MediaPlayer sound = MediaPlayer.create(getApplicationContext(), R.raw.buttonsound)
Button button = (button) findViewById(R.id.button);
ok.setOnClickListener(new View.onClickListener(){
public void onClick(View arg0){
sound.start();
}
});

1 个答案:

答案 0 :(得分:0)

您的Navigation-drawer片段中必须有回调。此回调接口应在您的主活动中实现。然后你可以覆盖onClick。

从导航抽屉:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);
        mDrawerListView = (ListView) rootView.findViewById(R.id.listView);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //A very basic callback, ideally you would want to perform null-checks
        mCallbacks.onNavigationDrawerItemSelected(position);
            }
        });

回调界面:

public static interface NavigationDrawerCallbacks {
    /**
     * Called when an item in the navigation drawer is selected.
     */
    void onNavigationDrawerItemSelected(int position);
    void onNotificationClicked();
}

在MainActivity中实现此界面:

public class MyActivity extends FragmentActivity implements

        NavigationDrawerFragment.NavigationDrawerCallbacks,.......

最后覆盖onClick:

    @Override
    public void onNavigationDrawerItemSelected(int position) {

        switch (position) {

            case 0 : playMusicAndHandleClick();
    }
}