Android下常见的四种对话框

摘要:在实际开发过程有时为了能够和用户进行很好的交互,需要使用到对话框,在Android中常用的对话框有四种:普通对话框、单选对话框、多选对话框、进度对话框。

一、普度对话框

  public void click(View view)
{
//this代表MainActivity是子类 getApplicationContext代表context是父类
//对话框不能用getApplicationContext
AlertDialog.Builder builder = new AlertDialog.Builder(this);//getApplicationContext());
builder.setTitle("警告");
builder.setMessage("世界上最遥远的距离是没有网络");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Toast.makeText(mContext, "点击了确定了按钮", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Toast.makeText(mContext, "点击了取消了按钮", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}

二、单选对话框

   public void click(View view)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);//getApplicationContext());
builder.setTitle("请选择你喜欢的课");
final String item[] = {"android", "java", "c", "c++"};
builder.setSingleChoiceItems(item, -1, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
String s = item[i];
Toast.makeText(mContext, s, Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
builder.show();
}

三、多选对话框

 public void click(View view)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);//getApplicationContext());
builder.setTitle("请选出你喜欢的水果");
final String[] item = {"苹果", "榴莲", "荔枝", "黄瓜", "香蕉"};
final boolean[] checkChoisce = {true, false, false, false, true};
builder.setMultiChoiceItems(item, checkChoisce, new DialogInterface.OnMultiChoiceClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b)
{
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Toast.makeText(mContext, "点击了确定了按钮", Toast.LENGTH_SHORT).show();
StringBuffer sb = new StringBuffer();
for (int j = 0; j < item.length; j ++)
{
if (checkChoisce[j])
{
String s = item[j];
sb.append(s);
}
}
Toast.makeText(mContext, sb.toString(), Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Toast.makeText(mContext, "点击了取消了按钮", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}

四、进度对话框

   public void click(View view)
{
//与进度相关的UI都可以在子线程更新UI
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("玩命加载中");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
new Thread(){public void run(){
for (int i = 0; i < 100; i ++)
{
SystemClock.sleep(50);
dialog.setProgress(i);
}
dialog.dismiss();
}}.start();
}
上一篇:BZOJ5010 FJOI2017矩阵填数(容斥原理)


下一篇:redis常用命令-2