1. 程式人生 > >Android 自定義介面的彈出框(可輸入資料)

Android 自定義介面的彈出框(可輸入資料)

    上午寫了一篇博文,介紹瞭如何定義從螢幕底部彈出PopupWindow,寫完之後,突然想起之前寫過自定義內容顯示的彈出框,就隨手寫了兩個例項,分享出來:


第一種實現方式:繼承Dialog

 1.1 線定義彈出框要顯示的內容:create_user_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/create_user_dialog_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_load_bg"
    android:minWidth="200dp"
    android:orientation="vertical"
    android:padding="10dp"
    android:paddingBottom="30dp"
    android:paddingTop="30dp">

    <EditText
        android:id="@+id/text_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_bg"
        android:hint="姓名"
        android:minHeight="45dp"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/text_mobile"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@drawable/edit_bg"
        android:hint="手機號"
        android:minHeight="45dp"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/text_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@drawable/edit_bg"
        android:gravity="top|left"
        android:hint="個性簽名"
        android:minHeight="145dp"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btn_save_pop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="儲存" />

</LinearLayout>
 1.2 定義要彈出的Dialog
public class CreateUserDialog extends Dialog {

    /**
     * 上下文物件 *
     */
    Activity context;

    private Button btn_save;

    public EditText text_name;

    public EditText text_mobile;

    public EditText text_info;


    private View.OnClickListener mClickListener;

    public CreateUserDialog(Activity context) {
        super(context);
        this.context = context;
    }

    public CreateUserDialog(Activity context, int theme, View.OnClickListener clickListener) {
        super(context, theme);
        this.context = context;
        this.mClickListener = clickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定佈局
        this.setContentView(R.layout.create_user_dialog);

        text_name = (EditText) findViewById(R.id.text_name);
        text_mobile = (EditText) findViewById(R.id.text_mobile);
        text_info = (EditText) findViewById(R.id.text_info);

        /*
         * 獲取聖誕框的視窗物件及引數物件以修改對話方塊的佈局設定, 可以直接呼叫getWindow(),表示獲得這個Activity的Window
         * 物件,這樣這可以以同樣的方式改變這個Activity的屬性.
         */
        Window dialogWindow = this.getWindow();

        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay(); // 獲取螢幕寬、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 獲取對話方塊當前的引數值
        // p.height = (int) (d.getHeight() * 0.6); // 高度設定為螢幕的0.6
        p.width = (int) (d.getWidth() * 0.8); // 寬度設定為螢幕的0.8
        dialogWindow.setAttributes(p);

        // 根據id在佈局中找到控制元件物件
        btn_save = (Button) findViewById(R.id.btn_save);

        // 為按鈕繫結點選事件監聽器
        btn_save.setOnClickListener(mClickListener);

        this.setCancelable(true);
    }
}
  1.3 呼叫彈出框:
    public void showEditDialog(View view) {
        createUserDialog = new CreateUserDialog(this,R.style.loading_dialog,onClickListener);
        createUserDialog.show();
    }
 private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.btn_save:

                    String name = createUserDialog.text_name.getText().toString().trim();
                    String mobile = createUserDialog.text_mobile.getText().toString().trim();
                    String info = createUserDialog.text_info.getText().toString().trim();

                    System.out.println(name+"——"+mobile+"——"+info);
                    break;
            }
        }
    };

第二種實現方式:繼承PopupWindow
  2.1 定義彈出框佈局檔案,和1.1定義的一致

  2.2 定義要彈出的PopupWindow

public class CreateUserPopWin extends PopupWindow {

    private Context mContext;

    private View view;

    private Button btn_save_pop;

    public EditText text_name;

    public EditText text_mobile;

    public EditText text_info;


    public CreateUserPopWin(Activity mContext, View.OnClickListener itemsOnClick) {

        this.mContext = mContext;

        this.view = LayoutInflater.from(mContext).inflate(R.layout.create_user_pop, null);

        text_name = (EditText) view.findViewById(R.id.text_name);
        text_mobile = (EditText) view.findViewById(R.id.text_mobile);
        text_info = (EditText) view.findViewById(R.id.text_info);

        btn_save_pop =  (Button) view.findViewById(R.id.btn_save_pop);

        // 設定按鈕監聽
        btn_save_pop.setOnClickListener(itemsOnClick);

        // 設定外部可點選
        this.setOutsideTouchable(true);


        /* 設定彈出視窗特徵 */
        // 設定檢視
        this.setContentView(this.view);

        // 設定彈出窗體的寬和高
          /*
         * 獲取聖誕框的視窗物件及引數物件以修改對話方塊的佈局設定, 可以直接呼叫getWindow(),表示獲得這個Activity的Window
         * 物件,這樣這可以以同樣的方式改變這個Activity的屬性.
         */
        Window dialogWindow = mContext.getWindow();

        WindowManager m = mContext.getWindowManager();
        Display d = m.getDefaultDisplay(); // 獲取螢幕寬、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 獲取對話方塊當前的引數值

        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth((int) (d.getWidth() * 0.8));

        // 設定彈出窗體可點選
        this.setFocusable(true);

    }
}
  2.3 呼叫該彈框元件
public void showEditPopWin(View view) {

        createUserPopWin = new CreateUserPopWin(this,onClickListener);

        createUserPopWin.showAtLocation(findViewById(R.id.main_view), Gravity.CENTER, 0, 0);
    }
 private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.btn_save_pop:

                    String name1 = createUserPopWin.text_name.getText().toString().trim();
                    String mobile1 = createUserPopWin.text_mobile.getText().toString().trim();
                    String info1 = createUserPopWin.text_info.getText().toString().trim();

                    System.out.println(name1+"——"+mobile1+"——"+info1);

                    createUserPopWin.dismiss();
                    break;
            }
        }
    };
原始碼下載地址(免費):http://download.csdn.net/detail/zuiwuyuan/9075977