package com.gumptech.sdk.demo; import android.app.AlertDialog; import android.app.Dialog; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.TextInputLayout; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; /** * A simple {@link Fragment} subclass. */ public class PaymentInfoSubmitFragment extends DialogFragment { interface OnSubmitListener { void onSubmit(Bundle payInfo); } private OnSubmitListener listener; TextInputLayout etProduct, etAmount, etCurrency, etServerId, etRoleId, etExtra; public PaymentInfoSubmitFragment() { } public static PaymentInfoSubmitFragment newInstance(OnSubmitListener listener) { PaymentInfoSubmitFragment fragment = new PaymentInfoSubmitFragment(); fragment.listener = listener; return fragment; } @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_payment, null, false); etProduct = view.findViewById(R.id.product); etAmount = view.findViewById(R.id.amount); etCurrency = view.findViewById(R.id.currency); etServerId = view.findViewById(R.id.server_id); etRoleId = view.findViewById(R.id.role_id); etExtra = view.findViewById(R.id.extra); etProduct.getEditText().addTextChangedListener(new EditTextWatcher(etProduct)); etAmount.getEditText().addTextChangedListener(new EditTextWatcher(etAmount)); etCurrency.getEditText().addTextChangedListener(new EditTextWatcher(etCurrency)); etServerId.getEditText().addTextChangedListener(new EditTextWatcher(etServerId)); etRoleId.getEditText().addTextChangedListener(new EditTextWatcher(etRoleId)); etExtra.getEditText().addTextChangedListener(new EditTextWatcher(etExtra)); //设置默认值 etProduct.getEditText().setText("EV1.test.abc"); etAmount.getEditText().setText("0.99"); etCurrency.getEditText().setText("USD"); etServerId.getEditText().setText("100"); etRoleId.getEditText().setText("41080"); view.findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String product = etProduct.getEditText().getText().toString(); String currency = etCurrency.getEditText().getText().toString(); String serverId = etServerId.getEditText().getText().toString(); String roleId = etRoleId.getEditText().getText().toString(); String extra = etExtra.getEditText().getText().toString(); String amount = etAmount.getEditText().getText().toString(); if (checkInput(product, amount, currency, serverId, roleId, extra)) { Bundle payInfo = new Bundle(); payInfo.putString("product", product); payInfo.putFloat("amount", Float.valueOf(amount)); payInfo.putString("extraInfo", extra); payInfo.putString("serverId", serverId); payInfo.putString("roleId", roleId); payInfo.putString("currency", currency); if (listener != null) { listener.onSubmit(payInfo); } dismiss(); } } }); AlertDialog dialog = builder.setView(view).create(); return dialog; } private boolean checkInput(String product, String amount, String currency, String serverId, String roleId, String extra) { if (TextUtils.isEmpty(product)) { etProduct.setError("product is empty"); return false; } if (TextUtils.isEmpty(currency)) { etCurrency.setError("currency is empty"); return false; } if (TextUtils.isEmpty(serverId)) { etServerId.setError("serverId is empty"); return false; } if (TextUtils.isEmpty(roleId)) { etRoleId.setError("roleId is empty"); return false; } if (TextUtils.isEmpty(extra)) { etExtra.setError("extra is empty"); return false; } if (TextUtils.isEmpty(amount)) { etAmount.setError("amount is empty"); return false; } return true; } class EditTextWatcher implements TextWatcher { private TextInputLayout editText; public EditTextWatcher(TextInputLayout editText) { this.editText = editText; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { editText.setError(null); } @Override public void afterTextChanged(Editable s) { } } }