几种自定义dialog(包括个别新的思路)
来源:互联网 发布:怎么关掉电脑445端口 编辑:程序博客网 时间:2024/06/06 03:41
文件包括一个mainactivity 一个activity_main布局文件 然后三个自定义dialog及其布局 下面先上传布局文件 然后是activity 效果图在最下面
三个dialog都是自定义的 第一种是接口传值的弹框 当然也可以把接口去了变成传统的自定义弹框 第二种是传统的弹框 第三种是类似相机 相册选择弹框
dialog 三种dialog 达到的效果都是响应activity中的事件 并做出相应的处理
首先上布局文件
activity_main
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.dialog1.MainActivity"><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击我1"/> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击我2"/> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击我3"/></LinearLayout></LinearLayout>dialog1
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:orientation="vertical" android:padding="20.0dip" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/corners_bg" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="40.0dip" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:gravity="center" android:text="提示" android:visibility="visible" /> <RelativeLayout android:layout_width="match_parent" android:padding="1dp" android:layout_height="wrap_content"> <View android:layout_width="wrap_content" android:layout_height="1.0px" android:background="#ffd0d0d0" /> </RelativeLayout> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="8dp" android:layout_gravity="center_horizontal" android:weightSum="1"> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minHeight="50dip" android:paddingBottom="15.0dip" android:paddingTop="15.0dip" android:text="退货原因:" /> <EditText android:id="@+id/et_yunyin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入原因" android:textColorHint="@color/grey" android:textSize="14sp" android:textColor="@color/black" android:layout_weight="0.63" /> </LinearLayout> <LinearLayout android:id="@+id/contentt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="8dp" android:layout_gravity="center_horizontal" android:weightSum="1"> <TextView android:id="@+id/messagee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minHeight="50dip" android:paddingBottom="15.0dip" android:paddingTop="15.0dip" android:text="退货金额:" /> <EditText android:id="@+id/et_jine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入金额" android:textColorHint="@color/grey" android:textSize="14sp" android:inputType="number" android:textColor="@color/black" android:layout_weight="0.63" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="0dp" android:padding="8dp"> <Button android:id="@+id/positiveButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/sharp_extre_blue" android:text="确定"/> <Button android:id="@+id/negativeButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@drawable/sharp_extre_blue" android:layout_marginLeft="8dp" android:text="取消"/> </LinearLayout> </LinearLayout></FrameLayout>
dialog2
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:orientation="vertical" android:padding="20.0dip" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/corners_bg" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="40.0dip" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:gravity="center" android:text="提示" android:visibility="visible" /> <RelativeLayout android:layout_width="match_parent" android:padding="1dp" android:layout_height="wrap_content"> <View android:layout_width="wrap_content" android:layout_height="1.0px" android:background="#ffd0d0d0" /> </RelativeLayout> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <TextView android:id="@+id/message" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:minHeight="50dip" android:paddingBottom="15.0dip" android:paddingTop="15.0dip" android:text="dsfsdafs" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="0dp" android:padding="5dp"> <Button android:id="@+id/positiveButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/sharp_extre_blue" android:text="取消"/> <Button android:id="@+id/negativeButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@drawable/sharp_extre_blue" android:layout_marginLeft="5dp" android:text="确定"/> </LinearLayout> </LinearLayout></FrameLayout>dialog3
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height ="match_parent" android:layout_width="300dp" android:orientation="vertical" android:layout_gravity="center" android:background="@android:color/white"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:padding="10dp" android:textSize="12sp" android:textColor="@color/green" android:text="选择支付方式"/> <View android:layout_width="match_parent" android:layout_height="1dp"></View> <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/xjpay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@null" android:drawableLeft="@mipmap/cash" android:layout_marginLeft="5dp" android:drawableRight="@drawable/xjpay_btn" android:padding="10dp" android:drawablePadding="10dp" android:text="现金支付" android:checked="true" /> <View android:layout_width="match_parent" android:layout_height="1dp"></View> <RadioButton android:id="@+id/weixin_pay" android:layout_marginLeft="5dp" android:layout_height="wrap_content" android:layout_width="fill_parent" android:drawableLeft="@mipmap/wechat" android:drawableRight="@drawable/xjpay_btn" android:padding="10dp" android:button="@null" android:text="微信支付" android:drawablePadding="10dp" /> <View android:layout_width="match_parent" android:layout_height="1dp"></View> <RadioButton android:id="@+id/alipay_pay" android:layout_marginLeft="5dp" android:layout_height="wrap_content" android:layout_width="fill_parent" android:drawableLeft="@mipmap/alipay" android:drawableRight="@drawable/xjpay_btn" android:padding="10dp" android:button="@null" android:text="支付宝支付" android:drawablePadding="10dp" android:visibility="gone"/> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dp" android:padding="10dp"> <Button android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/sharp_blue_btn" android:text="取消"/> <Button android:id="@+id/btn_sures" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:background="@drawable/sharp_blue_btn" android:layout_marginLeft="10dp" android:text="确定"/> </LinearLayout> </LinearLayout></LinearLayout>
mainactivity 很简单都能明白
package com.example.administrator.dialog1;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener { Handler myHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: Toast.makeText(MainActivity.this, "所以你是打算就确定现金支付了是吗,真的确定吗", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MainActivity.this, "所以你是打算就确定微信支付了是吗,真的确定吗", Toast.LENGTH_SHORT).show(); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1= (Button) findViewById(R.id.btn1); Button btn2= (Button) findViewById(R.id.btn2); Button btn3= (Button) findViewById(R.id.btn3); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn1: InputDialog1 inputDialog1 =new InputDialog1(MainActivity.this, new InputDialog1.OnEditInputFinishedListener() { @Override public void editInputFinished(String password, String passwordyin) { Toast.makeText(MainActivity.this,password+passwordyin, Toast.LENGTH_SHORT).show(); } }); inputDialog1.setView(new EditText(MainActivity.this)); //若对话框无法弹出输入法,加上这句话 inputDialog1.show(); break; case R.id.btn2: InputDialog2.Builder inputDialog2= new InputDialog2.Builder(MainActivity.this); inputDialog2.setTitle("提示"); inputDialog2.setMessage("是否确认?"); inputDialog2.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "所以是你要确定了吗", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); inputDialog2.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); inputDialog2.create().show(); break; case R.id.btn3: if(myHandler != null){ InputDialog3 way = new InputDialog3(MainActivity.this, myHandler); way.show(); } break; } }}
第一种dialog的activity代码
package com.example.administrator.dialog1;import android.app.AlertDialog;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class InputDialog1 extends AlertDialog implements OnClickListener{ private EditText etshuliang,etyuanyin; //编辑框 private Button btnConfrim, btnCancel; //确定取消按钮 private OnEditInputFinishedListener mListener; //接口 public interface OnEditInputFinishedListener{ void editInputFinished(String password, String passwordyin); } public InputDialog1(Context context, OnEditInputFinishedListener mListener) { super(context); this.mListener = mListener; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog1); //控件 etshuliang = (EditText)findViewById(R.id.et_jine); etyuanyin = (EditText)findViewById(R.id.et_yunyin);// if(buheshu==0){// etshuliang.setText(null);// etyuanyin.setText("");//// }else {// if(beizhu.equals(null)){// etshuliang.setText(buheshu+"");// etyuanyin.setText("");// }else {// etshuliang.setText(buheshu+"");// etyuanyin.setText(beizhu);// }////// } btnCancel = (Button) findViewById(R.id.negativeButton); btnConfrim = (Button) findViewById(R.id.positiveButton); btnConfrim.setOnClickListener(this); btnCancel.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.positiveButton) { //确定 if (mListener != null) { String password = etshuliang.getText().toString(); String passwordyin = etyuanyin.getText().toString(); if(password.equals("")){ password = "0"; } mListener.editInputFinished(password,passwordyin); } dismiss(); }else { //取消 dismiss(); } }}
第二种dialog 的activity代码
package com.example.administrator.dialog1;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;public class InputDialog2 extends Dialog { public InputDialog2(Context context) { super(context); } public InputDialog2(Context context, int theme) { super(context, theme); } public static class Builder { private Context context; private String title; private String message; private String positiveButtonText; private String negativeButtonText; private View contentView; private OnClickListener positiveButtonClickListener; private OnClickListener negativeButtonClickListener; public Builder(Context context) { this.context = context; } public Builder setMessage(String message) { this.message = message; return this; } public Builder setMessage(int message) { this.message = (String) context.getText(message); return this; } /** * Set the Dialog title from resource * * @param title * @return */ public Builder setTitle(int title) { this.title = (String) context.getText(title); 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; } /** * Set the positive button resource and it's listener * * @param positiveButtonText * @return */ public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) { this.positiveButtonText = (String) context .getText(positiveButtonText); this.positiveButtonClickListener = listener; return this; } public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) { this.positiveButtonText = positiveButtonText; this.positiveButtonClickListener = listener; return this; } public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) { this.negativeButtonText = (String) context .getText(negativeButtonText); this.negativeButtonClickListener = listener; return this; } public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) { this.negativeButtonText = negativeButtonText; this.negativeButtonClickListener = listener; return this; } public InputDialog2 create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Theme final InputDialog2 dialog = new InputDialog2(context, R.style.Dialog); View layout = inflater.inflate(R.layout.dialog2, null); dialog.addContentView(layout, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); // set the dialog title ((TextView) layout.findViewById(R.id.title)).setText(title); // set the confirm button if (positiveButtonText != null) { ((Button) layout.findViewById(R.id.positiveButton)) .setText(positiveButtonText); if (positiveButtonClickListener != null) { ((Button) layout.findViewById(R.id.positiveButton)) .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.positiveButton).setVisibility( View.GONE); } // set the cancel button if (negativeButtonText != null) { ((Button) layout.findViewById(R.id.negativeButton)) .setText(negativeButtonText); if (negativeButtonClickListener != null) { ((Button) layout.findViewById(R.id.negativeButton)) .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.negativeButton).setVisibility( View.GONE); } // set the content message if (message != null) { ((TextView) layout.findViewById(R.id.message)).setText(message); } else if (contentView != null) { // if no message set // add the contentView to the dialog body ((LinearLayout) layout.findViewById(R.id.content)) .removeAllViews(); ((LinearLayout) layout.findViewById(R.id.content)) .addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); } dialog.setContentView(layout); return dialog; } }}
第三种dialog的activity的代码
package com.example.administrator.dialog1;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.annotation.IdRes;import android.view.LayoutInflater;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.RadioGroup;/** * Created by Administrator on 2017/4/27 0027. */public class InputDialog3 extends Dialog { private String msg; private Handler handler = null; private RadioGroup radio_group; private Button btn_cancel; private Button btn_sures; private int str = 1; private View view; private Context context; private int position; public InputDialog3(Context context) { super(context); // TODO Auto-generated constructor stub this.context = context; } public InputDialog3(Context context, Handler handler) { super(context); // TODO Auto-generated constructor stub this.handler = handler; this.context = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); view = LayoutInflater.from(context).inflate(R.layout.dialog3, null); btn_cancel = (Button)view. findViewById(R.id.btn_cancel); btn_sures= (Button) view.findViewById(R.id.btn_sures); radio_group = (RadioGroup)view.findViewById(R.id.radio_group); btn_sures.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (handler!= null) { Message msg = new Message(); msg.what = str; msg.obj = position+""; handler.sendMessage(msg); } dismiss(); } }); btn_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub dismiss(); } }); radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { if(checkedId == R.id.xjpay){ str = 1; } if(checkedId == R.id.weixin_pay){ str = 2; } } }); setContentView(view); }}
其他的东西 像状态选择器就不说了 相信大家都明白 如果有其他的方法大家可以讨论,错误大家也可以指出
阅读全文
1 0
- 几种自定义dialog(包括个别新的思路)
- 自定义Dialog的几种实现方式
- android 几种自定义 Dialog
- 自定义dialog思路
- Dialog详解(包括进度条、PopupWindow、自定义view、自定义样式的对话框)
- MFC界面--利用CHtmlView和HTML制作新风格的界面(包括CView和Dialog)
- Android--Dialog,Dialog 的几种使用方法
- Dialog的几种方式
- Dialog的几种用法
- 几种常见的Dialog
- 自定义dialog无边框,包括黑边白边框
- Android学习笔记(17)---Dialog的几种使用方法
- 设置窗口大小(包括设置Dialog的窗口)
- android dialog常见的几种用法
- Android 对话框(Dialog)的几种用法。
- Dialog的几种形式 Android开发
- Android dialog的几种形式
- Android 对话框(Dialog)的几种用法。
- git基础操作
- 如何解决dxBarManager上面编辑框录入内容不能立刻生效的问题(光标移开,而不是tab,会导致内容revert)
- 实战mp4文件解析——对着文件分析,可以一看
- 打印1到最大的n位数:大数问题,全排列实现
- Kerberos:cannot get master principle
- 几种自定义dialog(包括个别新的思路)
- npm安装stylus和stylus-loader
- npm link
- (大数据)MapReduce开发,一个没有隐私的世界!
- 阿里巴巴Java开发手册
- LeetCode
- sql server 查询数据库表结构
- Mp4 文件格式——翻译的spec,还不错
- 软件测试工程师职业介绍和规划