0
播放聲音的選擇單選按鈕選項下面這裏字符串時,用戶在選擇手機鈴聲播放選擇..如何在Android的
字符串對話框顯示與單選按鈕:
final CharSequence[] items = {" Default", " Vibration", " Best awake ", " Elegent Ringtone " , "Digital phone"};
播放聲音的選擇單選按鈕選項下面這裏字符串時,用戶在選擇手機鈴聲播放選擇..如何在Android的
字符串對話框顯示與單選按鈕:
final CharSequence[] items = {" Default", " Vibration", " Best awake ", " Elegent Ringtone " , "Digital phone"};
1.Keep您聲音文件在一個數組中。在全球範圍內定義一個表示「音樂」的數組。
int[] music ;
2.Implement DialogInterface.OnClickListener您的活動(或片段)
3.Initialize在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.您的on dialog of DialogInterface.OnClickListener看起來應該看起來像這樣
@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;
}
}
5. playsound方法是這樣的
private void playSound(int res) {
MediaPlayer mediaPlayer = MediaPlayer.create(this,res);
mediaPlayer.start();
}