自定义自己的AlertDialog
16lz
2021-12-04
android提供的弹出框很单调,往往在开发中需要定义自己的AlertDialog来适应开发的需要
android用户帮助里面的方法是这样:
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MyActivity.this.finish(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create();
效果如图
1 实现单选框的弹出框
builder = new AlertDialog.Builder(MainActivity .this); builder.setTitle("选择哪个?") .setSingleChoiceItems(item,gameMethodValuesIndex,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { gameMethodValuesIndex = which; Toast.makeText(getApplicationContext(), ""+item[which], Toast.LENGTH_SHORT).show(); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton("取消", null); AlertDialog dialog = builder.create(); dialog.show();
item 是一个数组,自己定义一个就行了,编辑器不太会用,暂不上传了
效果如图:
(本地图片上传不了。。。擦,还是我没有找到方法?有知道的童鞋告诉一下,俺只找到了上传网络图片的方法)
要实现上面的效果还可以这样,自定义配置文件
LayoutInflater inflater = null; inflater = (LayoutInflater) getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.main, null);builder = new AlertDialog.Builder(MainActivity.this);builder.setTitle("选择哪个?").setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {}}).setNegativeButton("取消", null).setView(view);AlertDialog dialog = builder.create();dialog.show();
掌握了这个用法基本上自定义alertdialog就没什么了,可以应对大部分的开发
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- android UI开发之RecyclerView(一)简单实现
- onRetainNonConfigurationInstance和getLastNonConfigurationIns
- 键盘自动弹出解决
- Android获取本机Mac地址及IP地址方法
- android appwidget service的初始化
- Android退出程序的多种方法