Android开发之自定义对话框(2)—加入EditText
来源:互联网 发布:好的淘宝零食点知乎 编辑:程序博客网 时间:2024/05/23 22:59
由于项目需要,在弹出的对话框中要有编辑框,先看效果图:
1,先看布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="240dp" android:layout_height="wrap_content" android:background="@drawable/dialog_bkg_2" android:orientation="vertical"> <TextView android:id="@+id/dialog_title" android:layout_width="match_parent" android:layout_height="40dp" android:background="@drawable/dialog_title_bkg" android:gravity="center" android:paddingBottom="4dp" android:paddingTop="4dp" android:textColor="@color/white" android:text="@string/add_friend" android:textSize="20sp" /> <EditText android:id="@+id/edt_dialog_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:padding="4dp" android:hint="请输入新好友用户名" android:background="@drawable/dialog_bkg_edit" android:textColor="@drawable/radio_btn_text" android:textSize="18sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="35dp" android:layout_marginBottom="16dp" android:orientation="horizontal"> <Button android:id="@+id/btn_dialog_ok" style="@style/btn_style" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_weight="1" android:text="@string/button_add" android:background="@drawable/button_selector" android:textColor="@color/white" android:textAllCaps="false" android:textSize="18sp" /> <Button android:id="@+id/btn_dialog_cancel" style="@style/btn_style" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_weight="1" android:text="@string/button_cancel" android:background="@drawable/button_selector" android:textColor="@color/white" android:textAllCaps="false" android:textSize="18sp" /> </LinearLayout></LinearLayout>
2,自定义dialog:
/** * Created by gyq on 2017/5/5 08:39 */public class CustomDialog extends Dialog { public CustomDialog(Context context) { super(context); } public CustomDialog(Context context, int theme) { super(context, theme); } public static class Builder { private Context context; private String title; private String message; private float textSize; private String positiveButtonText; private String negativeButtonText; private View contentView; private EditText firendName; private DialogInterface.OnClickListener positiveButtonClickListener; private DialogInterface.OnClickListener negativeButtonClickListener; public Builder(Context context) { this.context = context; } /** * Set the Dialog title from resource * * @param title * @return */ public Builder setTitle(int title) { this.title = (String) context.getText(title); return this; } private Builder setMessage(String msg) { this.message = msg; return this; } public Builder setMessage(String message, float textSize) { this.message = message; this.textSize = textSize; return this; } public Builder setMessage(int message) { this.message = (String) context.getText(message); return this; } /** * Set the Dialog title from String * * @param title * @return */ public Builder setTitle(String title) { this.title = title; return this; } public Builder setContentView(View v) { this.contentView = v; return this; } public String getFriendName() { return firendName.getText().toString().trim(); } /** * Set the positive button resource and it's listener * * @param positiveButtonText * @return */ public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = (String) context .getText(positiveButtonText); this.positiveButtonClickListener = listener; return this; } public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = positiveButtonText; this.positiveButtonClickListener = listener; return this; } public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = (String) context .getText(negativeButtonText); this.negativeButtonClickListener = listener; return this; } public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = negativeButtonText; this.negativeButtonClickListener = listener; return this; } public CustomDialog create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Theme final CustomDialog dialog = new CustomDialog(context,R.style.Dialog); dialog.setCanceledOnTouchOutside(false); dialog.setCancelable(false); View layout = inflater.inflate(R.layout.dialog_clear_normal, null); dialog.addContentView(layout, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); // set the dialog title ((TextView) layout.findViewById(R.id.dialog_title)).setText(title); firendName = (EditText)layout.findViewById(R.id.edt_dialog_message); // set the confirm button if (positiveButtonText != null) { ((Button) layout.findViewById(R.id.btn_dialog_ok)) .setText(positiveButtonText); if (positiveButtonClickListener != null) { ((Button) layout.findViewById(R.id.btn_dialog_ok)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.btn_dialog_ok).setVisibility( View.GONE); } // set the cancel button if (negativeButtonText != null) { ((Button) layout.findViewById(R.id.btn_dialog_cancel)) .setText(negativeButtonText); if (negativeButtonClickListener != null) { ((Button) layout.findViewById(R.id.btn_dialog_cancel)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.btn_dialog_cancel).setVisibility( View.GONE); } // set the content message if (message != null) { TextView msgView = (TextView) layout.findViewById(R.id.message); msgView.setText(message); if (textSize != 0) { msgView.setTextSize(textSize); } } else if (contentView != null) { } dialog.setContentView(layout); return dialog; } }}
3,如何使用:
private void addFirend() { final CustomDialog.Builder builder = new CustomDialog.Builder(getActivity()); builder.setTitle("添加好友"); builder.setPositiveButton("添加", new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, int which) { new Thread() { @Override public void run() { String friendName = builder.getFriendName(); try{ EMClient.getInstance().contactManager().addContact(friendName,"我是你的朋友"); }catch (Exception e) { e.printStackTrace(); } } }.start(); dialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); }
0 0
- Android开发之自定义对话框(2)—加入EditText
- android开发之自定义对话框
- Android开发之自定义对话框
- android开发步步为营之2:开发自定义进度条对话框
- android开发——自定义EditText
- android EditText自定义开发实例
- Android开发之自定义可清空内容的EditText
- Android开发之EditText
- Android开发之EditText
- Android之自定义对话框
- 关于自定义对话框加入自定义ListView和getView多次加载导致EditText获取文本的问题
- Android开发——AlertDialog自定义对话框
- Android开发笔记(六十六)自定义对话框
- android开发步步为营之25:开发自定义进度条对话框
- android自定义控件之edittext
- Android控件之自定义EditText
- android自定义View之自定义EditText(添加删除功能)
- Android自定义View之自定义EditText(添加删除功能)
- 可视化编程
- 设置全局Context
- TCP三次握手和四次挥手详解以及原因
- git log 的提交历史排序选项
- 安卓商品展示案例
- Android开发之自定义对话框(2)—加入EditText
- Java方向如何准备BAT技术面试答案(汇总版)
- spring的工作原理以及AOP,IOC,DI等概念
- C#生成验证码
- 网络流习题
- ffmpeg中文文档
- Angular开发(七)-关于组件的生命周期
- Oracle AWR Reports: Understanding I/O Statistics
- 计算机专业方面的期刊介绍