错误与我调用setText()方法和递归调用TextWatcher有关。为了解决这个问题,我正在尝试删除观察者并在设置文本后再次添加它。我需要TextWatcher激活才能使我的EditText错误起作用或mPassword edittext。如果我使用以下代码删除并添加TextWatcher:
mPassword.removeTextChangedListener(new InputValidator(mPassword));
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
mPassword.setText(phone);
flag = false;
mPassword.addTextChangedListener(new InputValidator(mPassword));
它仍然在我身上崩溃。
我做错了什么?
以下是代码:
import java.util.regex.Pattern;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText mUsername;
private EditText mPassword;
Editable s;
private Drawable error_indicator;
String phone;
private boolean flag = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting custom drawable instead of red error indicator,
//error_indicator = getResources().getDrawable(R.drawable.ic_launcher);
int left = 0;
int top = 0;
//int right = error_indicator.getIntrinsicHeight();
//int bottom = error_indicator.getIntrinsicWidth();
phone = "";
mUsername = (EditText) findViewById(R.id.etUsername);
mPassword = (EditText) findViewById(R.id.etPassword);
// Called when user type in EditText
mUsername.addTextChangedListener(new InputValidator(mUsername));
mPassword.addTextChangedListener(new InputValidator(mPassword));
}
private class InputValidator implements TextWatcher {
private EditText et;
private InputValidator(EditText editText) {
this.et = editText;
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Toast.makeText(getApplicationContext(), "In onTextChanged() Method", Toast.LENGTH_SHORT).show();
if (s.length() != 0 && flag) {
switch (et.getId()) {
case R.id.etUsername: {
if (!Pattern.matches("(^[A-Za-z][\\w-(.|\\n)]+@[A-Za-z][\\w-(.|\\n)]+(.|\\n)(com|net|org|gov|mil|biz|info|mobi|name|in|aero|jobs|museum))$", s )) {
et.setError("please enter a valid email address");
}
}
break;
case R.id.etPassword: {
//doWork();
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
if (!Pattern.matches("\\([1-9]\\d{2}\\)[0-9]{3}-\\d{4}$", s)) {
et.setError("please enter a valid 10 digit phone number");
}
mPassword.removeTextChangedListener(new InputValidator(mPassword));
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
mPassword.setText(phone);
flag = false;
mPassword.addTextChangedListener(new InputValidator(mPassword));
break;
}
}
}
else {
flag = true;
}
}
}
public String formatPhoneNumber(String phoneNumber) {
final StringBuffer sbPhone = new StringBuffer(phoneNumber);
if (sbPhone.length() == 9) {
if (sbPhone.indexOf("-") == -1) {
sbPhone.insert(8, "-");
}
Toast.makeText(getApplicationContext(), "In formatPhoneNumber() Method - 9", Toast.LENGTH_SHORT).show();
}
else if (sbPhone.length() == 8) {
if (sbPhone.indexOf("-") != -1) {
sbPhone.delete(sbPhone.indexOf("-"), sbPhone.indexOf("-"));
}
Toast.makeText(getApplicationContext(), "In formatPhoneNumber() Method - 8", Toast.LENGTH_SHORT).show();
}
else if (sbPhone.length() == 5) {
if (sbPhone.indexOf("(") != -1) {
sbPhone.delete(sbPhone.indexOf("("), sbPhone.indexOf("(")+1);
}
if (sbPhone.indexOf("(") != -1) {
sbPhone.delete(sbPhone.indexOf(")"), sbPhone.indexOf(")"));
}
Toast.makeText(getApplicationContext(), "In formatPhoneNumber() Method - 5", Toast.LENGTH_SHORT).show();
}
else if (sbPhone.length() == 3) {
if (sbPhone.indexOf("(") == -1) {
sbPhone.insert(0, "(");
}
if (sbPhone.indexOf(")") == -1 && sbPhone.indexOf("(") != -1) {
sbPhone.append(")");
}
Toast.makeText(getApplicationContext(), "In formatPhoneNumber() Method - 3", Toast.LENGTH_SHORT).show();
}
return sbPhone.toString();
}
}
错误:
03-18 11:59:51.910: E/AndroidRuntime(24049): FATAL EXCEPTION: main
03-18 11:59:51.910: E/AndroidRuntime(24049): java.lang.StackOverflowError
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.text.DynamicLayout.reflow(DynamicLayout.java:324)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.text.DynamicLayout.<init>(DynamicLayout.java:174)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.makeSingleLayout(TextView.java:7106)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.makeNewLayout(TextView.java:6955)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.checkForRelayout(TextView.java:7551)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4410)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.se
感谢任何帮助。
答案 0 :(得分:1)
在删除或添加时,您正在创建两个不同的侦听器对象。
mPassword.removeTextChangedListener(new InputValidator(mPassword));
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
mPassword.setText(phone);
flag = false;
mPassword.addTextChangedListener(new InputValidator(mPassword));
这一定是
将成员添加为
private InputValidator mPasswordTextWatcher = null;
private InputValidator mUserNameTextWatcher = null;
onCreate()
中的
mPasswordTextWatcher = new InputValidator(mPassword);
mUserNameTextWatcher = new InputValidator(mUsername);
// Called when user type in EditText
mUsername.addTextChangedListener(mUserNameTextWatcher);
mPassword.addTextChangedListener(mPasswordTextWatcher);
现在就这样做
mPassword.removeTextChangedListener(mPasswordTextWatcher);
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
mPassword.setText(phone);
flag = false;
mPassword.addTextChangedListener(mPasswordTextWatcher);
对用户名EditText执行相同操作。