通过Toast类显示消息提示框
特点:
没有任何控制按钮
不会获得焦点
经过一段时间后会自动小数
Toast.makeText(this,"要显示的内容",Toast.LENGTH_SHORT).show()
setDuration(int duration) | 用于设置消息提示框持续时间的长短,通常使用Toast。LENGTH_LONG 或者 Toast.LENGTH_SHORT参数 |
setGravity(int gravity,int xOffset,int yOffset) | 指定具体偏移值 |
setMargin(float horizontalMargin,float VerticalMargin) | 页边距 |
setText(CharSequence s) | 文本内容 |
setView(View view) | 显示视图 |
使用AlertDialog类实现对话框
AlertDialog类的常用方法
setTitle() | 对话框设置标题 |
setIcon() | 使用Drawable资源为对话框设置图标 |
setMessage | 显示内容 |
setButton | BUTTON_POSITIVE(确定按钮) BUTTON_NEGATIVE(取消按钮) BUTTON_NEUTRAL(中立按钮) |
实列
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center_horizontal"
android:background="@drawable/bg1"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/word"
android:background="#38cfd1"
android:textSize="@dimen/textSize"
android:layout_marginBottom="@dimen/activity_horizontal_margin"
android:text="@string/button1" />
<Button
android:id="@+id/buttonlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/word"
android:background="#3961b7"
android:textSize="@dimen/textSize"
android:layout_marginBottom="@dimen/activity_horizontal_margin"
android:text="@string/button2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/word"
android:background="#43b243"
android:textSize="@dimen/textSize"
android:layout_marginBottom="@dimen/activity_horizontal_margin"
android:text="@string/button3" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/word"
android:background="#f9671e"
android:textSize="@dimen/textSize"
android:text="@string/button4" />
</LinearLayout>
package com.example.testapplication;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class Activity_alertdemo extends AppCompatActivity {
private boolean[] checkedItems;//记录各列表项的状态
private String[] items;//各列表项要显示的内容
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alertdemo);
// 获取 显示带取消,确定按钮的对话框
Button button1 = (Button) findViewById(R.id.button1);
// 添加单击事件监听器
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 创建对话框对象
AlertDialog alertDialog = new AlertDialog.Builder(Activity_alertdemo.this).create();
alertDialog.setIcon(R.drawable.advise);
alertDialog.setTitle("乔布斯");
// 设置要显示的内容
alertDialog.setMessage("活着就是为了改变世界,难道还有其他原因吗?");
// 添加取消按钮
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Activity_alertdemo.this, "您点击了否按钮", Toast.LENGTH_SHORT).show();
}
});
// 添加确定按钮
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Activity_alertdemo.this, "您点击了是按钮", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show(); // 显示对话框
}
});
// 获取 显示带列表的对话框 按钮
Button button2 = (Button) findViewById(R.id.buttonlist);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Tag","点击了");
// 创建名言字符串数组
final String[] items = new String[]{"当你有使命,它会让你更专注", "要么出众,"
+ "要么出局", "活着就是为了改变世界", "求知若饥,虚心若愚"};
// 创建列表对话框对象
AlertDialog.Builder builder = new AlertDialog.Builder(Activity_alertdemo.this);
builder.setIcon(R.drawable.advise1); // 设置对话框的图标
builder.setTitle("请选择你喜欢的名言"); // 设置对话框的标题
// 添加列表项
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Activity_alertdemo.this, "您选择"+items[i], Toast.LENGTH_SHORT).show();
}
});
builder.create().show(); // 创建对话框并显示
}
});
// 获取 显示带单选列表项的对话框按钮
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 创建名字字符串数组
final String[] items = new String[]{"扎克伯格", "乔布斯", "拉里.埃里森",
"安迪.鲁宾", "马云"};
// 显示带单选列表项的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(Activity_alertdemo.this);
builder.setIcon(R.drawable.advise2);
builder.setTitle("如果让你选择,你最想做那个一个:");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// 显示选择结果
Toast.makeText(Activity_alertdemo.this, "您选择了"+ items[i], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定",null); // 添加确定按钮
builder.create().show(); // 创建对话框并显示
}
});
// 获取显示带多选列表的对话框按钮
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 记录各列表项的状态
checkedItems = new boolean[]{false,true,false,true,false};
// 各列表要显示的内容
items = new String[]{"开心消消乐", "球球大作战", "欢乐斗地主",
"梦幻西游", "超级玛丽"};
// 显示列表的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(Activity_alertdemo.this);
builder.setIcon(R.drawable.advise2);
builder.setTitle("请选择您喜爱的游戏");
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
checkedItems[i] = b; // 改变被操作列表项的状态
}
});
// 为对话框添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String result = "";
for(int j = 0; j < checkedItems.length;j++){
if(checkedItems[j]){ // 当选项被选择中
result += items[j]+ ","; // 将选项的内容添加到result中
}
}
// 当result不为空时,通过消息提示框显示选择的结果
if(!"".equals(result)){
// 去掉最后面“,”号
result = result.substring(0,result.length()-1);
Toast.makeText(Activity_alertdemo.this, "您选择了[" + result + "]", Toast.LENGTH_SHORT).show();
}
}
});
builder.create().show();
}
});
}
}