自定义自己的AlertDialog
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就没什么了,可以应对大部分的开发
???