Android Dialog 的一些使用和优化心得(DialogFragment 的使用和优化)
private TextView tv_no;
private OnYesClickListener onYesClickListener;
private onNoClickListener onNoClickListener;
public void setOnNoClickListener(ExitFragment.onNoClickListener onNoClickListener) {
this.onNoClickListener = onNoClickListener;
}
public void setOnYesClickListener(OnYesClickListener onYesClickListener) {
this.onYesClickListener = onYesClickListener;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (rootView == null)
{
rootView = inflater.inflate(R.layout.fragment_exit,container,false);
this.tv_yes = rootView.findViewById(R.id.tv_dialog_yes);
this.tv_no = rootView.findViewById(R.id.tv_dialog_no);
}
return rootView;
}
public interface OnYesClickListener{
void onYesClickener();
}
public interface onNoClickListener{
void onNoClicked();
}
@Override
public void onResume() {
super.onResume();
this.tv_yes.setOnClickListener(v -> onYesClicked());
this.tv_no.setOnClickListener(v -> onNoClicked());
}
private void onNoClicked() {
if (onNoClickListener != null)
{
onNoClickListener.onNoClicked();
}
}
private void onYesClicked() {
if (onYesClickListener != null)
{
onYesClickListener.onYesClickener();
}
}
}
布局:
<?xm
l version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripper_white_bg"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:background="@drawable/ripper_white_bg"
android:textSize="16sp"
android:textColor="#4A4A4A"
android:id="@+id/tv_dialog_title"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:gravity="center_horizontal"
android:text="@string/tv_confirm_exit_system"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="@+id/view_dialog_line_hor"
android:layout_marginTop="6dp"
app:layout_constraintTop_toBottomOf="@id/tv_dialog_title"
android:background="@color/black_ef"
android:layout_width="match_parent"
android:layout_height="1dp" />
<TextView
android:layout_marginBottom="20dp"
android:paddingRight="6dp"
android:paddingLeft="6dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:id="@+id/tv_dialog_no"
android:background="@drawable/ripper_white_bg"
app:layout_constraintRight_toLeftOf="@id/view_dialog_ver_line"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:textColor="#9B9B9B"
android:textSize="14sp"
android:layout_marginTop="20dp"
android:text="@string/tv_confirm_no"
app:layout_constraintTop_toBottomOf="@id/view_dialog_line_hor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:id="@+id/view_dialog_ver_line"
android:layout_marginTop="20dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_dialog_line_hor"
android:background="@color/black_ef"
android:layout_width="1dp"
android:layout_height="40dp" />
<TextView
评论