|
2 | 2 |
|
3 | 3 | import android.app.Activity; |
4 | 4 | import android.app.ProgressDialog; |
| 5 | +import android.content.Context; |
5 | 6 | import android.content.DialogInterface; |
6 | 7 | import android.content.Intent; |
7 | 8 | import android.content.SharedPreferences; |
|
12 | 13 | import android.preference.PreferenceFragment; |
13 | 14 | import android.preference.PreferenceManager; |
14 | 15 | import android.support.v7.app.AlertDialog; |
15 | | -import android.text.TextUtils; |
16 | 16 | import android.view.LayoutInflater; |
17 | 17 | import android.view.View; |
18 | 18 | import android.widget.EditText; |
@@ -116,27 +116,47 @@ public void resetUserName() { |
116 | 116 | View dialogView = inflater.inflate(R.layout.dialog_settings_your_name, null); |
117 | 117 | final EditText editInput = (EditText) dialogView.findViewById(R.id.et_dialog_settings_your_name); |
118 | 118 | editInput.setText(prefUsername.getSummary()); |
| 119 | + |
119 | 120 | final AlertDialog dialog = |
120 | 121 | new AlertDialog.Builder(getActivity()) |
121 | 122 | .setTitle(R.string.title_user_name) |
122 | 123 | .setView(dialogView) |
123 | 124 | .setNegativeButton(R.string.dialog_no, null) |
124 | | - .setPositiveButton(R.string.dialog_yes, new DialogInterface.OnClickListener() { |
125 | | - @Override |
126 | | - public void onClick(DialogInterface dialog, int which) { |
127 | | - String enteredName = editInput.getText().toString(); |
128 | | - if (TextUtils.isEmpty(enteredName) || enteredName == null || !Character.isLetterOrDigit(((String) enteredName).charAt(0))) { |
129 | | - Toast.makeText(getActivity(), R.string.valid_msg_name,Toast.LENGTH_LONG).show(); |
130 | | - } |
131 | | - else { |
132 | | - prefUsername.getEditor().putString(getString(R.string.key_user_name), enteredName).commit(); |
133 | | - prefUsername.setSummary(editInput.getText().toString()); |
134 | | - dialog.dismiss(); |
135 | | - } |
| 125 | + .setPositiveButton(R.string.dialog_yes, null).create(); |
136 | 126 |
|
137 | | - } |
138 | | - }).create(); |
139 | 127 | dialog.show(); |
| 128 | + |
| 129 | + dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { |
| 130 | + @Override |
| 131 | + public void onClick(View v) { |
| 132 | + |
| 133 | + if (validated(editInput)) { |
| 134 | + String enteredName = editInput.getText().toString(); |
| 135 | + prefUsername.getEditor().putString(getString(R.string.key_user_name), enteredName).commit(); |
| 136 | + prefUsername.setSummary(editInput.getText().toString()); |
| 137 | + dialog.dismiss(); |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + private boolean validated(EditText editInput) { |
| 145 | + if (editInput == null) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + String authorText = editInput.getText().toString().trim(); |
| 150 | + Context mContext = getActivity(); |
| 151 | + |
| 152 | + if ("".equals(authorText)) { |
| 153 | + editInput.setError(mContext.getString(R.string.valid_msg_name)); |
| 154 | + return false; |
| 155 | + } else if (!Character.isLetterOrDigit(authorText.charAt(0))) { |
| 156 | + editInput.setError(mContext.getString(R.string.title_valid)); |
| 157 | + return false; |
| 158 | + } |
| 159 | + return true; |
140 | 160 | } |
141 | 161 |
|
142 | 162 | @Override |
|
0 commit comments