protected void dialog() {
AlertDialog.Builder builder = new Builder(VideoViewActivity.this);
builder.setMessage("是否进入下一视频");
builder.setTitle("提示");
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
CategoryVideo categoryvideo = new CategoryVideo();
categoryvideo.runnext();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
现在做个播放器 需求是一首歌结束后弹出dialog是否进入下一首,如果不选择那么10秒后自动跳的下一首。
应该怎么做呢????基本的已完成,求教高手~~~~~~~~~
[code="java"]
package com.rp;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TimeCountActivity extends Activity {
private Button btn;
private TextView textView;
Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = new TextView(this);
final AlertDialog alertDialog = new AlertDialog.Builder(this)
.setView(textView).setIcon(R.drawable.icon).setTitle("提示")
.setMessage("是否进入下一视频")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
}).create();
handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what > 0) {
Log.v("time", msg.what + "");
textView.setText(msg.what + "秒自动跳转下一首");
} else {
alertDialog.dismiss();
textView.setText("Over......");
}
super.handleMessage(msg);
}
};
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
alertDialog.setView(textView);
alertDialog.show();
Timer time = new Timer(true);
TimerTask tt = new TimerTask() {
int countTime = 10;
public void run() {
if (countTime > 0) {
countTime--;
}
Message msg = new Message();
msg.what = countTime;
handler.sendMessage(msg);
}
};
time.schedule(tt, 1000, 1000);
}
});
}
}
[/code]
用Java自带的定时器Timer来做不就可以了么?
先显示dialog时间到了把dialog去掉嘛
http://blog.sina.com.cn/s/blog_5033827f0100moir.html
http://hi.baidu.com/lvqiyong/blog/item/a71647ed2fb54820adafd5f4.html
这两个里有介绍
有这么几个方法可以用来解决这个问题:
① 直接新起一个线程,如下:
[code="java"]
new Thread(
new Runnable() {
public void run() {
try{
Thread.sleep(指定时间);
dialog.dismiss();
} catch (Exception e) {
}
}
};
).start();
[/code]
② 在 Android 中有个 AsyncTask 类是专门用来进行这类处理的,你可以在它的 doInBackground 方法里睡眠指定时间,然后调用dialog 的 dismiss() 方法即可,就跟①是一致的
针对你的需要求,第一种方案更简单一点
[code="java"]61行去掉,加重复了view
效果图到我的博客看
[url]
http://dl.iteye.com/upload/picture/pic/97436/fbd0929a-ec35-3940-96f0-03d1c5f5f1b9.png[/url]
[/code]
监听某音频结束可以用MediaPlayer.OnCompletionListener接口
等待执行可以用handler.postDelayed (Runnable r, long delayMillis);
[code="java"]
class Player extends Activity implements [color=red]MediaPlayer.OnCompletionListener[/color]{
MediaPlayer music;
Handler hWait = new Handler();
@Override
public void onCreate(...){
...//TODO 初始化music
[color=red]music.setOnCompletionListener(this);[/color] //设置监听器
}
@Override
public void onCompletion (MediaPlayer mp){
//TODO 显示你的dialog
[color=red]hWait.postDelayed/color{
public void run(){
//TODO 播放下一首,可以加上从dialog返回来的判断执行
}
},
10000); //10000ms=10s;
}
[/code]
[quote]恩 这个我想到过 就是不知道倒计时怎么运用到dialog里 而且时间到了怎么自动关闭dialog也不是很清楚 [/quote]
在线程要结束的时候关掉,你可以参考一下这个小例子,希望对你有帮助。
[url]http://676744379-qq-com.iteye.com/admin/blogs/1123068[/url]
你怎么设置的???
[code="java"]
final AlertDialog alertDialog = new AlertDialog.Builder(this)
.setView(textView).setIcon(R.drawable.icon).setTitle("提示")
.setMessage("是否进入下一视频")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//取消操作
}
})
.create();
[/code]
果断用Dialog风格的Activity就行了
参考:
http://blog.sina.com.cn/s/blog_665a3eaa0100q58j.html
把要弹出的东西写成Activity,你的handler就能写进去起作用了,然后到时间后finish();
点击按键事件写成
[code="java"]setResult(结果码);
finish(); //关闭Activity [/code]
在原来的Activity中启动这个Dialog:
[code="java"]Intent intent = new Intent(this,你的Dialog风格的Activity.class);
startActivityForResult(intent, Dialog请求码自己设置);[/code]
在原来的Activity接受Dialog的返回值
[code="java"]@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch(requestCode){
case Dialog请求码:
if(resultCode==结果码) 处理事件;
break;
}
}[/code]