AlertDialog6种使用方法
AlertDialog
1.AlertDialog的6种创建模式
1.1setMessage
1)Java代码
-
//1.创建构造器
-
AlertDialog.Builder builder=new AlertDialog.Builder(this);
-
//2.设置参数
-
builder.setTitle("弹窗提示")
-
.setIcon(R.mipmap.boy)
-
.setMessage("选择你的性别?")
-
.setPositiveButton("男",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
Toast.makeText(DialogTest.this, "选中男", Toast.LENGTH_SHORT).show();
-
}
-
})
-
.setNegativeButton("女",new DialogInterface.OnClickListener(){
-
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
Toast.makeText(DialogTest.this, "选中女", Toast.LENGTH_SHORT).show();
-
}
-
})
-
.setNeutralButton("未知",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
Toast.makeText(DialogTest.this, "选中未知", Toast.LENGTH_SHORT).show();
-
}
-
-
});
1.2setItem,设置列表项
1)Java代码
-
String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
-
//1.创建构造器
-
AlertDialog.Builder builder=new AlertDialog.Builder(this);
-
//2.设置参数
-
builder.setTitle("请选择城市地区")
-
.setIcon(R.mipmap.city)
-
.setItems(citys, new DialogInterface.OnClickListener() {
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
Toast.makeText(DialogTest.this, "" citys[i], Toast.LENGTH_SHORT).show();
-
}
-
})
-
.setPositiveButton("确认",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNegativeButton("取消",new DialogInterface.OnClickListener(){
-
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNeutralButton("忽略",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
-
});
-
//3.创建对象,显示对象
-
builder.create().show();
1.3setSingleChoiceItems,设置对话框内容为单选列表项
- 可以传递数组或者是集合
-
String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
-
//1.创建构造器
-
AlertDialog.Builder builder=new AlertDialog.Builder(this);
-
//2.设置参数
-
builder.setTitle("请选择城市地区")
-
.setIcon(R.mipmap.city)
-
.setSingleChoiceItems(citys, 0,new DialogInterface.OnClickListener() {
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
Toast.makeText(DialogTest.this, "" citys[i], Toast.LENGTH_SHORT).show();
-
}
-
})
-
.setPositiveButton("确认",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNegativeButton("取消",new DialogInterface.OnClickListener(){
-
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNeutralButton("忽略",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
-
});
-
//3.创建对象,显示对象
-
builder.create().show();
1.4setMultiChoiceItems设置多选
-
// 设置多选
-
String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
-
//1.创建构造器
-
AlertDialog.Builder builder=new AlertDialog.Builder(this);
-
//2.设置参数
-
builder.setTitle("请选择城市地区")
-
.setIcon(R.mipmap.city)
-
.setMultiChoiceItems(citys, new boolean[]{false, false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
-
-
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
-
Toast.makeText(DialogTest.this, "" citys[i] b, Toast.LENGTH_SHORT).show();
-
}
-
})
-
.setPositiveButton("确认",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNegativeButton("取消",new DialogInterface.OnClickListener(){
-
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNeutralButton("忽略",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
-
});
-
//3.创建对象,显示对象
-
builder.create().show();
1.5setAdapter设置自定义的样式
- 需要传入一个自定义的布局
1)子布局样式
- 文本框
- 输入框
- 多选框
-
<?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">
-
-
<LinearLayout
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:orientation="horizontal"
-
>
-
<TextView
-
android:id="@ id/s1"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text=""
-
/>
-
<EditText
-
android:layout_width="0dp"
-
android:layout_height="wrap_content"
-
android:id="@ id/s2"
-
android:layout_weight="1"
-
/>
-
<CheckBox
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:id="@ id/s3"
-
/>
-
</LinearLayout>
-
</LinearLayout>
2)Java代码
-
// 设置多选
-
String[] citys={"济南","青岛","潍坊","日照","临沂","枣庄"};
-
//1.创建构造器
-
AlertDialog.Builder builder=new AlertDialog.Builder(this);
-
//2.设置参数
-
builder.setTitle("请选择城市地区")
-
.setIcon(R.mipmap.city)
-
.setAdapter(new ArrayAdapter<String>(DialogTest.this, R.layout.myselect,R.id.s1,citys), new DialogInterface.OnClickListener() {
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
}
-
)
-
.setPositiveButton("确认",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNegativeButton("取消",new DialogInterface.OnClickListener(){
-
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNeutralButton("忽略",new DialogInterface.OnClickListener(){
-
-
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
-
});
-
//3.创建对象,显示对象
-
builder.create().show();
1.6setView,指定对话框为自定义的View
1)布局代码
-
<?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">
-
-
<LinearLayout
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:orientation="horizontal"
-
>
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="账号:"
-
android:textSize="30dp"
-
/>
-
<EditText
-
-
android:id="@ id/account"
-
android:layout_width="0dp"
-
android:layout_height="wrap_content"
-
android:hint="输入账号"
-
android:singleLine="true"
-
android:maxLength="16"
-
android:layout_weight="1"
-
android:textSize="30dp"
-
/>
-
-
</LinearLayout>
-
<LinearLayout
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:orientation="horizontal"
-
>
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="密码:"
-
android:textSize="30dp"
-
android:inputType="textPassword"
-
/>
-
<EditText
-
android:id="@ id/password"
-
android:layout_width="0dp"
-
android:layout_height="wrap_content"
-
android:hint="输入密码"
-
android:singleLine="true"
-
android:maxLength="16"
-
android:layout_weight="1"
-
android:textSize="30dp"
-
/>
-
-
</LinearLayout>
-
<LinearLayout
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:orientation="horizontal"
-
>
-
-
<Button
-
android:id="@ id/lbtn1"
-
android:layout_width="0dp"
-
android:layout_weight="1"
-
android:layout_height="wrap_content"
-
android:text="登录"
-
android:textSize="30dp"
-
/>
-
<Button
-
android:id="@ id/lbtn2"
-
android:layout_width="0dp"
-
android:layout_weight="1"
-
android:layout_height="wrap_content"
-
android:text="取消"
-
android:textSize="30dp"
-
/>
-
-
</LinearLayout>
-
</LinearLayout>
2)Java代码
- dismiss,可以设置消失
-
// 6.自定义View
-
//1.获取布局
-
View view= LayoutInflater.from(this).inflate(R.layout.login,null);
-
//2.获取布局中的控件
-
EditText account=view.findViewById(R.id.account);
-
EditText password=view.findViewById(R.id.password);
-
Button lbtn1=view.findViewById(R.id.lbtn1);
-
Button lbtn2=view.findViewById(R.id.lbtn2);
-
-
//3.创建构造器
-
AlertDialog.Builder builder=new AlertDialog.Builder(this);
-
//4.设置参数
-
builder.setTitle("输入指定的登录信息")
-
.setIcon(R.mipmap.city)
-
.setView(view)
-
.setPositiveButton("确认",new DialogInterface.OnClickListener(){
-
-
@Override
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNegativeButton("取消",new DialogInterface.OnClickListener(){
-
-
-
@Override
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNeutralButton("忽略",new DialogInterface.OnClickListener(){
-
-
@Override
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
-
});
-
//5.创建对象,
-
AlertDialog alertDialog=builder.create();
-
//6.单独设置事件监听器
-
lbtn1.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View view) {
-
if (account.getText().toString().equals("1001")&&password.getText().toString().equals("123456")){
-
Toast.makeText(DialogTest.this, "登录成功!", Toast.LENGTH_SHORT).show();
-
//===设置对话框消失===
-
alertDialog.dismiss();
-
}
-
}
-
});
-
-
lbtn2.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View view) {
-
Toast.makeText(DialogTest.this, "取消登录!", Toast.LENGTH_SHORT).show();
-
//===设置对话框消失===
-
alertDialog.dismiss();
-
}
-
});
-
//7.显示对象
-
alertDialog.show();
-
}
3)改建
-
public void loginAlert(View view1) {
-
// 6.自定义View
-
//1.获取布局
-
View view= LayoutInflater.from(this).inflate(R.layout.login,null);
-
//2.获取布局中的控件
-
EditText account=view.findViewById(R.id.account);
-
EditText password=view.findViewById(R.id.password);
-
Button lbtn1=view.findViewById(R.id.lbtn1);
-
Button lbtn2=view.findViewById(R.id.lbtn2);
-
-
//3.创建构造器
-
AlertDialog.Builder builder=new AlertDialog.Builder(this);
-
//4.设置参数
-
builder.setTitle("输入指定的登录信息")
-
.setIcon(R.mipmap.city)
-
.setView(view)
-
.setPositiveButton("确认",new DialogInterface.OnClickListener(){
-
-
@Override
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNegativeButton("取消",new DialogInterface.OnClickListener(){
-
-
-
@Override
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
})
-
.setNeutralButton("忽略",new DialogInterface.OnClickListener(){
-
-
@Override
-
public void onClick(DialogInterface dialogInterface, int i) {
-
-
}
-
-
});
-
//5.创建对象,
-
AlertDialog alertDialog=builder.create();
-
//6.单独设置事件监听器
-
lbtn1.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View view) {
-
if (account.getText().toString().equals("1001")&&password.getText().toString().equals("123456")){
-
Toast.makeText(DialogTest.this, "登录成功!", Toast.LENGTH_SHORT).show();
-
//===设置对话框消失===
-
alertDialog.dismiss();
-
}
-
}
-
});
-
-
lbtn2.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View view) {
-
Toast.makeText(DialogTest.this, "取消登录!", Toast.LENGTH_SHORT).show();
-
//===设置对话框消失===
-
alertDialog.dismiss();
-
}
-
});
-
//7.显示对象
-
alertDialog.show();
-
}
点击弹出框之后的
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgcakke
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01