android 自定义Alertdialog对话框用于耗时加载

来源:互联网 发布:银行数据录入员好学吗 编辑:程序博客网 时间:2024/05/22 15:31

本文维护中。。。。(之前写的编码乱了,导致部分显示出错)

更好的自定义Dialog方式请戳这里(别人写的):http://blog.csdn.net/duanyanrui/article/details/8494767

android的AlertDialog对话框是非阻塞的,如果要使用阻塞的对话框需要popupwindow。

先说一下这里要模拟的效果,当我们点击一些选项的时候(比如检测软件更新等),这些会启动一些任务,如果这些非常的耗时任务(访问网络)长时间没有响应android就会出现anr错误,解决办法就是在后台线程中进行这些耗时任务,然后ui线程(主线程)中显示加载的对话框(就是一直转圈的效果,也有些是做成其他的卡通一直跳),后台耗时任务结束后对话框消失。接下来看代码。

/** * 用途:可以用于加载页面,但是我这里只有一个线程,要扩展的话可以在setDoInBackground()方法中多写几个参数多new几个DoInBTask对象 * 暂时是只想到这种方法,否则的话就要全部重写(线程较多的话还是不建议使用我这种方式,不过原理上也可以借鉴),应该能使用在大部分加载的地方, * 加载的图片什么的可以自己再自定义,主要修改showDialog方法中的代码 * @author Sqq * */public class WaitDialog {private Context context;AlertDialog dialog;private boolean touchcancelable = false;private backTask bt;private DoInBTask dbt;public WaitDialog(Context con){context = con;}public WaitDialog showDialog(String mess){dialog = new AlertDialog.Builder(context).create();Window mWindow = dialog.getWindow();mWindow.setGravity(Gravity.CENTER);dialog.show();dialog.setCancelable(touchcancelable);dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);dialog.setOnDismissListener(new OnDismissListener() {@Overridepublic void onDismiss(DialogInterface arg0) {if(dbt!=null&&dbt.getStatus()!=AsyncTask.Status.FINISHED)dbt.cancel(true);}});Window window = dialog.getWindow();window.setContentView(R.layout.doinb_dialog);TextView v = (TextView) window.findViewById(R.id.d_tip);v.setText(mess);return this;}public void setTouchcancelable(boolean touchcancelable) {this.touchcancelable = touchcancelable;dialog.setCancelable(touchcancelable);}public boolean isTouchcancelable() {return touchcancelable;}public WaitDialog setDoInBackground(backTask task){bt = task;dbt = new DoInBTask();dbt.execute();return this;}        public interface backTask{public boolean doInBackground();public void taskEnd(boolean ret);}public abstract class DoInBTask extends AsyncTask<Void,Void,Boolean>{@Overrideprotected Boolean doInBackground(Void... arg0) {Log.d("sqq", "还在运行");if(bt.doInBackground()){return true;}return false;}@Overrideprotected void onPostExecute(Boolean result) {Log.d("sqq", "要结束了");if(dialog!=null){if(dialog.isShowing()){dialog.dismiss();}}bt.taskEnd(result);super.onPostExecute(result);}@Overrideprotected void onCancelled() {Log.d("sqq", "取消了");super.onCancelled();}}}


上面的代码主要的逻辑就是在showDialog实现一个自定义的AlertDialog,比较彻底的自定义方式就是dialog.getwindow得到Window,对这个得到的window.setContentview,这样得到的dialog的view就是我们再布局文件(R.layout.doinb_dialog)中定义的view,没有AlertDialog自带的几个按钮属性。

除了自定义AlertDialog,还有一点就是在设置回调的时候要启动后台的线程DoInBTask,这个线程继承自AsyncTask这个抽象类,我在其中做的操作就是在任务结束时让dialog消失,而那个接口的作用就是让使用这个类的人去实现他要在线程中做的事情,以及线程结束后要做的事情。

最后来看看使用方法

