这是我的MainActivity中代码的一部分。 stopwatch1是MainActivity中调用的图像按钮:
stopwatch1 = (ImageButton)findViewById(R.id.stopwatch1);
我已经尝试过搜索有关如何使用dialog.setOnDismissListener的线索,但是,没有正确的答案来帮助我在故意或意外退出对话后杀死音频。任何帮助将不胜感激。
stopwatch1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog mmDialog1 = new Dialog(context);
mmDialog1.setContentView(R.layout.countdowntimer);
mmDialog1.setTitle("Stop-watch");
Button ddButton1 = (Button) mmDialog1.findViewById(R.id.countdown_exit);
final Button ddButton2 = (Button) mmDialog1.findViewById(R.id.countdown_start);
final TextView timeview = (TextView) mmDialog1.findViewById(R.id.timeview);
ddButton2.setTag(1);
ddButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final int status = (Integer) v.getTag();
if (status == 1) {
ddButton2.setText("Stop");
countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
int time = (int) (millisUntilFinished/1000);
timeview.setText(Integer.toString(time));
}
@Override
public void onFinish() {
timeview.setText("60");
ddButton2.setText("Start");
MediaPlayer stop = MediaPlayer.create(context, R.raw.stop);
stop.start();
}
}.start();
v.setTag(0);
} else {
ddButton2.setText("Start");
timeview.setText("60");
countDownTimer.cancel();
v.setTag(1);
}
}
});
ddButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mmDialog1.dismiss();
}
});
mmDialog1.show();
}
});
答案 0 :(得分:2)
我的建议是实现一个接口。对话框关闭后,将回调关闭媒体播放器。
这是一个简单的例子。
<强>接口强>
public interface DialogClosed {
void shutDownMediaPlayer();
}
然后在调用活动
中实现接口<强>活性强>
public class MainActivity extends Activity implements DialogClosed {
....
@Override
public void shutDownMediaPlayer() {
//shut down media player here
}
}
此外,您可以添加一个布尔值来检查对话框是否显示为
boolean isShowing = mDialog.isShowing();
然后你可以随时关闭媒体播放器,如果这是假的话。
答案 1 :(得分:0)
所以我使用了Jawascript中的一些指针,并且稍微编辑了我的代码,现在它可以工作了:
stopwatch1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog mmDialog1 = new Dialog(context);
mmDialog1.setContentView(R.layout.countdowntimer);
mmDialog1.setTitle("Stop-watch");
Button ddButton1 = (Button) mmDialog1.findViewById(R.id.countdown_exit);
final Button ddButton2 = (Button) mmDialog1.findViewById(R.id.countdown_start);
final TextView timeview = (TextView) mmDialog1.findViewById(R.id.timeview);
final MediaPlayer stop = MediaPlayer.create(context, R.raw.stop);
ddButton2.setTag(1);
mmDialog1.setCancelable(false);
if (mmDialog1.isShowing() == false) {
stop.stop();
}
ddButton2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
final int status = (Integer) v.getTag();
if (status == 1) {
ddButton2.setText("Stop");
countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
int time = (int) (millisUntilFinished/1000);
timeview.setText(Integer.toString(time));
}
@Override
public void onFinish() {
timeview.setText("60");
ddButton2.setText("Start");
try {
stop.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
stop.start();
}
}.start();
v.setTag(0);
} else {
ddButton2.setText("Start");
timeview.setText("60");
countDownTimer.cancel();
v.setTag(1);
stop.stop();
}
}
});
ddButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mmDialog1.dismiss();
}
});
mmDialog1.show();
}
});