Android studio 提示框Toast 弹出框AlertDialog 多种提示方法

Elaine ·
更新时间:2024-11-13
· 862 次阅读

1、Toast
在这里插入图片描述

Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { //放在UI线程弹Toast Toast.makeText(Setting.this, "正在更新请稍后", Toast.LENGTH_LONG).show(); } });

2、弹出框
2.1默认样式
在这里插入图片描述

android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this); builder.setTitle("提示"); builder.setMessage("更新成功"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogActivity.this, "确定了",Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("取消", null); builder.show();

2.2单选
在这里插入图片描述

final String[] gender = new String[]{"java","android"}; android.support.v7.app.AlertDialog.Builder builder1=new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this); builder.setTitle("请选择"); builder.setItems(gender, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastUtil.showMsg(AlertDialogActivity.this,gender[which]); } }); builder.show();

在这里插入图片描述

android.support.v7.app.AlertDialog.Builder builder2=new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this); builder.setTitle("你最喜欢吃一下那种菜肴:"); final String[] dish =new String[]{"红烧牛肉","粉蒸排骨"}; builder.setSingleChoiceItems(dish, 1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastUtil.showMsg(AlertDialogActivity.this,dish[which]); dialog.dismiss(); } }); builder.setCancelable(false); builder.show(); break;

2.3多选框
在这里插入图片描述

String[] dessert = new String[]{"抹茶千层","芒果拿破仑"}; boolean[] begin = new boolean[]{false,false}; android.support.v7.app.AlertDialog.Builder builder3=new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this); builder.setTitle("你吃过以下哪些甜点"); builder.setMultiChoiceItems(dessert, begin, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { } }); builder.setPositiveButton("确认",null); builder.setNegativeButton("取消",null); builder.show(); break;

用的次数很多可以封装一下
Prompt.java

public class Promptextends MyApplication { public static void Bill_headtk(final Bill_head ym,final String nr) { new Thread(new Runnable() { @Override public void run() { Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { //放在UI线程弹Toast AlertDialog.Builder builder=new AlertDialog.Builder(ym); builder.setTitle("提示"); builder.setMessage(nr); builder.setPositiveButton("确定",null); builder.show(); } }); } }).start(); } }

在Bill_head.java中调用

Prompt.Bill_headtk(Bill_head.this, "请输入");

其他类似


作者:晓雨哥哥写代码



Android Studio studio 方法 alertdialog toast Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章