public class MainActivity extends Activity {Button bt;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt = (Button) findViewById(R.id.doinb);bt.setText("加载");}public void onClick(View v){switch (v.getId()) {case R.id.doinb:WaitDialog dialog = new WaitDialog(this).showDialog("正在加载").setDoInBackground(new backTask() {@Overridepublic boolean doInBackground() {// 后台任务try {Thread.sleep(5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return true;}@Overridepublic void taskEnd(boolean ret) {// 任务结束,可操作ui组件bt.setText("成功了");}});/** * 默认是false不能点击取消 */dialog.setTouchcancelable(true);break;default:break;}}}

WaitDialog的实现也可以不用上面的回调的方式,改写一下

public class WaitDialog {private Context context;AlertDialog dialog;private boolean touchcancelable = false;//private backTask bt;private DoInBTask dbt;public WaitDialog(Context con){context = con;}public WaitDialog showDialog(String mess){dialog = new AlertDialog.Builder(context).create();Window mWindow = dialog.getWindow();mWindow.setGravity(Gravity.CENTER);dialog.show();dialog.setCancelable(touchcancelable);dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);dialog.setOnDismissListener(new OnDismissListener() {@Overridepublic void onDismiss(DialogInterface arg0) {if(dbt!=null&&dbt.getStatus()!=AsyncTask.Status.FINISHED)dbt.cancel(true);}});Window window = dialog.getWindow();window.setContentView(R.layout.doinb_dialog);TextView v = (TextView) window.findViewById(R.id.d_tip);v.setText(mess);return this;}public void setTouchcancelable(boolean touchcancelable) {this.touchcancelable = touchcancelable;dialog.setCancelable(touchcancelable);}public boolean isTouchcancelable() {return touchcancelable;}public WaitDialog setDoInBackground(DoInBTask task/*backTask task*/){/*bt = task;dbt = new DoInBTask();*/dbt = task;dbt.execute();return this;}/*public interface backTask{public boolean doInBackground();public void taskEnd(boolean ret);}*/public abstract class DoInBTask extends AsyncTask<Void,Void,Boolean>{/*@Overrideprotected Boolean doInBackground(Void... arg0) {Log.d("sqq", "还在运行");if(bt.doInBackground()){return true;}return false;}*/@Overrideprotected void onPostExecute(Boolean result) {Log.d("sqq", "要结束了");if(dialog!=null){if(dialog.isShowing()){dialog.dismiss();}}//bt.taskEnd(result);super.onPostExecute(result);}@Overrideprotected void onCancelled() {Log.d("sqq", "取消了");super.onCancelled();}}}
public class MainActivity extends Activity {Button bt;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt = (Button) findViewById(R.id.doinb);bt.setText("加载");}public void onClick(View v){switch (v.getId()) {case R.id.doinb:/*WaitDialog dialog = new WaitDialog(this).showDialog("正在加载").setDoInBackground(new backTask() {@Overridepublic boolean doInBackground() {String dir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/aaa/ddd";File filet = new File(dir);if(!filet.exists()){Log.d("sqq", "cunzai");filet.mkdirs();// 新建文件夹}// 后台任务try {Thread.sleep(5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return true;}@Overridepublic void taskEnd(boolean ret) {// 任务结束,可操作ui组件bt.setText("成功了");}});*/WaitDialog dialog = new WaitDialog(this);dialog.showDialog("正在加载").setDoInBackground(dialog.new DoInBTask() {@Overrideprotected Boolean doInBackground(Void... arg0) {// 后台任务try {Thread.sleep(5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}return true;}@Overrideprotected void onPostExecute(Boolean result) {// 任务结束,可操作ui组件bt.setText("成功了");super.onPostExecute(result);}});/** * 默认是false不能点击取消 */dialog.setTouchcancelable(true);break;default:break;}}}



源码:http://download.csdn.net/detail/u012806692/9450035

0 0
原创粉丝点击