Android自定义AlertDialog实现

本文主要介绍AlertDialog的基本知识,以及使用自定义AlertDialog实现用户登录UI的程序。本文内容主要份分为以下几个部分:

  • AlertDialog介绍
  • 自定义AlertDialog实现用户登录UI

AlertDialog介绍

Android对话框是提示用户作出决定或输入额外信息的小窗口。 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件。

Dialog是对话框的基类,但是Android官方手册并不建议直接使用Dialog对象来操作,而是使用Dialog的继承类AlertDialog、DataPickerDialog、TimePickerDialog,这里我们主要介绍AlertDialog这个类。

Android提供了多种AlertDialog,有最简单的AlertDialog,带列表的,带两个(3个)Button等,有时候这些都不能满足我们,这里就需要使用自定义的AlertDialog来实现一些功能;接下来,我们将用一个自定义的AlertDialog来实现弹出式用户登录U。I


自定义AlertDialog实现

首先,建立一个Layout作为弹出的自定义AlertDialog界面,在这个Layout中放置一个ImageView,和两个LinearLayout,在两个LinearLayout中均放置一个TextView和一个EditText,其中EditText作为编辑框用于输入用户名和用户密码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#00ff00"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textSize="22dp"/>
        <EditText
            android:id="@+id/city_text"
            android:layout_width="219dp"
            android:layout_height="wrap_content"
            android:hint="user name"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="22dp"/>
        <EditText
            android:id="@+id/nian_text"
            android:layout_width="217dp"
            android:layout_height="wrap_content"
            android:hint="password"/>
    </LinearLayout>
</LinearLayout>

这里ImageView的背景只是设置为一个颜色,效果如下


这里写图片描述

然后在activity_main的布局中放置一个Button控件,当该Button被点击之后弹出AlertDialog对话框:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_sign"
        android:text="Log in"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

逻辑非常明确,接下来就在MainActivity中做两个工作:1、设置Button控件的点击事件;2、点击事件中弹出AlertDialog并记录对话框中的内容;
先给出MainActivity的代码,如下:

public class MainActivity extends AppCompatActivity {

    private final static String TAG1 = "AlertDialog";
    private Button loginButton;
    private String userName;
    private String userPassword;
    private EditText userNameEdit;
    private EditText userPasswordEdit;
    private View alertDialogView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);

        alertDialogView = getLayoutInflater ().inflate (R.layout.alertdialog_layout, null, false);

        loginButton = findViewById (R.id.button_sign);
        loginButton.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder loginAlertDialog = new AlertDialog.Builder (MainActivity.this);
                loginAlertDialog.setView (alertDialogView);
                loginAlertDialog.setPositiveButton ("OK", new DialogInterface.OnClickListener () {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        userNameEdit = alertDialogView.findViewById (R.id.city_text);
                        userPasswordEdit = alertDialogView.findViewById (R.id.nian_text);
                        userName = userNameEdit.getText ().toString ();
                        userPassword = userPasswordEdit.getText ().toString ();
                    }
                });

                loginAlertDialog.show ();
            }
        });
        Log.i (TAG1, "onCreate: user name is " + userName +" userPassword is " + userPassword);
    }
}

首先,使用findViewById获取login Button控件,并设置Listener接口;

然后,使用getLayoutInflater获得LayoutInflater实例,然后调用该实例的inflate方法来获得我们自定义的View,inflate方法传入三个参数,第一个是我们自己定义布局的id,第二个是ViewGroup实例作为父布局,这里传入null,第三个是否使用父布局,传入false表示不使用。

接着就是最重要的地方,使用AlertDialog.Builder建立一个AlertDialog的实例,并用setView方法设置将要弹出的对话框的UI,这里就传入我们刚刚获得alertDialogView 这个对象;setPositiveButton 方法可以再对话框底部设置一个小按钮,这里我们将该按钮命名为“OK”;另外,这个按钮同样可以实现监听功能,我们在监听方法onClick中做了这样的操作:获取user name中填入的内容赋值给userName 变量,获取password中填入的内容赋值给userPassword 变量,之后便可以利用这两个变量来判断用户名以及用户密码是否正确,从而实现用户登录功能。(这里实际登录未实现)
此处有两点需要注意:

  1. 因为用户名和密码的EditText是在我们自定义的View上,因此在调用findViewById方法时必须是alertDialogView.findViewById,否则返回的将是null;
  2. 使用getText方法并不能直接获得String变量,需要调用toString方法来转换为String类型;

做完这些工作以后就可以实现自定义AlertDialog实现用户登录的UI界面了,实际程序运行的效果如下:


效果图

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您介绍一下Android仿IOS自定义AlertDialog提示框的实现方法。 首先,我们需要在Android项目中创建一个自定义布局文件,用于显示弹框的内容。可以使用LinearLayout或RelativeLayout等布局容器来组织弹框的内容,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="标题" android:textSize="18sp" /> <TextView android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="内容" android:textSize="14sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout> ``` 接下来,我们需要创建一个自定义AlertDialog类,用于显示弹框和处理按钮点击事件。在这个类中,我们需要实现onCreateDialog方法来加载自定义布局文件,并设置弹框的标题、内容和按钮监听器等。例如: ```java public class IOSAlertDialog extends DialogFragment { private String title; private String message; private DialogInterface.OnClickListener confirmListener; private DialogInterface.OnClickListener cancelListener; public IOSAlertDialog(String title, String message, DialogInterface.OnClickListener confirmListener, DialogInterface.OnClickListener cancelListener) { this.title = title; this.message = message; this.confirmListener = confirmListener; this.cancelListener = cancelListener; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = LayoutInflater.from(getActivity()); View view = inflater.inflate(R.layout.dialog_ios_alert, null); TextView titleView = view.findViewById(R.id.title); TextView messageView = view.findViewById(R.id.message); Button confirmButton = view.findViewById(R.id.confirm); Button cancelButton = view.findViewById(R.id.cancel); titleView.setText(title); messageView.setText(message); confirmButton.setOnClickListener(confirmListener); cancelButton.setOnClickListener(cancelListener); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view); return builder.create(); } } ``` 最后,在我们的Activity中,我们可以通过创建一个实例对象,并调用show方法来显示弹框。例如: ```java IOSAlertDialog dialog = new IOSAlertDialog( "提示", "确定要删除吗?", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 确定按钮点击事件 } }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 取消按钮点击事件 } }); dialog.show(getSupportFragmentManager(), "IOSAlertDialog"); ``` 这样,我们就可以实现一个Android仿IOS自定义AlertDialog提示框了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值