AlertDialog源码理解

首先看一下AlertDialog简单的用法。

@Override
protected void onCreate(Bundle savedInstanceState) {
   
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	builder.setMessage("hello")
			.setTitle("AlertDialog")
			.create()
			.show();
}

一步一步来看看到底在源码中做了什么。
首先调用AlertDialog.Builder()创建了一个Builder类的对象,它是一个AlertDialog的内部类。

public class AlertDialog {
   
	...
	public static class Builder {
   
	
		...
		
		public Builder(@NonNull Context context) {
   
		    this(context, resolveDialogTheme(context, 0));
		}
		
		...
		
	}
	
	...
	
}

通过this调用另一个构造方法。

public Builder(@NonNull Context context, @StyleRes int themeResId) {
   
    P = new AlertController.AlertParams(new ContextThemeWrapper(
            context, resolveDialogTheme(context, themeResId)));
    mTheme = themeResId;
}

创建了一个AlertParams类的对象P。

class AlertController {
   
	...
	public static class AlertParams {
   
		...
		public AlertParams(Context context) {
   
			mContext = context;
			mCancelable = true;
			mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		...
	}
	...
}

AlertParams是AlertController的静态内部类。用户在调用Builder方法后会创建一个对应的AlertParams实例P。
接着调用了一系列Builder的set方法。我们看其中一个setTitle方法。

public Builder setTitle(@Nullable CharSequence title) {
   
    P.mTitle = title;
    return this;
}

为P中的对应属性赋值。setMessage和其他的一些set方法也是类似的

public Builder setMessage(@Nullable CharSequence message) {
   
    P.mMessage = message;
    return this;
}

通过我们调用一系列的set方法,P中的各个属性已经被填充为我们传入的参数。
接着调用了Builder的create()方法。

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值