ANDROID中自定义对话框AlertDialog使用示例
作者:潘侯爷 时间:2023-07-22 17:24:12
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式。AlertDialog中定义的一些对话框往往无法满足我们关于对话框的需求,这时我们就需要通过自定义对话框VIEW来实现需求,这里我自定义一个登陆的提示对话框,效果图显示如下:
Layout(alertdialog自定义登陆按钮)界面代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="自定义登陆"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:onClick="login"/>
</LinearLayout>
Layout(login_layout登陆窗口)界面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="请输入用户名"
android:id="@+id/et_username"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码"
android:id="@+id/et_password"/>
</LinearLayout>
java功能实现代码:
public class AlertDialogDemo extends AppCompatActivity {
private EditText et_username,et_password;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alertdialog);
}
public void login(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("登录");
//通过布局填充器获login_layout
View view = getLayoutInflater().inflate(R.layout.login_layout,null);
//获取两个文本编辑框(密码这里不做登陆实现,仅演示)
final EditText et_username = (EditText) view.findViewById(R.id.et_username);
final EditText et_password = (EditText) view.findViewById(R.id.et_password);
builder.setView(view);//设置login_layout为对话提示框
builder.setCancelable(false);//设置为不可取消
//设置正面按钮,并做事件处理
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String name = et_username.getText().toString().trim();
String pass = et_password.getText().toString().trim();
Toast.makeText(AlertDialogDemo.this,name + "正在登录....",Toast.LENGTH_SHORT).show();
}
});
//设置反面按钮,并做事件处理
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogDemo.this,"取消登录",Toast.LENGTH_SHORT).show();
}
});
builder.show();//显示Dialog对话框
}
}
标签:Android,对话框,AlertDialog
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
旧项目升级新版Unity2021导致Visual Studio无法使用的问题
2023-12-28 21:51:26
![](https://img.aspxhome.com/file/2023/6/126006_0s.png)
C#实现动态创建接口并调用的实例
2021-11-12 16:38:17
![](https://img.aspxhome.com/file/2023/4/92924_0s.jpg)
C#实现获取Excel中图片所在坐标位置
2021-10-14 23:36:18
![](https://img.aspxhome.com/file/2023/8/108768_0s.jpg)
Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使用
2022-09-08 18:56:36
![](https://img.aspxhome.com/file/2023/8/103928_0s.gif)
c语言颜色代码详解
2021-05-27 08:07:54
![](https://img.aspxhome.com/file/2023/0/125910_0s.png)
log4j配置失效日志中打印Debug信息问题
2022-10-12 10:13:12
![](https://img.aspxhome.com/file/2023/7/100667_0s.png)
Java生成动态版验证码的方法实例
2023-08-26 03:53:08
C#实现利用Windows API读写INI文件的方法
2023-03-16 01:23:06
Java中BufferedReader和BufferedWriter使用方式
2022-01-06 15:06:05
![](https://img.aspxhome.com/file/2023/9/75779_0s.png)
Java 继承与多态的深入理解
2023-10-05 04:25:41
spring boot org.junit.jupiter.api不存在的解决
2023-07-11 18:34:16
![](https://img.aspxhome.com/file/2023/6/83596_0s.png)
Unity调取移动端的麦克风进行录音并播放
2023-06-04 22:18:05
![](https://img.aspxhome.com/file/2023/3/116653_0s.png)
java 启动exe程序,传递参数和获取参数操作
2023-09-11 04:30:47
![](https://img.aspxhome.com/file/2023/9/71629_0s.jpg)
java求数组元素重复次数和java字符串比较大小示例
2023-08-04 22:20:02
用Linq从一个集合选取几列得到一个新的集合(可改列名)
2023-08-23 22:48:23
![](https://img.aspxhome.com/file/2023/1/68081_0s.png)
C#开发之Socket网络编程TCP/IP层次模型、端口及报文等探讨
2023-03-28 14:49:53
![](https://img.aspxhome.com/file/2023/4/129744_0s.jpg)
Android 去掉自定义dialog的白色边框的简单方法
2022-01-05 06:57:54
Java使用poi操作excel实例解析
2022-01-26 00:15:41
Git工具 conflict冲突问题解决方案
2023-07-27 10:27:30
![](https://img.aspxhome.com/file/2023/1/57911_0s.png)
RocketMQ源码解析topic创建机制详解
2023-03-16 01:06:23