带警报的Android警报对话框

时间:2013-12-14 14:32:18

标签: java android eclipse

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

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();

    }

        });

}

}

1 个答案:

答案 0 :(得分:2)

将声音片段放在android原始文件夹中。

MediaPlayer 对象置于警告postive 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;

                }
                }
            }
        );