Android之AlertDialog Demo(2019011

作者: watayouxiang | 来源:发表于2019-01-15 13:17 被阅读9次

    Android之AlertDialog Demo(20190115)

    android.support.v7.app.AlertDialog 的使用

    源码下载:https://github.com/watayouxiang/AlertDialogDemo

    简单的AlertDialog

    public void showSimpleDialog(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("Title");
        builder.setMessage("Message");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", null);
        builder.setNeutralButton("中性", null);
        builder.show();
    }
    

    setView

    public void setView(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Title");
        builder.setMessage("Message");
        builder.setView(new EditText(this));
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", null);
        builder.show();
    }
    

    setSingleChoiceItems

    public void setSingleChoiceItems(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("单选列表");
        builder.setSingleChoiceItems(
                new String[]{"11111", "22222", "33333", "44444", "55555"},
                0,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                    }
                }
        );
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", null);
        builder.show();
    }
    

    setMultiChoiceItems

    public void setMultiChoiceItems(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("多选列表");
        builder.setMultiChoiceItems(
                new String[]{"11111", "22222", "33333", "44444", "55555"},
                new boolean[]{false, true, true, false, false},
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        Toast.makeText(mContext, "which " + which + ", isChecked " + isChecked, Toast.LENGTH_SHORT).show();
                    }
                }
        );
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", null);
        builder.show();
    }
    

    setItems

    public void setItems(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Title");
        builder.setItems(
                new String[]{"11111", "22222", "33333", "44444", "55555"},
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(mContext, "which " + which, Toast.LENGTH_SHORT).show();
                    }
                }
        );
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", null);
        builder.show();
    }
    

    自定义Dialog

    public void showMyDialog(View view) {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder.create();
        //取消外部点击隐藏
        alertDialog.setCanceledOnTouchOutside(false);
        //设置取消监听
        alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                Toast.makeText(mContext, "onCancel", Toast.LENGTH_SHORT).show();
            }
        });
        //设置自定义布局
        View rootView = getLayoutInflater().inflate(R.layout.view_my_dialog, null);
        rootView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
        alertDialog.setView(rootView);
        alertDialog.show();
        //设置弹窗参数(注意:弹窗样式修改必须在 dialog.show() 方法后调用,否则不生效)
        Window window = alertDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            //dialog即使没有设置四周的margin也有margin,可以设置给dialog设置个background来解决
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //宽度为屏幕宽度的80%
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = (int) (getWindowManager().getDefaultDisplay().getWidth() * 0.8f);
            window.setAttributes(params);
        }
    }
    

    view_my_dialog.xml

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF">
    
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="18dp"
            android:layout_marginRight="20dp"
            android:text="申请加入本课题学习社群"
            android:textColor="#2F373F"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <View
            android:id="@+id/v_titleDivider"
            android:layout_width="47dp"
            android:layout_height="5dp"
            android:layout_marginTop="10dp"
            android:background="#F44336"
            app:layout_constraintStart_toStartOf="@+id/tv_title"
            app:layout_constraintTop_toBottomOf="@+id/tv_title" />
    
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:text="姓名"
            android:textColor="#2F373F"
            android:textSize="15sp"
            app:layout_constraintBottom_toBottomOf="@+id/et_name"
            app:layout_constraintEnd_toStartOf="@+id/et_name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/et_name" />
          
    
        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="44dp"
            android:layout_marginTop="26dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/bg_cmty_dialog_et"
            android:hint="请输入姓名"
            android:inputType="text"
            android:paddingLeft="10dp"
            android:textColorHint="#6F6F6F"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tv_name"
            app:layout_constraintTop_toBottomOf="@+id/v_titleDivider" />
    
        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:text="电话"
            android:textColor="#2F373F"
            android:textSize="15sp"
            app:layout_constraintBottom_toBottomOf="@+id/et_phone"
            app:layout_constraintEnd_toStartOf="@+id/et_phone"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/et_phone" />
          
    
        <EditText
            android:id="@+id/et_phone"
            android:layout_width="0dp"
            android:layout_height="44dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/bg_cmty_dialog_et"
            android:hint="请输入手机号码"
            android:inputType="phone"
            android:paddingLeft="10dp"
            android:textColorHint="#6F6F6F"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tv_phone"
            app:layout_constraintTop_toBottomOf="@+id/et_name" />
    
        <TextView
            android:id="@+id/tv_remark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:text="备注"
            android:textColor="#2F373F"
            android:textSize="15sp"
            app:layout_constraintEnd_toStartOf="@+id/et_remark"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/et_remark" />
          
    
        <EditText
            android:id="@+id/et_remark"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/bg_cmty_dialog_et"
            android:gravity="start|top"
            android:hint="请输入备注"
            android:inputType="text"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:textColorHint="#6F6F6F"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tv_remark"
            app:layout_constraintTop_toBottomOf="@+id/et_phone" />
    
        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="8dp"
            android:padding="10dp"
            android:text="取消"
            android:textColor="#6F6F6F"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/et_remark" />
    
        <View
            android:layout_width="53dp"
            android:layout_height="53dp"
            android:background="@drawable/bg_cmty_dialog_round"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    
        <TextView
            android:id="@+id/tv_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="8dp"
            android:padding="10dp"
            android:text="申请加入"
            android:textColor="#2F373F"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    
    
    </android.support.constraint.ConstraintLayout>
    

    相关文章

      网友评论

        本文标题:Android之AlertDialog Demo(2019011

        本文链接:https://www.haomeiwen.com/subject/oztydqtx.html