对于许久不用的东西,容易忘记。百度许久,也未能找到自己所要。 从今日起,有些东西就记载下来,不仅方便自己,也希望能帮到他人。

对话框很简单,就是new 一个对象,然后给他设置各种各样的属性,网络有很多Dialog的教程,我甚至还看到过仿IOS的Dialog,效果很棒。

不过我大android 真的要抄袭 IOS ,相对于封闭的IOS,android 有什么不能实现?

系统的Dialog模块比较简单,就是一个标题栏,内容以及几个按钮,内容部分可以用布局代替。这样真的能实现我的功能吗?

android 左右切换对话框 dialog_第1张图片

如图所示,我需要给每个Item设置一个地址,难道要弹出一个对话宽,输入数据,关闭对话框,再弹出对话框,如此反复吗?这样产品经理会杀了我的。

那我应该怎么处理?,给对话宽增加俩个按钮,左/右 移动?

android 左右切换对话框 dialog_第2张图片

左右箭头向不同的方向滑动,这样就不用弹出对话框-》输入数据-》关闭对话框-》弹出对话框…, 而是弹出对话框-》输入数据-》输入数据-》… -》关闭对话框。

废话不多说了,上代码。

1.自定义类,继承对话框

public class PickDialog extends Dialog  {    public PickDialog(@NonNull Context context) {        super(context);    }    public PickDialog(@NonNull Context context, int themeResId) {        super(context, themeResId);    }    protected PickDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {        super(context, cancelable, cancelListener);    }}

2.初始化view

 private void initView(Context context) {        View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);        //设置View        setContentView(view);        //findViewById        cancel = findViewById(R.id.cancel);        confirm = findViewById(R.id.confirm);        address = findViewById(R.id.address);        next = findViewById(R.id.next);        last = findViewById(R.id.last);        tv_address = findViewById(R.id.tv_address);        bg = findViewById(R.id.bg);        //设置Touch事件,触摸view有动态效果        cancel.setOnTouchListener(this);        confirm.setOnTouchListener(this);        next.setOnTouchListener(this);        last.setOnTouchListener(this);        //切换到上一个对话框,其实对话框只有一个,只不过点击改变数据,在加上动画效果        last.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.e(TAG, "onClick: 上一个");                //这个动画效果只是缩放,如果做成淡出,淡入效果就更加逼真了。                ScaleAnimation animation = new ScaleAnimation(                        1.0f, 0f, 1.0f, 1.0f,                        Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f                );                animation.setDuration(200);                bg.startAnimation(animation);                iPickDialog.onLast();            }        });        //切换到下一个对话框,其实对话框只有一个,只不过点击改变数据,在加上动画效果        next.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.e(TAG, "onClick: 下一个");                 //这个动画效果只是缩放,如果做成淡出,淡入效果就更加逼真了。                ScaleAnimation animation = new ScaleAnimation(                        1.0f, 0f, 1.0f, 1.0f,                        Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f                );                animation.setDuration(200);                bg.startAnimation(animation);                iPickDialog.onNext();            }        });        confirm.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //确认按钮                iPickDialog.onConfirm(getAddress());                dismiss();            }        });        cancel.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //取消按钮                Log.e(TAG, "onClick: cancel");                dismiss();            }        });        //放大动画        animation1 = new ScaleAnimation(                1.0f, 1.5f, 1.0f, 1.5f,                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f        );        animation1.setDuration(200);        animation1.setFillAfter(true);        //动画归位        animation2 = new ScaleAnimation(                1.0f, 1f, 1.0f, 1f,                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f        );        animation2.setDuration(200);        animation2.setFillAfter(true);        //Edittext监听回车按钮        address.setOnKeyListener(new View.OnKeyListener() {            @Override            public boolean onKey(View v, int keyCode, KeyEvent event) {                if (keyCode == KeyEvent.KEYCODE_ENTER) {                    if (event.getAction() == KeyEvent.ACTION_DOWN) {                        //按下事件                        ScaleAnimation animation = new ScaleAnimation(                                1.0f, 0f, 1.0f, 1.0f,                                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f                        );                        animation.setDuration(200);                        bg.startAnimation(animation);                        iPickDialog.onNext();                    }                }                return false;            }        });    }
