目录
一、DialogFragment类的实现
public class EditDialogFragment extends AppCompatDialogFragment {
public static final String TAG_BASE = "TAG_BASE";
private View root;
private Button btnCancel, btnSubmit;
private String addressId;
private static OnSuccessListener mOnSuccessListener;
private EditText etDeviceId, etAPIUrl, etPushUrl, etAppId, etAppKey, etTimeOut;
private NetConfigBean mDataBean;
public static EditDialogFragment newInstance(OnSuccessListener onSuccessListener) {
Bundle args = new Bundle();
EditDialogFragment fragment = new EditDialogFragment();
fragment.setArguments(args);
mOnSuccessListener = onSuccessListener;
return fragment;
}
public static EditDialogFragment newInstance(NetConfigBean bean, OnSuccessListener onSuccessListener) {
Bundle args = new Bundle();
args.putSerializable(TAG_BASE, bean);
EditDialogFragment fragment = new EditDialogFragment();
fragment.setArguments(args);
mOnSuccessListener = onSuccessListener;
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.dialog_fragment_edit, container, false);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
initIntentData();
initView();
initListener();
return root;
}
private void initIntentData() {
Bundle arguments = getArguments();
if (arguments != null) {
mDataBean = (NetConfigBean) arguments.getSerializable(TAG_BASE);
}
}
private void initView() {
btnSubmit = root.findViewById(R.id.btnSubmit);
btnCancel = root.findViewById(R.id.btnCancel);
etDeviceId = root.findViewById(R.id.etDeviceId);
etAPIUrl = root.findViewById(R.id.etAPIUrl);
etPushUrl = root.findViewById(R.id.etPushUrl);
etAppId = root.findViewById(R.id.etAppId);
etAppKey = root.findViewById(R.id.etAppKey);
etTimeOut = root.findViewById(R.id.etTimeOut);
if (mDataBean != null) {
etDeviceId.setText(mDataBean.deviceId);
etAPIUrl.setText(mDataBean.aipServerUrl);
etPushUrl.setText(mDataBean.pushServerUrl);
etAppId.setText(mDataBean.appId);
etAppKey.setText(mDataBean.appKey);
etTimeOut.setText(String.valueOf(mDataBean.timeOut));
}
}
private void initListener() {
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String deviceId = etDeviceId.getText().toString();
String aPIUrl = etAPIUrl.getText().toString();
String pushUrl = etPushUrl.getText().toString();
String appId = etAppId.getText().toString();
String appKey = etAppKey.getText().toString();
String timeOut = etTimeOut.getText().toString();
if (!TextUtils.isEmpty(deviceId)
&& !TextUtils.isEmpty(aPIUrl)
&& !TextUtils.isEmpty(pushUrl)
&& !TextUtils.isEmpty(appId)
&& !TextUtils.isEmpty(appKey)
&& !TextUtils.isEmpty(timeOut)) {
NetConfigBean bean = new NetConfigBean();
bean.osVersioin = mDataBean.osVersioin;
bean.deviceId = deviceId;
bean.aipServerUrl = aPIUrl;
bean.pushServerUrl = pushUrl;
bean.appId = appId;
bean.appKey = appKey;
bean.timeOut = Long.valueOf(timeOut);
bean.updateTime = mDataBean.updateTime;
bean.sel = mDataBean.sel;
bean.netType = mDataBean.netType;
dismiss();
if (mOnSuccessListener != null) {
mOnSuccessListener.onSuccess(bean);
}
} else {
Toast.makeText(getContext(), "内容不能为空", Toast.LENGTH_SHORT).show();
}
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
public interface OnSuccessListener {
void onSuccess(NetConfigBean bean);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
@Override
public void onStart() {
super.onStart();
getDialog().setCanceledOnTouchOutside(false);
Window win = getDialog().getWindow();
win.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
WindowManager.LayoutParams params = win.getAttributes();
params.gravity = Gravity.CENTER;
// 使用ViewGroup.LayoutParams,以便Dialog 宽度充满整个屏幕
params.width = (int) (dm.widthPixels * 0.8);
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
// params.height = (int) (dm.heightPixels * 0.3);
win.setAttributes(params);
}
}
二、layout布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_item_bg"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="10dp">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="编辑"
android:textColor="@color/black"
android:textSize="18dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/etDeviceId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="DeviceId"
android:textSize="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTitle" />
<EditText
android:id="@+id/etAPIUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="APIUrl"
android:textSize="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/etDeviceId" />
<EditText
android:id="@+id/etPushUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="PushUrl"
android:textSize="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/etAPIUrl" />
<EditText
android:id="@+id/etAppId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="AppId"
android:textSize="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/etPushUrl" />
<EditText
android:id="@+id/etAppKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="AppKey"
android:textSize="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/etAppId" />
<EditText
android:id="@+id/etTimeOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="TimeOut(ms)"
android:inputType="number"
android:textSize="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/etAppKey" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginTop="20dp"
android:background="@color/theme"
android:text="修改"
android:textColor="@color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/guideline"
app:layout_constraintTop_toBottomOf="@id/etTimeOut" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="@color/color_grey"
android:text="取消"
app:layout_constraintLeft_toRightOf="@id/guideline"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/btnSubmit" />
</androidx.constraintlayout.widget.ConstraintLayout>
三、资源文件
shape_item_bg:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:radius="8dp" />
</shape>
colors:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="black_aa">#aa000000</color>
<color name="white">#FFFFFFFF</color>
<color name="theme">#1676FE</color>
<color name="darkTheme">#0F5ECF</color>
<color name="color_1676FE">#1676FE</color>
<color name="color_grey">#F5F5F5</color>
<color name="transparent">#00000000</color>
</resources>
四、调用
EditDialogFragment.newInstance(NetConfigBean.NetConfigDao2Bean(dao), new EditDialogFragment.OnSuccessListener() {
@Override
public void onSuccess(NetConfigBean bean) {
if (settingModel.updateNetConfig(bean)) {
Toast.makeText(mContext, "修改成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "修改失败", Toast.LENGTH_SHORT).show();
}
}
}).show(((AppCompatActivity) mContext).getSupportFragmentManager(), "editDialog");