android 自訂對話方塊

來源:互聯網
上載者:User

說到對話方塊你肯定會想到AlertDialog.Builder。當然這次不是用AlertDialog.Builder來實現的!而是Dialog類

AlertDialog.Builder提供的方法有:
setTitle():給對話方塊設定title.
setIcon():給對話方塊設定表徵圖。
setMessage():設定對話方塊的提示資訊
setItems():設定對話方塊要顯示的一個list,一般用於要顯示幾個命令時
setSingleChoiceItems():設定對話方塊顯示一個單選的List
setMultiChoiceItems():用來設定對話方塊顯示一系列的複選框。
setPositiveButton():給對話方塊添加”Yes”按鈕。
setNegativeButton():給對話方塊添加”No”按鈕。
那麼在Dialog類怎樣實現的呢?當然是layout啦,你可以自訂一個xml來布置你對話方塊
看看例子和源碼吧
package com.hl;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MyDialog extends Activity implements android.view.View.OnClickListener {
 Button btn1=null;
    Button btn2=null;
    Button btn3=null;
 @Override
  public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         btn1=(Button)findViewById(R.id.b1);
         btn2=(Button)findViewById(R.id.b2);
         btn3=(Button)findViewById(R.id.b3);
         btn1.setOnClickListener(this);
         btn2.setOnClickListener(this);
         btn3.setOnClickListener(this);
     }
 @Override
 public void onClick(View v) {
  switch(v.getId()){
        case R.id.b1:
            break;
        case R.id.b2:
        case R.id.b3:
            new MyDialogs(this).setDisplay();
            break;
        default:
  
 }
}
class MyDialogs extends Dialog implements android.view.View.OnClickListener{
 private Button b1;
    private Window window=null;
   
    public MyDialogs(Context context){
        super(context);
    }
    public void setDisplay(){
     setContentView(R.layout.dialog);//設定對話方塊的布局
     b1=(Button)findViewById(R.id.clo);
     b1.setOnClickListener(this);
     setProperty();
        setTitle("自訂對話方塊");//設定對話方塊的標題
        show();//顯示對話方塊  
    }
     //要顯示這個對話方塊,只要建立該類對象.然後調用該函數即可.
    public void setProperty(){
         window=getWindow();//   得到對話方塊的視窗.
         WindowManager.LayoutParams wl = window.getAttributes();
         wl.x =0;//這兩句設定了對話方塊的位置.0為中間
         wl.y =180;
         wl.alpha=0.6f;//這句設定了對話方塊的透明度
         wl.gravity=Gravity.BOTTOM;        
         window.setAttributes(wl);
    }
  @Override
  public void onClick(View v) {
   dismiss();//取消   
  }
  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.