R.layout.dialog_layout
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#55000000"    android:orientation="horizontal"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_centerInParent="true"        android:orientation="horizontal">        <LinearLayout            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="0.5"/>        <LinearLayout            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="9"            android:gravity="center">            <ImageView                android:id="@+id/last"                android:layout_width="64dp"                android:layout_height="wrap_content"                android:src="@drawable/left"/>            <LinearLayout                android:id="@+id/bg"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_weight="1"                android:background="#ffffff"                android:orientation="vertical"                >                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_marginTop="10dp"                    android:paddingLeft="10dp"                    android:text="地址绑定"                    android:textColor="#06d6fa"                    android:textSize="20dp"                    android:textStyle="bold"/>                <View                    android:layout_width="match_parent"                    android:layout_height="2dp"                    android:layout_marginTop="5dp"                    android:background="#06d6fa"/>                <TextView                    android:id="@+id/tv_address"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_marginLeft="20dp"                    android:layout_marginTop="20dp"                    android:text="地址:"                    android:textColor="#ff0000"                    android:textSize="18sp"/>                <led.com.hyco.yt_pickdemo.view.ClearEditText                    android:id="@+id/address"                    android:layout_width="match_parent"                    android:layout_height="45dp"                    android:layout_marginBottom="20dp"                    android:layout_marginLeft="20dp"                    android:layout_marginRight="20dp"                    android:layout_marginTop="5dp"                    android:background="@drawable/edit_bg"                    android:paddingLeft="2dp"                    android:paddingRight="2dp"                    android:singleLine="true"                    android:textColor="#000000"                    android:textSize="20sp"/>                <View                    android:layout_width="match_parent"                    android:layout_height="2dp"                    android:layout_marginTop="5dp"                    android:background="#06d6fa"/>                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="50dp"                    android:orientation="horizontal">                    <TextView                        android:id="@+id/cancel"                        android:layout_width="0dp"                        android:layout_height="match_parent"                        android:layout_weight="1"                        android:gravity="center"                        android:text="取消"                        android:textSize="18sp"/>                    <View                        android:layout_width="1dp"                        android:layout_height="match_parent"                        android:background="#06d6fa"/>                    <TextView                        android:id="@+id/confirm"                        android:layout_width="0dp"                        android:layout_height="match_parent"                        android:layout_weight="1"                        android:gravity="center"                        android:text="确定"                        android:textSize="18sp"/>                LinearLayout>            LinearLayout>            <ImageView                android:id="@+id/next"                android:layout_width="64dp"                android:layout_height="wrap_content"                android:src="@drawable/right"/>        LinearLayout>        <LinearLayout            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="0.5"/>    LinearLayout>RelativeLayout>使用方法很简单:
  pickDialog = new PickDialog(this, R.style.PickDialog);  //设置接口  pickDialog.setiPickDialog(this);  //设置数据  pickDialog.init(list.get(pos));  //显示对话框  pickDialog.show();

Demo下载地址:https://download.csdn.net/download/weixin_39923324/10590124

更多相关文章

  1. 基于 android 数据备份恢复的一种实现
  2. Android开发实践 数据存储 学习、应用、总结
  3. Android studio 通过以servlet搭建的服务器访问 PC端 mysql数据
  4. 自定义控件及效果
  5. android 连接远程数据库
  6. Android UI开发第十二篇――动画效果Animation(一)
  7. Android[中级教程]第八章 Json数据的处理
  8. Android开发中阴影效果的实现
  9. Android UI开发第十二篇——动画效果Animation(一)

随机推荐

  1. assets android 获取流 转为string
  2. Android之TextView
  3. Android(安卓)的用户层 uevent处理机制
  4. 演化理解 Android(安卓)异步加载图片
  5. Android(安卓)Design Demo 策略模式 Text
  6. android的窗口机制分析---------类图关系
  7. Android(安卓)AIDL进程间通信(IPC)
  8. Android(安卓)Activity和Intent机制学习
  9. Android内核的简单分析
  10. 如何在Android当中显示网络图片