我正在尝试通过片段中存在的菜单项播放声音文件。
以下是我的片段的完整代码,
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_one, container,false);
ImageButton popupCTelevision = (ImageButton)rootview.findViewById(R.id.chineseTelevision);
ImageButton popupCBathe = (ImageButton)rootview.findViewById(R.id.chineseBathe);
ImageButton popupCSick = (ImageButton)rootview.findViewById(R.id.chineseSick);
popupCTelevision.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), v);
popupMenu.inflate(R.menu.menu_chinese_television);
popupMenu.show();
}
});
popupCBathe.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), v);
popupMenu.inflate(R.menu.menu_chinese_bathe);
popupMenu.show();
}
});
popupCSick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(getActivity().getApplicationContext(), v);
popupMenu.inflate(R.menu.menu_chinese_sick);
popupMenu.show();
}
});
return rootview;
}
public boolean onMenuItemClick(MenuItem item){
switch(item.getItemId()) {
case R.id.c_bathe:
final MediaPlayer CSick = MediaPlayer.create(getActivity(), R.raw.c_bathe);
MenuItem playCSick = (MenuItem) getView().findViewById(R.id.c_sick);
CSick.start();
playCSick.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
CSick.start();
return true;
}
});
default:
return false;
}
}
}
我认为问题在于根本没有使用方法onMenuItemClick
。如何为代码实现此方法?
如何按下按钮播放声音文件?
final MediaPlayer englishSoundMP = MediaPlayer.create(this, R.raw.english_bath);
Button playEnglishBath = (Button) this.findViewById(R.id.play_english);
playEnglishBath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
englishSoundMP.start();
}
});