代码改变世界

AlertDialog弹出退出对话框和图片对话框

2012-04-20 14:43  omgee  阅读(1299)  评论(0编辑  收藏  举报

一.先建好一个dialog

 

      protected void dialog() { 

             AlertDialog.Builder builder = new Builder(LinkMap.this); 
            
             builder.setMessage("确定要退出吗?"); 

             builder.setTitle("提示"); 
             builder.setPositiveButton("确认", 

                 new android.content.DialogInterface.OnClickListener() { 
 
                         @Override
 
                         public void onClick(DialogInterface dialog, int which) { 
 
                             //dialog.dismiss(); 
 
                             //LinkMap.this.finish(); 
                        //     onDestroy();
                         
                              //或直接结束进程
                             try
                             {
                                 finish();
                                 System.exit(0);
                             android.os.Process.killProcess(android.os.Process.myPid()); 
                             }
                             catch (Exception ex)
                             {
                              Log.i("error",ex.getMessage());
                             }
                             
                                Intent  intent=new Intent(LinkMap.this,OmgeTCEActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  //注意本行的FLAG设置 
                                 setResult(0,intent); //这理有2个参数(int resultCode, Intent intent)
                                startActivity(intent); 
                             
                             
                         } 
 
                     });
              

 
             builder.setNegativeButton("取消", 
                     new android.content.DialogInterface.OnClickListener() { 
 

                         @Override
 
                         public void onClick(DialogInterface dialog, int which) { 
                             dialog.dismiss(); 
                             
                     
                         } 
 
                     }); 
 
             builder.create().show(); 
 
         } 

二.写返回按钮触发事件

 public boolean onKeyDown(int keyCode, KeyEvent event) { 
 
             if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
                 dialog(); 
                 return false; 
             } 
             return false;
         }


效果如下:省略

弹出包含图片的对话框

其实很简单

AlertDialog.builder有一个setview方法,你只要写一个xml里面imgeview加载你的图片,再setview你的视图就ok了

在一个view中加载子view

       View mPopView=super.getLayoutInflater().inflate(R.layout.popview, null);
          View  mMapView.addView( mPopView,
                    new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                            null, MapView.LayoutParams.TOP_LEFT));
        
            
            mPopView.setVisibility(View.VISIBLE);

一个view...progress.xml

<?xml version="1.0" encoding="utf-8"?>
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
 android:id="@+id/progress_img"
    >
    
    <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:src="@drawable/ic_mytce"
        />
    
  </RelativeLayout>

 


dialog中加载图片,实现无按钮自动弹出进程框与关闭

 //进程图片加载框
      public   AlertDialog.Builder dialog_progress()
      {
           AlertDialog.Builder builder = new Builder(LinkMap.this);
           View view =super.getLayoutInflater().inflate(R.layout.progress,(ViewGroup) findViewById(R.id.progress_img));           
           builder.setView(view);
           return builder;
           
      }
      

 

 显示dialog,关闭dialog

     AlertDialog dialog_pro=null;
 


//进程加载 dialog_pro=dialog_progress().create(); dialog_pro.show(); //加载完成,退出加载进程 dialog_pro.dismiss();

 

  

 

 

 完成