Dialog(弹窗)

Dialog一般用自定义的,系统自带的也有

//系统自带Dialog
AlertDialog.Builder b=new AlertDialog.Builder(Reign.this); b.setTitle("是否登录");//设置标题 b.setMessage("是否登录"); //设置内容 b.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent i = new Intent(); Bundle bundle=new Bundle(); i.setClass(Reign.this, Login.class); bundle.putSerializable("data", (Serializable) user); i.putExtras(bundle); startActivity(i); dialog.dismiss(); } }); b.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Reign.this, "还是登录吧", Toast.LENGTH_SHORT).show(); dialog.dismiss();//销毁弹窗 } }); b.create().show();//先create,再show
    //
b.setCancelable(true);点击空白处,是否消失弹窗,默认是true,消失
 
 

//学了自定义在说

转载于:https://www.cnblogs.com/Xacm/p/5465940.html

从底部弹出的dialog。位置你可以在base里自己改。使用方法都有。public class BaseDialog extends Dialog { private View mContentView; public Context mContext; public LayoutInflater mInflater; public BaseDialog(Context context) { this(context, R.style.BaseDialogStyle); mContext = context; } public BaseDialog(Context context, int themeResId) { super(context, themeResId); mContext = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.dialog_base); Window dialogWindow = getWindow(); WindowManager.LayoutParams lp1 = dialogWindow.getAttributes(); WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); int width = outMetrics.widthPixels; lp1.width = width; dialogWindow.setGravity(Gravity.BOTTOM); dialogWindow.setAttributes(lp1); mInflater = LayoutInflater.from(mContext); } public void setContentView(int layoutResID) { mContentView = View.inflate(mContext, layoutResID, null); LinearLayout view = (LinearLayout) findViewById(R.id.base_container); view.addView(mContentView); } public View getContextView() { return mContentView; } } public class CustomDialog extends BaseDialog implements View.OnClickListener { /** * 标题 */ private TextView mTitleTv; /** * 确定按钮 */ private Button mPositiveBt; /** * 取消按钮 */ private Button mNegativeBt; /** * 确定取消中间分割线 */ private View mDiverView; /** * 内容容器 */ private LinearLayout mContentLin; private View.OnClickListener mPositiveClickListener; private View.OnClickListener mNegativeClickListener; private List<String> mContents; private String mContent; private String mTilte; private String mPositiveText; private String mNegativeText; private CustomDialog(Context context) { super(context); } private CustomDialog(Context context, int themeResId) { super(context, themeResId); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_one_button); mTitleTv = (TextView) findViewById(R.id.title); mContentLin = (LinearLayout) findViewById(R.id.container); mPositiveBt = (Button) findViewById(R.id.positiveButton); mNegativeBt = (Button) findViewById(R.id.negativeButton); mDiverView = findViewById(R.id.diver); mPositiveBt.setOnClickListener(this); mNegativeBt.setOnClickListener(this); setContent(mContent); setContents(); setTitle(); setButtonClickListener(); setNegativeBtText(); setPositiveBtText(); } private void setContents() { if (mContents != null && mContents.size() != 0) { if (mContents.size() == 1) { setContent(mContents.get(0)); } else { for (int i = 0; i < mContents.size(); i++) { View view = mInflater.inflate(R.layout.diaolog_content_item_left, null); TextView tv = (TextView) view.findViewById(R.id.tv); tv.setText(mContents.get(i)); mContentLin.addView(view); } } } } private void setContent(String content) { if (!TextUtils.isEmpty(content)) { View view = mInflater.inflate(R.layout.diaolog_content_item_center, null); TextView tv = (TextView) view.findViewById(R.id.tv); tv.setText(content); mContentLin.addView(view); } } /** * 设置标题 */ private void setTitle() { if (!TextUtils.isEmpty(mTilte)) { mTitleTv.setVisibility(View.VISIBLE); mTitleTv.setText(mTilte); } } /** * 设置右侧按钮的显示文字,默认为确定 */ private void setPositiveBtText() { if (!TextUtils.isEmpty(mPositiveText)) { mPositiveBt.setText(mPositiveText); } } /** * 设置左侧按钮的显示文字,默认为取消 */ private void setNegativeBtText() { if (!TextUtils.isEmpty(mNegativeText)) { mNegativeBt.setText(mNegativeText); } } /** * 设置监听 可不设置 */ private void setButtonClickListener() { if (mPositiveClickListener != null) { mPositiveBt.setVisibility(View.VISIBLE); } if (mNegativeClickListener != null) { mNegativeBt.setVisibility(View.VISIBLE); } if (mNegativeClickListener != null && mPositiveClickListener != null) { mDiverView.setVisibility(View.VISIBLE); } } public static class Builder { private CustomDialog dialog; public Builder(Context context) { dialog = new CustomDialog(context); } public Builder(Context context, int themeResId) { dialog = new CustomDialog(context, themeResId); } /** * 通过Builder类创建dialog */ public CustomDialog create() { return dialog; } /** * 多条提示 */ public Builder setContent(List<String> contents) { dialog.mContents = contents; return this; } /** * 单条提示 */ public Builder setContent(String content) { dialog.mContent = content; return this; } /** * 设置标题 */ public Builder setTitle(String tilte) { dialog.mTilte = tilte; return this; } /** * 设置右侧按钮的显示文字,默认为确定 * * @param positiveText 按钮上的显示内容 */ public Builder setPositiveBtText(String positiveText) { dialog.mPositiveText = positiveText; return this; } /** * 设置左侧按钮的显示文字,默认为取消 * * @param negativeText 按钮上的显示内容 */ public Builder setNegativeBtText(String negativeText) { dialog.mNegativeText = negativeText; return this; } ; /** * 设置监听 可不设置 * * @param positiveClickListener 确定按钮 * @param negativeClickListener 确定取消按钮 */ public Builder setButtonClickListener(View.OnClickListener positiveClickListener, View.OnClickListener negativeClickListener) { dialog.mPositiveClickListener = positiveClickListener; dialog.mNegativeClickListener = negativeClickListener; return this; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.positiveButton: if (mPositiveClickListener != null) { mPositiveClickListener.onClick(v); dismiss(); } break; case R.id.negativeButton: if (mNegativeClickListener != null) { mNegativeClickListener.onClick(v); dismiss(); } break; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值