自定义自己的AlertDialog
16lz
2021-01-23
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就没什么了,可以应对大部分的开发
更多相关文章
- Android Material Design 进度条 自定义进度条样式
- android之4.0控件switch自定义开关滑块、滑块轨道和宽度
- Android退出程序的多种方法
- Android数据共享 sharedPreferences 的使用方法
- Android Studio使用过程中遇到的问题(一)-- "value 2"异常解决方法
- 简单总结RectF、Rect 和Matrix ,还有Paint的使用方法
- android和ios button点击变暗效果
- 关于用Android的API重写drawRegion方法的代码