Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中选择单选按钮选项播放声音_Android - Fatal编程技术网

如何在Android中选择单选按钮选项播放声音

如何在Android中选择单选按钮选项播放声音,android,Android,下面是当用户选择“铃声播放”选项时的字符串 要在带有单选按钮的对话框中显示的字符串: final CharSequence[] items = {" Default", " Vibration", " Best awake ", " Elegent Ringtone " , "Digital phone"}; 1.将声音文件保存在阵列中。定义一个数组,全局显示“音乐” int[] music ; 2.在活动(或片段)中实现DialogInterface.OnClickListener 3.在

下面是当用户选择“铃声播放”选项时的字符串

要在带有单选按钮的对话框中显示的字符串:

final CharSequence[] items = {" Default", " Vibration", " Best awake ", " Elegent Ringtone " , "Digital phone"};

1.将声音文件保存在阵列中。定义一个数组,全局显示“音乐”

int[] music ;
2.在活动(或片段)中实现DialogInterface.OnClickListener

3.在OnCreate中初始化声音分辨率数组和标题数组,并创建单选警报对话框

final CharSequence[] items = {" Default", " Vibration", " Best awake ", " Elegent Ringtone " , "Digital phone"};
music = new int[]{R.raw.one,R.raw.two,R.raw.three,R.raw.four,R.raw.five};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setPositiveButton("OK",this);
    builder.setNegativeButton("Cancel",this);
    builder.setTitle("Choose Sound");
    builder.setSingleChoiceItems(items,-1,this);
    builder.create();
    builder.show();
4.您的DialogInterface.OnClickListener的onCLick将如下所示

 @Override
public void onClick(DialogInterface dialogInterface, int position) {
  switch (position){
      case DialogInterface.BUTTON_POSITIVE :
          //Do your stuffs
          dialogInterface.dismiss();
          break;
      case DialogInterface.BUTTON_NEGATIVE:
          //Do your stuffs
          dialogInterface.dismiss();
          break;
      default: //Single choice item selected
          playSound(music[position]);
          break;
  }
}
private void playSound(int res) {
    MediaPlayer mediaPlayer = MediaPlayer.create(this,res);
    mediaPlayer.start();
}
5.播放声音的方法是这样的

 @Override
public void onClick(DialogInterface dialogInterface, int position) {
  switch (position){
      case DialogInterface.BUTTON_POSITIVE :
          //Do your stuffs
          dialogInterface.dismiss();
          break;
      case DialogInterface.BUTTON_NEGATIVE:
          //Do your stuffs
          dialogInterface.dismiss();
          break;
      default: //Single choice item selected
          playSound(music[position]);
          break;
  }
}
private void playSound(int res) {
    MediaPlayer mediaPlayer = MediaPlayer.create(this,res);
    mediaPlayer.start();
}