step1: MainActivity 首页
package com.example.dialogfragmentdemotwo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestingDialogFragment testingDialogFragment = new TestingDialogFragment();
testingDialogFragment.show(getFragmentManager(), "");
testingDialogFragment.onSetClickDialogListener(new TestingDialogFragment.SetOnClickDialogListener() {
@Override
public void onClickDialogListener(int type, boolean boolClick, String content) {
Log.e("TAG", "onClickDialogListener: " + type + "\t" + boolClick + "\t" + content);
}
});
}
}
step2: dialogfragmentdemotwo\TestingDialogFragment.java 弹框页
package com.example.dialogfragmentdemotwo;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class TestingDialogFragment extends DialogFragment {
protected TextView tv_tip;
protected Button btn_dialog;
private SetOnClickDialogListener mSetOnClickListener;
public void onSetClickDialogListener(SetOnClickDialogListener listener) {
this.mSetOnClickListener = listener;
}
public interface SetOnClickDialogListener {
void onClickDialogListener(int type, boolean boolClick, String content);
}
@Override
public void onStart() {
super.onStart();
if (getDialog() != null && getDialog().getWindow() != null) {
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setCanceledOnTouchOutside(true);
}
View mView = inflater.inflate(R.layout.dialog_test, container, false);
tv_tip = mView.findViewById(R.id.tv_tip);
btn_dialog = mView.findViewById(R.id.btn_dialog);
btn_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getDialog().cancel();
if (mSetOnClickListener != null) {
mSetOnClickListener.onClickDialogListener(0, true, "test");
}
}
});
return mView;
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
}
step3: D:\workspace\DialogFragmentDemoTwo\app\src\main\res\layout\dialog_test.xml 弹框布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="320dp"
android:layout_height="450dp"
android:background="@drawable/radius_border"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:text="@string/app_name"
android:textColor="#333333"
android:textSize="14sp"
tools:text="正在测试XXX (1/5)" />
<Button
android:id="@+id/btn_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:text="@string/app_name" />
</RelativeLayout>
</LinearLayout>
step4: 圆角边框 D:\workspace\DialogFragmentDemoTwo\app\src\main\res\drawable\radius_border.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:topRightRadius="15dp"
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"/>
<stroke android:width="1dp" android:color="#eeeeee"/>
</shape>
run,success!
end
|