版权声明:本文为博主原创文章,转载希望能注明出处,感谢。 https://blog.csdn.net/u010126792/article/details/83505184
android中弹框很多种,alert框,dialog,AlertDialog,popupwindow,DialogFragment,toast,dialog样式的activity等等,今天讲解AlertDialog的简单使用。
1 AlertDialog.Builder 设置相关参数
图标,title,message,按钮,是否点击消失等等。
public void showDialog(){
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.mipmap.ic_launcher);
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//title上的图标和文本
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("发现新版本");
//自定义的title
//builder.setCustomTitle(imageView);
//dialog中间显示的信息
// builder.setMessage("Message");
//设置是否可以取消
builder.setCancelable(false);
//Set a custom view resource to be the contents of the Dialog
//不是全部view,只是message下的contents
//builder.setView(R.layout.activity_main);
builder.setItems(new String[]{"aaaa", "bbbbb", "cccc"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//多选,单选item
// builder.setMultiChoiceItems();
// builder.setSingleChoiceItems();
//确认按钮
builder.setPositiveButton(
"立即更新",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//具体位置和代码加载顺序无关,跟样式有关
builder.setNeutralButton("其他",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//title上的图标和文本
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("发现新版本");
//dialog中间显示的信息
builder.setMessage("Message");
//设置是否可以取消
builder.setCancelable(false);
//Set a custom view resource to be the contents of the Dialog
//不是全部view,只是message下的contents
builder.setView(R.layout.activity_main);
//确认按钮
builder.setPositiveButton(
"立即更新",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
如果只想显示按钮,不错任何操作:
builder.setNegativeButton(“取消”,null);
//点击外部是否消失
setCanceledOnTouchOutside( false );
2 dialog 设置(字体大小,颜色设置)
dialog.show之后,依然可以进行dialog的设置。
AlertDialog dialog = builder.create();
dialog.show();
// 必须在执行show之后才能来设置
TextView tvMsg = (TextView) dialog.findViewById(android.R.id.message);
tvMsg.setTextSize(16);
tvMsg.setTextColor(Color.RED);
//设置按钮的字体,颜色
dialog.getButton(dialog.BUTTON_NEGATIVE).setTextSize(17);
dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(Color.BLUE);
dialog.getButton(dialog.BUTTON_POSITIVE).setTextSize(17);
dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(Color.BLACK);
设置自定义view:
AlertDialog dialog = builder.create();
dialog.show();
dialog.setContentView(R.layout.activity_main);
TextView textView = dialog.findViewById(R.id.idtv);
textView.setText("sldfjslfjlsfjsf");
textView.setTextColor(Color.RED);
源码解释:
/**
* Set the screen content from a layout resource. The resource will be
* inflated, adding all top-level views to the screen.
*
* @param layoutResID Resource ID to be inflated.
*/
public void setContentView(@LayoutRes int layoutResID) {
mWindow.setContentView(layoutResID);
}
dialog重新获取事件,进行更新:
dialog.getButton(dialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
设置背景:
dialog.getWindow().setBackgroundDrawable( new ColorDrawable( 0 ) );