iOS中系统基础的弹窗主要有两种样式:UIAlertControllerStyleAlert 与 UIAlertControllerStyleActionSheet。
Android中系统基本的弹出样式有:按钮positiveButton与negativeButton, setSingleChoiceItems单选框,setMultiChoiceItems多选框以及setProgressStyle进度样式的弹窗。当然、吐司(Toast)也算是Android中比较经典的弹窗了。
iOS
UIAlertControllerStyleAlert与UIAlertControllerStyleActionSheet提示框
//多按钮选择提示框
- (void)showMutilButtonAlert
{
//两种风格preferredStyle
// typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
// UIAlertControllerStyleActionSheet = 0,//屏幕底部弹出
// UIAlertControllerStyleAlert //屏幕中央弹出
// } NS_ENUM_AVAILABLE_IOS(8_0);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"AlertTitle" message:@"这是Alert弹窗" preferredStyle:UIAlertControllerStyleActionSheet];
//UIAlertActionStyleDefault 默认的style
UIAlertAction *enter = [UIAlertAction actionWithTitle:@"enter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"这里是点击enter后的回调");
}];
//UIAlertActionStyleDestructive 会出现红色字体,用于提示操作 例如删除等
UIAlertAction *back = [UIAlertAction actionWithTitle:@"back" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"这里是点击back后的回调");
}];
//UIAlertActionStyleCancel 取消操作的style
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"这里是点击cancel后的回调");
}];
[alert addAction:enter];
[alert addAction:back];
[alert addAction:cancel];
//建议选择
alert.preferredAction = enter;
[self presentViewController:alert animated:YES completion:nil];
}
带输入框textfield的UIAlertControllerStyleAlert样式弹窗
``
-
(void)showInputAlert
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"AlertTitle" message:@"这是Alert弹窗" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//这里可设置对象的属性以及点击回调
textField.placeholder = @"请输入姓名";
textField.tag = 1;
//监听编辑内容改变
[textField addTarget:self action:@selector(textFieldAction:) forControlEvents:UIControlEventEditingChanged];
}];[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//这里可设置对象的属性以及点击回调
textField.placeholder = @"请输入电话号码";
textField.tag = 2;
//监听编辑内容
[textField addTarget:self action:@selector(textFieldAction:) forControlEvents:UIControlEventEditingChanged];
}];UIAlertAction *enter = [UIAlertAction actionWithTitle:@"enter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"这里是点击enter后的回调");
}];UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"这里是点击cancel后的回调");
}];
[alert addAction:enter];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
//监听alert中 输入框的文字改变
- (void)textFieldAction:(UITextField *)textField
{
if (textField.tag == 1) {
NSLog(@"名字为:%@",textField.text);
}else if(textField.tag == 2){
NSLog(@"电话号码为:%@",textField.text);
}
}
``
Android
界面布局xml
//确定取消对话框
//确定取消对话框
public void click1(View view){
//创建对话创建器
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("警告");
builder.setMessage("欲练此功、必先自宫");
//设置确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"确定",Toast.LENGTH_SHORT).show();
}
});
//设置取消
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
//设置 是否可取消的 空白区域取消 默认为true
builder.setCancelable(true);
AlertDialog ad = builder.create();
ad.show();
}
单选对话框
//单选对话框
public void click2(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("请来自哪里");
final String[] items = new String[]{"水星","火星","地星"};
builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,items[i],Toast.LENGTH_SHORT).show();;
dialogInterface.dismiss();
}
});
//设置 是否可取消的 空白区域取消 默认为true
builder.setCancelable(false);
builder.show(); //show方法实际上也就是先创建AlertDialog再展示
}
多选对话框
//多选对话框
public void click3(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("请选择你喜欢的女生?");
final String[] items = new String[]{"xiaocang","xiaoji","xiaosan","xiaoqiao"};
final boolean[] bItems = new boolean[]{true,false,false,false};
builder.setMultiChoiceItems(items, bItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
bItems[i] = b;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int index) {
String text = "";
for (int i = 0; i < items.length ; i++){
text += bItems[i] ? items[i] + "," : "";
}
Toast.makeText(MainActivity.this,text, Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
builder.show();
}
进度条对话框
//进度条对话框
public void click4(View view){
//创建进度条对话框
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setTitle("正在加载。。。");
dialog.setMax(100);
dialog.setProgress(0);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Thread t = new Thread(){
@Override
public void run() {
for (int i = 0 ; i < 100 ; i++){
dialog.setProgress(i);
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
dialog.dismiss();
}
};
t.start();
dialog.show();
}