PaymentInfoSubmitFragment.java
5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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) {
}
}
}