一,概念与作用
AlertDialog是用来创建对话框,可以在当前活动界面弹出一个对话框,弹出一个选项框,弹出一个提示框,用于提示一些重要信息 或是警告内容 或者可输入内容。
二,实现原理
1,首先创建 AlertDialog.Builder 对象 用来 new一个 点creare();
2,绑定自定义布局 绑定UI
3,重要的方法 ,setView() setCanceledOnTouchOutside(); show();
代码如下
AlertDialog mDialog = new AlertDialog.Builder(mContext, R.style.MyDialog).create();
View layoutId = LinearLayout.inflate(mContext, R.layout.type_dialog_layout, null);
mDialog.setView(layoutId, 0, 0, 0, 0);
mDialog.setCanceledOnTouchOutside(true);
mDialog.show();
1,2,3这三步是非常重要的,接下来,看源码
create()方法 创建,然后直接返回 AlertDialog对象
public AlertDialog create() {
// We can't use Dialog's 3-arg constructor with the createThemeContextWrapper param,
// so we always have to re-set the theme
final AlertDialog dialog = new AlertDialog(P.mContext, mTheme);
P.apply(dialog.mAlert);
dialog.setCancelable(P.mCancelable);
if (P.mCancelable) {
dialog.setCanceledOnTouchOutside(true);
}
dialog.setOnCancelListener(P.mOnCancelListener);
dialog.setOnDismissListener(P.mOnDismissListener);
if (P.mOnKeyListener != null) {
dialog.setOnKeyListener(P.mOnKeyListener);
}
return dialog;
}
setView() 这个方法是设置显示的位置
/**
* Set the view to display in the dialog, specifying the spacing to appear around that
* view. This method has no effect if called after {@link #show()}.
*
* @param view The view to show in the content area of the dialog
* @param viewSpacingLeft Extra space to appear to the left of {@code view}
* @param viewSpacingTop Extra space to appear above {@code view}
* @param viewSpacingRight Extra space to appear to the right of {@code view}
* @param viewSpacingBottom Extra space to appear below {@code view}
*/
public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,
int viewSpacingBottom) {
mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);
}
那么 setCanceledOnTouchOutside() 是设置触摸窗口外,也可以收回弹出框
/**
* Sets whether this dialog is canceled when touched outside the window's
* bounds. If setting to true, the dialog is set to be cancelable if not
* already set.
*
* @param cancel Whether the dialog should be canceled when touched outside
* the window.
*/
public void setCanceledOnTouchOutside(boolean cancel) {
if (cancel && !mCancelable) {
mCancelable = true;
}
mWindow.setCloseOnTouchOutside(cancel);
}
三,效果

四,gif图 走起

五,贴上代码
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.ok_send);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showCoin();
}
});
}
//弹出提示
public void showCoin() {
final AlertDialog mDialog = new AlertDialog.Builder(this, R.style.MyDialog).create();
View layoutId = LinearLayout.inflate(this, R.layout.coin_type_dialog_layout, null);
mDialog.setView(layoutId, 0, 0, 0, 0);
mDialog.setCanceledOnTouchOutside(true);
mDialog.show();
// 得到当前显示设备的宽度
int mWidth = getWindowManager().getDefaultDisplay().getWidth();
// 得到此AlertDialog界面的参数对象
WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
// 设置AlertDialog的界面宽度
params.width = mWidth - (mWidth / 6);
// 设置AlertDialog高度为包裹内容
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
// 设置AlertDialog的重心
params.gravity = Gravity.CENTER;
// 设置AlertDialog的大小, 不能设置重心参数, 推荐用Attributes设置
mDialog.getWindow().setLayout(mWidth - (mWidth / 3), ViewGroup.LayoutParams.WRAP_CONTENT);
mDialog.getWindow().setAttributes(params);
}
}
六,简介
android开发,少不了弹出框。自定义弹出,以一变通百。学好一个demo后面就好办了。
看了效果图,感觉很吸引人嘛,弹出框里,还能弹出框,看代码,
代码下载链接 : AlertDialog弹出框
|