欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android简单对话框及编辑对话框Dialog

程序员文章站 2022-06-08 18:57:52
...

一、简单对话框

以退出登录为例:
android简单对话框及编辑对话框Dialog

 AlertDialog.Builder dialog=new AlertDialog.Builder(mContext);
                dialog.setIcon(R.mipmap.welcome_logo);
                dialog.setTitle("退出登录");
                dialog.setMessage(user_id+"\n你确定要退出登录吗?");
                dialog.setCancelable(false);
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent=new Intent(mContext, LoginActivity.class);
                        getActivity().startActivity(intent);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(mContext,"请继续体验",Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.show();
  }

一、可编辑对话框:

android简单对话框及编辑对话框Dialog
modify_course_dialog.xml

<?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">
    <EditText
        android:id="@+id/course_name"
        android:layout_width="wrap_content"
        android:layout_alignLeft="@+id/course_remark"
        android:layout_height="wrap_content"
        android:hint="课程名"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:ems="10"
        />
    <!--获取焦点-->
    <requestFocus />
    <EditText
        android:id="@+id/course_remark"
        android:hint="备注"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/course_name"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:ems="10">
    </EditText>
</RelativeLayout>

.java:

 holder.v_modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                        builder.setIcon(R.mipmap.welcome_logo);
                        builder.setTitle("课程修改");
                        final View view = View.inflate(mContext, R.layout.modify_course_dialog, null);
                        EditText course_name = view.findViewById(R.id.course_name);
                        EditText course_remark = view.findViewById(R.id.course_remark);
                        builder.setView(view);
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String CourseName = course_name.getText().toString();
                                String CourseRemark = course_remark.getText().toString();
                                 Toast.makeText(mContext,"CourseName ="+CourseName +"-----CourseRemark ="+CourseRemark ,Toast.LENGTH_SHORT).show();
                                dialog.cancel();
                            }

                        });

                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }

                        });
                        builder.show();
                    }
                }

            }
        });
相关标签: 笔记 android