android中的对话框怎样让他自动消失
推荐于2019-01-24 · 知道合伙人影视综艺行家
关注
展开全部
public class MyDialog extends Dialog {
private int FLAG_DISMISS = 1;
private boolean flag = true;
public MyDialog(Context context) {
super(context);
setTitle("自动消失对话框测试!");
}
@Override
public void show() {
super.show();
mThread.start();
}
@Override
public void dismiss() {
super.dismiss();
flag = false;
}
private Thread mThread = new Thread(){
@Override
public void run() {
super.run();
while(flag){
try {
Thread.sleep(2000);
Message msg = mHandler.obtainMessage();
msg.what = FLAG_DISMISS;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == FLAG_DISMISS)
dismiss();
}
};
}
LZ 筒子,AlertDialog都被框死用来做警告用的,不好操作。想实现自己的对话框还是继承Dialog然后DIY更好,选择也多得多了。我这个自定义的Dialog就是显示了2s然后消失的,测试过没问题。实现起来也相当之简单,抛砖引玉,期待更好的方法。
private int FLAG_DISMISS = 1;
private boolean flag = true;
public MyDialog(Context context) {
super(context);
setTitle("自动消失对话框测试!");
}
@Override
public void show() {
super.show();
mThread.start();
}
@Override
public void dismiss() {
super.dismiss();
flag = false;
}
private Thread mThread = new Thread(){
@Override
public void run() {
super.run();
while(flag){
try {
Thread.sleep(2000);
Message msg = mHandler.obtainMessage();
msg.what = FLAG_DISMISS;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == FLAG_DISMISS)
dismiss();
}
};
}
LZ 筒子,AlertDialog都被框死用来做警告用的,不好操作。想实现自己的对话框还是继承Dialog然后DIY更好,选择也多得多了。我这个自定义的Dialog就是显示了2s然后消失的,测试过没问题。实现起来也相当之简单,抛砖引玉,期待更好的方法。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询