Android實(shí)現(xiàn)可輸入數(shù)據(jù)的彈出框
之前一篇文章,介紹了如何定義從屏幕底部彈出PopupWindow即《Android Animation實(shí)戰(zhàn)之屏幕底部彈出PopupWindow》,寫完之后,突然想起之前寫過自定義內(nèi)容顯示的彈出框,就隨手寫了兩個(gè)實(shí)例,分享出來:
第一種實(shí)現(xiàn)方式:繼承Dialog
1.1 線定義彈出框要顯示的內(nèi)容: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="手機(jī)號(hào)" 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="個(gè)性簽名" 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 { /** * 上下文對(duì)象 * */ 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); /* * 獲取圣誕框的窗口對(duì)象及參數(shù)對(duì)象以修改對(duì)話框的布局設(shè)置, 可以直接調(diào)用getWindow(),表示獲得這個(gè)Activity的Window * 對(duì)象,這樣這可以以同樣的方式改變這個(gè)Activity的屬性. */ Window dialogWindow = this.getWindow(); WindowManager m = context.getWindowManager(); Display d = m.getDefaultDisplay(); // 獲取屏幕寬、高用 WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 獲取對(duì)話框當(dāng)前的參數(shù)值 // p.height = (int) (d.getHeight() * 0.6); // 高度設(shè)置為屏幕的0.6 p.width = (int) (d.getWidth() * 0.8); // 寬度設(shè)置為屏幕的0.8 dialogWindow.setAttributes(p); // 根據(jù)id在布局中找到控件對(duì)象 btn_save = (Button) findViewById(R.id.btn_save); // 為按鈕綁定點(diǎn)擊事件監(jiān)聽器 btn_save.setOnClickListener(mClickListener); this.setCancelable(true); } }
1.3 調(diào)用彈出框
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; } } };
第二種實(shí)現(xiàn)方式:繼承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); // 設(shè)置按鈕監(jiān)聽 btn_save_pop.setOnClickListener(itemsOnClick); // 設(shè)置外部可點(diǎn)擊 this.setOutsideTouchable(true); /* 設(shè)置彈出窗口特征 */ // 設(shè)置視圖 this.setContentView(this.view); // 設(shè)置彈出窗體的寬和高 /* * 獲取圣誕框的窗口對(duì)象及參數(shù)對(duì)象以修改對(duì)話框的布局設(shè)置, 可以直接調(diào)用getWindow(),表示獲得這個(gè)Activity的Window * 對(duì)象,這樣這可以以同樣的方式改變這個(gè)Activity的屬性. */ Window dialogWindow = mContext.getWindow(); WindowManager m = mContext.getWindowManager(); Display d = m.getDefaultDisplay(); // 獲取屏幕寬、高用 WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 獲取對(duì)話框當(dāng)前的參數(shù)值 this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT); this.setWidth((int) (d.getWidth() * 0.8)); // 設(shè)置彈出窗體可點(diǎn)擊 this.setFocusable(true); } }
2.3 調(diào)用該彈框組件
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; } } };
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)Android有所幫助。
- Android 多種簡單的彈出框樣式設(shè)置代碼
- Android使用Dialog風(fēng)格彈出框的Activity
- react-native 封裝選擇彈出框示例(試用ios&android)
- Android中自定義PopupWindow實(shí)現(xiàn)彈出框并帶有動(dòng)畫效果
- Android 仿微信朋友圈點(diǎn)贊和評(píng)論彈出框功能
- android自定義彈出框樣式的實(shí)現(xiàn)方法
- 高仿IOS的Android彈出框
- Android仿微信進(jìn)度彈出框的實(shí)現(xiàn)方法
- Android編程實(shí)現(xiàn)仿QQ發(fā)表說說,上傳照片及彈出框效果【附demo源碼下載】
- Android自定義彈出框的方法
相關(guān)文章
Android動(dòng)畫效果之自定義ViewGroup添加布局動(dòng)畫(五)
這篇文章主要介紹了Android動(dòng)畫效果之自定義ViewGroup添加布局動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式
本篇文章主要介紹了Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09Android ListView物流獲取追蹤功能實(shí)現(xiàn)
這篇文章主要介紹了Android ListView物流獲取追蹤功能實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-03-03詳解Android應(yīng)用中DialogFragment的基本用法
Android App中建議使用DialogFragment作為對(duì)話框的容器,DialogFragment類提供了創(chuàng)建對(duì)話框并管理其外觀需要的所有控件,本文主要內(nèi)容便為詳解Android應(yīng)用中DialogFragment的基本用法,而不再需要調(diào)用Dialog的方法需要的朋友可以參考下2016-05-05Flutter構(gòu)建自定義Widgets的全過程記錄
在Flutter實(shí)際開發(fā)中,大家可能會(huì)遇到flutter框架中提供的widget達(dá)不到我們想要的效果,這時(shí)就需要我們?nèi)プ远xwidget,下面這篇文章主要給大家介紹了關(guān)于Flutter構(gòu)建自定義Widgets的相關(guān)資料,需要的朋友可以參考下2022-01-01Android用戶界面開發(fā)之:TextView的使用實(shí)例
Android用戶界面開發(fā)之:TextView的使用實(shí)例,需要的朋友可以參考一下2013-05-05使用genymotion訪問本地上Tomcat上數(shù)據(jù)的方法
下面小編就為大家?guī)硪黄褂胓enymotion訪問本地上Tomcat上數(shù)據(jù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03