2013-12-14 210 views
2

我正在使用我的应用程序,这是我的代码,请帮助我在我的AlertDialog上添加声音。例如,如果我选择在我的警报"Yes"对话框通知“You got it"是对声音的功能。带声音的Android警报对话框

package com.example.radiobbutton; 



import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.widget.RadioGroup; 
import android.widget.Toast; 
import android.widget.RadioGroup.OnCheckedChangeListener; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1); 
     rg.setOnCheckedChangeListener(new OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(RadioGroup group, final int checkedId) 
      { 
       AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
         MainActivity.this); 

       alertDialog2.setTitle("Confirm Answer..."); 

       // Setting Dialog Message 
       alertDialog2.setMessage("Is that your final Answer?"); 

       alertDialog2.setPositiveButton("YES", 
         new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, int which) { 


       switch(checkedId) 
       { 
       case R.id.radio0: 
        Toast.makeText(getApplicationContext(), 
          "You got it", Toast.LENGTH_SHORT) 
          .show(); 
        break; 
       case R.id.radio1: 
        Toast.makeText(getApplicationContext(), 
          "Wrong", Toast.LENGTH_SHORT) 
          .show(); 
        break; 
       case R.id.radio2: 
        Toast.makeText(getApplicationContext(), 
          "Draw", Toast.LENGTH_SHORT) 
          .show(); 
        break; 

       } 
       } 
      } 
     ); 

       alertDialog2.setNegativeButton("NO", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // Write your code here to execute after dialog 
           Toast.makeText(getApplicationContext(), 
             "Choose again", Toast.LENGTH_SHORT) 
             .show(); 
           dialog.cancel(); 
          } 
         }); 

        alertDialog2.show(); 

    } 

     }); 

} 

} 

回答

2

把声音剪辑了Android原文件夹内。

MediaPlayer的对象内部警报阳性的onclick功能的Android

public void onClick(DialogInterface dialog, int which) { 
final MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.clip); 
       mp.start(); 
       switch(checkedId) 
       { 
       case R.id.radio0: 
        Toast.makeText(getApplicationContext(), 
          "You got it", Toast.LENGTH_SHORT) 
          .show(); 
        break; 
       case R.id.radio1: 
        Toast.makeText(getApplicationContext(), 
          "Wrong", Toast.LENGTH_SHORT) 
          .show(); 
        break; 
       case R.id.radio2: 
        Toast.makeText(getApplicationContext(), 
          "Draw", Toast.LENGTH_SHORT) 
          .show(); 
        break; 

       } 
       } 
      } 
     ); 
+0

他们是mp.start后一个错误“语法错误,插入‘AssignmentOperator表达’来完成表达”建议立即进行删除我该怎么办? – chicharp

+0

对不起,mp.start(); – Nambi

+0

我正在处理和现在的错误在.create“方法创建(上下文,int)的类型MediaPlayer不适用于参数(新的DialogInterface.OnClickListener(){},int)”我怎么能修理它? – chicharp