资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在Android中利用Dialog实现一个对话框功能

今天就跟大家聊聊有关如何在Android中利用Dialog实现一个对话框功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联公司自2013年创立以来,先为庆安等服务建站,庆安等地企业,进行企业商务咨询服务。为庆安企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

一、普通对话框

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("温馨提示");//标题
builder.setMessage("天气冷,注意保暖");
builder.setIcon(R.mipmap.ic_launcher);
builder.create();
builder.show();

普通对话框

二、确定取消对话框

builder.setTitle("确定取消对话框");
builder.setMessage("请选择确定或取消");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
 //正能量按钮 Positive
 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "你点击了确定", Toast.LENGTH_SHORT).show();
 }
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "你选择了取消", Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

确定取消对话框

三、多按钮对话框

builder.setTitle("多个按钮对话框");
builder.setMessage("请选择");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("我没玩够", new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "继续浏览精彩内容", Toast.LENGTH_SHORT).show();
 }
});
builder.setNeutralButton("开启", new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "起床了", Toast.LENGTH_SHORT).show();
 }
});
builder.setNegativeButton("我累了,要休息一下", new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "欢迎再来", Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

多按钮对话框

四、列表对话框

final String arrItem[] = getResources().getStringArray(R.array.aikaifa);
builder.setItems(arrItem, new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "你选择了第" + arrItem[which], Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

列表对话框

五、带Adapter的对话框

builder.setTitle("带Adapter的对话框");
builder.setIcon(R.mipmap.ic_launcher);
final List> list = new ArrayList>();
int arrImg[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
 R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
 R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
for (int i = 0; i < arrImg.length; i++) {
 Map map = new HashMap();
 map.put("img", arrImg[i]);
 map.put("title", "爱开发" + i);
 list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(activity, list, R.layout.list_item, new String[]{"img", "title"}, new int[]{R.id.iv, R.id.tv});
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, "你选择了" + list.get(which).get("title").toString().trim(), Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

带Adapter的对话框

六、单选对话框

builder.setTitle("单选对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setSingleChoiceItems(R.array.aikaifa, 0, new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {
 Toast.makeText(activity, which+"", Toast.LENGTH_SHORT).show();
 }
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {

 }
});
builder.create().show();

单选对话框

七、多选对话框

builder.setTitle("多选对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMultiChoiceItems(R.array.aikaifa, null, new DialogInterface.OnMultiChoiceClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
 Toast.makeText(activity, which+""+isChecked, Toast.LENGTH_SHORT).show();
 }
});
builder.create().show();

多选对话框

八、日期对话框

 DatePickerDialog datePickerDialog=new DatePickerDialog(activity,
 new DatePickerDialog.OnDateSetListener() {

  @Override
  public void onDateSet(DatePicker view, int year, int monthOfYear,
 int dayOfMonth) {
Toast.makeText(activity,
 year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show();
  }
 },
 2017, 02, 9);
datePickerDialog.show();

日期对话框

九、时间对话框

TimePickerDialog timePickerDialog=new TimePickerDialog(activity,
 new TimePickerDialog.OnTimeSetListener() {

  @Override
  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(activity,
 hourOfDay+"小时"+minute+"分钟", Toast.LENGTH_SHORT).show();
  }
 },
 17, 49, true);
timePickerDialog.show();

时间对话框

十、自定义对话框

View view= LayoutInflater.from(activity).inflate(R.layout.dialog_login, null);
builder.setView(view);
builder.create();
final EditText et_phone=(EditText)view.findViewById(R.id.et_phone);
final EditText et_password=(EditText)view.findViewById(R.id.et_password);
Button btn_submit=(Button)view.findViewById(R.id.btn_submit);
btn_submit.setOnClickListener( new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 Toast.makeText(activity, "手机号码:"+et_phone.getText().toString()+" 短信验证码:"+et_password.getText().toString(), Toast.LENGTH_SHORT).show();
 }
});
builder.show();

自定义对话框

项目设计到的xml

list_item.xml

<?xml version="1.0" encoding="utf-8"?>

 
 

dialog_login.xml

<?xml version="1.0" encoding="utf-8"?>


 

 

 

 

  短信验证码"
  android:inputType="number"
  android:maxLength="6"
  android:paddingLeft="10dp"
  android:textSize="14sp" />

  
 

 

看完上述内容,你们对如何在Android中利用Dialog实现一个对话框功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


网站栏目:如何在Android中利用Dialog实现一个对话框功能
URL网址:http://www.cdkjz.cn/article/igdohd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220