1. 程式人生 > >Dialog的四種用法

Dialog的四種用法

 對話方塊Dialog在android基本用法很多種簡單介紹一下以下四種用法

 Button btn1;
 Button btn2;
 Button btn3;
 Button btn4;

 final int DIALOG_ONE=1;
 final int DIALOG_TWO=2;

    btn1= (Button)findViewById(R.id.button1);
        btn2= (Button)findViewById(R.id.button2);
        btn3= (Button)findViewById(R.id.button3);
        btn4= (Button)findViewById(R.id.button4);

第一種簡單的點選事件:

 btn1.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Dialog dlg;//1定義Dialog對話方塊
    Builder bd;/2/定義構建器
    bd = new AlertDialog.Builder(DialogProjectActivity.this);
    bd.setTitle("標題");
    bd.setMessage("訊息");
    bd.setPositiveButton("確定",new DialogInterface.OnClickListener() {
     
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      
     }
    });
    
    dlg = bd.create();//3建立構建器並將其內容賦給對話方塊
    dlg.show();//4顯示對話方塊
    
   }
  });

第二個自定義函式實現:

       btn2.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
   MyDialog().show(); 
   }

   private Dialog MyDialog() {
    // TODO Auto-generated method stub
    Dialog dlg;
    Builder bd;
    bd = new AlertDialog.Builder(DialogProjectActivity.this);
    bd.setTitle("標題");
    bd.setMessage("訊息");
    bd.setPositiveButton("確定",new DialogInterface.OnClickListener() {
     
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      
     }
    });
    
    dlg = bd.create();
  
    return dlg;
   }
  });

 

第三種重寫OncreateDialog(int)方法:

定義重寫

 @Override
 protected Dialog onCreateDialog(int id) {
  // TODO Auto-generated method stub

  Dialog dlg;
  Builder bd = null;
  switch(id)
  {
  case DIALOG_ONE:
   bd = new AlertDialog.Builder(DialogProjectActivity.this);
   bd.setTitle("標題");
   bd.setMessage("訊息");
   bd.setPositiveButton("確定",new DialogInterface.OnClickListener() {
    
    public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
     
    }
   });
   
  
   break;
  case DIALOG_TWO:
   bd = new AlertDialog.Builder(DialogProjectActivity.this);
   bd.setTitle("標題");
   bd.setMessage("訊息");
   bd.setPositiveButton("確定",new DialogInterface.OnClickListener() {
    
    public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
     
    }
   });
   
  
   break;
  
  }
  
   dlg = bd.create();
   dlg.show();
  return dlg;
 }

實現

  btn3.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    showDialog(DIALOG_TWO);
   }
  });

 

第四種自定義實現類:


public class DialogTest extends Dialog {

 public DialogTest(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dialog);
 }

}

 btn4.setOnClickListener(new OnClickListener() {
  
  public void onClick(View v) {
   // TODO Auto-generated method stub
   DialogTest dt = new DialogTest(DialogProjectActivity.this);
   dt.show();
  }
 })   ;