Dialog总结
AlertDialog.Builder AlertDialog Dialog辨析
Dialog implements DialogInterface, Window.Callback,KeyEvent.Callback, OnCreateContextMenuListener, window.OnWindowDismissedCallback
AlertDialog extends Dialog implements DialogInterface {
public static class Builder {}
}
一,谷歌建议使用之类AlertDialog ,不建议直接使用Dialog 。
二,AlertDialog.Builder是用来创建AlertDialog ,并简化设置参数的工具类,可直接用连续的"."符号设置参数。
如:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
TextView tv = new TextView(builder.getContext());
tv.setText("haha");
builder.setTitle("aaa")
.setView(tv)
.show();
------------------------
AlertDialog.Builder 的api
/*** Creates an {@link AlertDialog} with the arguments supplied to this* builder.* <p>* Calling this method does not display the dialog. If no additional* processing is needed, {@link #show()} may be called instead to both* create and display the dialog.*/public AlertDialog create() {// Context has already been wrapped with the appropriate theme.final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);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;}
---
/*** Creates an {@link AlertDialog} with the arguments supplied to this* builder and immediately displays the dialog.* <p>* Calling this method is functionally identical to:* <pre>* AlertDialog dialog = builder.create();* dialog.show();* </pre>*/public AlertDialog show() {final AlertDialog dialog = create();dialog.show();return dialog;}
半自定义Dialog View的两种方法
注:并非自定义view,而是利用Dialog本身提供的api进行自定义。
AlertDialog
public static void showSimpleDialog(Context context, String msg, View.OnClickListener listener) {final AlertDialog.Builder builder = new AlertDialog.Builder(context);final AlertDialog dialog = builder.create();LayoutInflater inflater = LayoutInflater.from(context);View view = inflater.inflate(R.layout.dialog_simple_deposit, null);TextView tvConfirm = (TextView) view.findViewById(R.id.tv_confirm);tvConfirm.setOnClickListener(listener);tvConfirm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});TextView tvMsg = (TextView) view.findViewById(R.id.tv_msg);tvMsg.setText(msg);dialog.setView(view);dialog.setCancelable(false);// 返回键不可取消dialog.setView(view, 0, 0, 0, 0);// 可消除部分边框,当不能全部消除dialog.show();}
--------------------------------------
Dialog
public static void showSimpleDialog1(Context context, String msg) {final Dialog dialog =new Dialog(context, R.style.dialog);// 可以消除黑色边框LayoutInflater inflater = LayoutInflater.from(context);View view = inflater.inflate(R.layout.dialog_simple_deposit, null);TextView tvConfirm = (TextView) view.findViewById(R.id.tv_confirm);// tvConfirm.setOnClickListener(listener);tvConfirm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});TextView tvMsg = (TextView) view.findViewById(R.id.tv_msg);tvMsg.setText(msg);dialog.setContentView(view);dialog.setCancelable(false);// 返回键不可取消dialog.show();}
---
<style name="dialog" parent="@android:style/Theme.Dialog"><item name="android:windowFrame">@null</item><item name="android:windowIsFloating">true</item><item name="android:windowIsTranslucent">true</item><item name="android:windowNoTitle">true</item><item name="android:background">@android:color/transparent</item><item name="android:windowBackground">@android:color/transparent</item><item name="android:backgroundDimEnabled">true</item><item name="android:backgroundDimAmount">0.6</item></style>
浙公网安备 33010602011771号