gpt4 book ai didi

java - Stackoverflow EditText 小部件

转载 作者:行者123 更新时间:2023-12-01 13:15:47 24 4
gpt4 key购买 nike

该错误与我调用 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

感谢任何帮助。

最佳答案

您在删除或添加时创建了两个不同的监听器对象。

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 执行相同操作。

关于java - Stackoverflow EditText 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22487038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com