赞
踩
自定义对话框是经常使用的功能,我们常用的弹窗操作,除了使用popwindow(popupwindow的简单实用)就是使用dialog来实现,这两种组件都支持之定义布局和功能来满足我们个性化的需求,也可以不采用自定义而直接使用系统封装好的api来实现功能。今天简单总结下在使用dialog做弹窗功能的方法和会遇到的问题与解决方案
方法一:直接使用系统的,不自定义布局和功能方式,效果如下图:
点击事件中代码如下:
AlertDialog.Builder dialog = new AlertDialog.Builder(GroupInfoActivity.this); dialog.setTitle("This is Dialog"); dialog.setMessage("Something important"); dialog.setCancelable(false); dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //自己的逻辑 } }); dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //自己的逻辑 } }); dialog.show();
方法二:效果如下图
代码如下:
1,:自定义我们的dialog类如下所示
import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.text.TextUtils; import android.view.View; import android.widget.TextView; import com.im.R; /** * 公用的Dialog,提示用户的一些操作 * Created by AndyYuan on 2018/12/26. */ public class CommonDialog extends Dialog implements View.OnClickListener { private TextView contentTxt; private TextView titleTxt; private TextView submitTxt; private TextView cancelTxt; private Context mContext; private String content; private OncloseListener listener; private String positiveName; private String negativeName; private String title; public CommonDialog(@NonNull Context context) { super(context); this.mContext = context; } public CommonDialog(@NonNull Context context, int themeResId, String content) { super(context, themeResId); this.mContext = context; this.content = content; } public CommonDialog(@NonNull Context context, int themeResId, String content, OncloseListener listener) { super(context, themeResId); this.mContext = context; this.content = content; this.listener = listener; } protected CommonDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) { super(context, cancelable, cancelListener); this.mContext = context; } public CommonDialog setTitle(String title) { this.title = title; return this; } public CommonDialog setPositiveButton(String name) { this.positiveName = name; return this; } public CommonDialog setNegativeButton(String name) { this.negativeName = name; return this; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_common); setCanceledOnTouchOutside(false); initView(); } private void initView() { contentTxt = (TextView) findViewById(R.id.content); titleTxt = (TextView) findViewById(R.id.title); submitTxt = (TextView) findViewById(R.id.submit); submitTxt.setOnClickListener(this); cancelTxt = (TextView) findViewById(R.id.cancel); cancelTxt.setOnClickListener(this); contentTxt.setText(content); if (!TextUtils.isEmpty(positiveName)) { submitTxt.setText(positiveName); } if (!TextUtils.isEmpty(negativeName)) { cancelTxt.setText(negativeName); } if (!TextUtils.isEmpty(title)) { titleTxt.setText(title); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.cancel: if (listener != null) { listener.onClick(false); } this.dismiss(); break; case R.id.submit: if (listener != null) { listener.onClick(true); } this.dismiss(); break; } } public interface OncloseListener { void onClick(boolean confirm); } }
2:定义公共的Dialog的布局:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/bg_round_white"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/title"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="12dp"
- android:gravity="center_horizontal"
- android:padding="12dp"
- android:text="这是dialog的标题"
- android:textColor="@color/black"
- android:textSize="14sp" />
-
- <TextView
- android:id="@+id/content"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:layout_marginBottom="30dp"
- android:layout_marginLeft="40dp"
- android:layout_marginRight="40dp"
- android:layout_marginTop="20dp"
- android:gravity="center"
- android:lineSpacingExtra="3dp"
- android:text="这里是dialog中的内容"
- android:textSize="16sp" />
-
- <View
- android:layout_width="match_parent"
- android:layout_height="1dp"
- android:background="@color/light_gray" />
-
- <!--<LinearLayout-->
- <!--android:layout_width="match_parent"-->
- <!--android:layout_height="50dp"-->
- <!--android:orientation="horizontal">-->
-
- <!--<TextView-->
- <!--android:id="@+id/cancel"-->
- <!--android:layout_width="match_parent"-->
- <!--android:layout_height="match_parent"-->
- <!--android:layout_weight="1.0"-->
- <!--android:background="@drawable/bg_dialog_left_white"-->
- <!--android:gravity="center"-->
- <!--android:text="@string/cancel"-->
- <!--android:textSize="12sp" />-->
-
- <!--<View-->
- <!--android:layout_width="1dp"-->
- <!--android:layout_height="match_parent"-->
- <!--android:background="@color/gray" />-->
-
- <!--<TextView-->
- <!--android:id="@+id/submit"-->
- <!--android:layout_width="match_parent"-->
- <!--android:layout_height="match_parent"-->
- <!--android:layout_weight="1.0"-->
- <!--android:background="@drawable/bg_dialog_right_white"-->
- <!--android:gravity="center"-->
- <!--android:text="@string/ok"-->
- <!--android:textSize="12sp" />-->
-
- <!--</LinearLayout>-->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="51dp"
- android:gravity="center"
- android:orientation="horizontal">
-
- <TextView
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="2" />
-
- <TextView
- android:id="@+id/cancel"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center_vertical"
- android:text="取消"
- android:textColor="#999999" />
-
- <TextView
- android:id="@+id/submit"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center_vertical"
- android:text="确定"
- android:textColor="#E84242" />
- </LinearLayout>
-
- </LinearLayout>
3:在style里面定义我们自己的样式:
- <!--自定义的dialog的style设置-->
- <style name="commonDialog" parent="@android:style/Theme.Dialog">
- <item name="android:windowFrame">@null</item>
- <!--边框-->
- <item name="android:windowIsFloating">true</item>
- <!--是否浮现在activity之上-->
- <item name="android:windowIsTranslucent">false</item>
- <!--半透明-->
- <item name="android:windowNoTitle">true</item>
- <!--无标题-->
- <item name="android:windowBackground">@android:color/transparent</item>
- <!--背景透明-->
- <item name="android:backgroundDimEnabled">true</item>
- <!--模糊-->
- </style>
4:其中bg_round_white.xml文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="@color/white" />
- <corners android:radius="8dp" />
- </shape>
5:调用方式很简单如下,我的是在一个点击事件里调用的如下代码:
- new CommonDialog(GroupInfoActivity.this, R.style.commonDialog, "确定删除群聊天记录?",
- confirm -> {
- if (confirm) {
- 这里都是自己的需要的逻辑操作......................
- Toast.makeText(GroupInfoActivity.this, "清空聊天记录成功", Toast.LENGTH_SHORT).show();
- }
- }).setTitle("提示").show();
方法三:采用自定义和功能方式效果也不错:
//初始化并弹出对话框方法 private void showDialog () { View view = LayoutInflater.from(this).inflate(R.layout.high_opinion_dialog_layout, null, false); final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create(); Button btn_cancel_high_opion = view.findViewById(R.id.btn_cancel_high_opion); Button btn_agree_high_opion = view.findViewById(R.id.btn_agree_high_opion); btn_cancel_high_opion.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferencesUnitls.setParam(getApplicationContext(), "HighOpinion", "false"); //... To-do dialog.dismiss(); } }); btn_agree_high_opion.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //... To-do dialog.dismiss(); } }); dialog.show(); //此处设置位置窗体大小,我这里设置为了手机屏幕宽度的3/4 dialog.getWindow().setLayout((ScreenUtils.getScreenWidth(this) / 4 * 3), LinearLayout.LayoutParams.WRAP_CONTENT); }
其中ScreenUtils工具类如下所示:
public class ScreenUtils { /** * 获取屏幕高度(px) */ public static int getScreenHeight(Context context) { return context.getResources().getDisplayMetrics().heightPixels; } /** * 获取屏幕宽度(px) */ public static int getScreenWidth(Context context) { return context.getResources().getDisplayMetrics().widthPixels; } }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。