赞
踩
这是一个基于Dialog类封装的弹出确认框样式。
效果图:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 圆角半径 -->
<corners android:radius="20dp" />
<!-- 背景颜色 -->
<solid android:color="#FFF" />
</shape>
<!--对话框样式-->
<style name="mdialog" parent="Theme.AppCompat.Dialog">
<!--背景透明-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--没有标题-->
<item name="android:windowNoTitle">true</item>
<!--背景昏暗-->
<item name="android:backgroundDimEnabled">true</item>
</style>
android:background="@drawable/my_dialog_style"
<?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:layout_gravity="center" android:background="@drawable/my_dialog_style" android:orientation="vertical"> <TextView android:id="@+id/dialog_title" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:paddingTop="10dp" android:text="恢复默认值" android:textStyle="bold" android:textColor="#353535" android:textSize="18sp" /> <TextView android:id="@+id/dialog_content" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:text="恢复到默认设置。" android:textSize="15sp" android:textColor="#666666"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CFCFCF" /> <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="取消" android:textStyle="bold" android:textColor="#4848f5" android:textSize="16sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#CFCFCF" /> <TextView android:id="@+id/confirm" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="确定" android:textStyle="bold" android:textColor="#4848f5" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
public class MyDialog extends Dialog implements View.OnClickListener { private TextView mTitle, mContent; private TextView mConfirm, mCancel; private Context mContext; private String content; private OncloseListener listener; private String positiveName; private String negativeName; private String title; public MyDialog(Context context) { super(context); this.mContext = context; } public MyDialog(@NonNull Context context, int themeResId, String content) { super(context, themeResId); this.mContext = context; this.content = content; } public MyDialog(@NonNull Context context, int themeResId, OncloseListener listener) { super(context, themeResId); this.mContext = context; this.listener = listener; } public MyDialog(@NonNull Context context, int themeResId, String content, OncloseListener listener) { super(context, themeResId); this.mContext = context; this.content = content; this.listener = listener; } protected MyDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) { super(context, cancelable, cancelListener); this.mContext = context; } /** * 设置弹框标题 * @param title 标题内容 * @return */ public MyDialog setTitle(String title) { this.title = title; return this; } /** * 设置弹框的提示内容 * @param content 弹框的提示内容 * @return */ public MyDialog setContent(String content) { this.content = content; return this; } /** * 设置弹框确认键的内容 * @param name 确认键显示内容 * @return */ public MyDialog setPositiveButton(String name) { this.positiveName = name; return this; } /** * 设置弹框取消键的内容 * @param name 取消键显示内容 * @return */ public MyDialog setNegativeButton(String name) { this.negativeName = name; return this; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_dialog); setCanceledOnTouchOutside(false); mTitle = findViewById(R.id.dialog_title); mContent = findViewById(R.id.dialog_content); mConfirm = findViewById(R.id.confirm); mCancel = findViewById(R.id.cancel); mConfirm.setOnClickListener(this); mCancel.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.confirm: if (listener != null) { listener.onClick(true); } this.dismiss(); break; case R.id.cancel: if (listener != null) { listener.onClick(false); } this.dismiss(); break; } } public interface OncloseListener { void onClick(boolean confirm); } }
1)界面布局文件res/layout/activity_test.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".TestActivity"> <android.support.constraint.ConstraintLayout android:id="@+id/default_layout" android:layout_width="0dp" android:layout_height="60dp" android:layout_marginTop="10dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/rangeBar_minus"> <LinearLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="25dp" android:layout_marginEnd="25dp" android:layout_marginBottom="0dp" android:background="#dbdbdb" android:orientation="horizontal" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <TextView android:id="@+id/restore_defaults" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginStart="30dp" android:gravity="center_vertical" android:text="恢复默认值" android:textColor="@android:color/black" android:textSize="16sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <ImageView android:layout_width="30dp" android:layout_height="wrap_content" android:src="@mipmap/next" android:layout_marginEnd="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
public class TestActivity extends AppCompatActivity { private TextView restoreDefaults; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); restoreDefaults = findViewById(R.id.restore_defaults); restoreDefaults.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(); } }); } /** * 显示对话框 */ private void showDialog() { MyDialog dialog = new MyDialog(TestActivity.this, R.style.mdialog, new MyDialog.OncloseListener() { @Override public void onClick(boolean confirm) { if (confirm) { // TODO: } else { // TODO: } } }); dialog.show(); } }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。