onTextChanged 事件在 Android 中不起作用
onTextChanged event doesn't work in Android
我在一个应用程序中有一个 EditBox
和一个 TextView
。
我希望 TextView 在我输入时将输入到 EditBox 中的数字转换为单词 ( onTextChanged()
) 并显示它。
我制作了 class 用于将数字转换为单词并且它可以正常工作。
我搜索 onTextChanged
事件并找到了使用方法,但我遇到了问题。
代码如下:
ed.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
String num = ed.getText().toString();
String str = num.replace(",", "");
double x = Double.parseDouble(str);
String text = NumberFormat.getIntegerInstance(Locale.US).format(x);
ed.setText(text);
/*if (ed.length() != 0)
{
String numtoalph = NumToAlph.convert(x);
tv.setText( numtoalph + tv.getText().toString());
}*/
NumToAlph numToAlph = new NumToAlph();
String alph = NumToAlph.Convert(text, x);
tv.setText(alph + " ریال");
}
catch (Exception ex)
{
tv.setText(ex.toString());
}
@Override
public void afterTextChanged(Editable s) {}
});
*NumToAlph 是 class
当我将它安装在我的 phone 上并尝试在我按下第一个按钮时输入时应用程序冻结了,一分钟后我收到一条通知说
this app is using too much CPU
我测试了代码,我是说这个
try {
String num = ed.getText().toString();
String str = num.replace(",", "");
double x = Double.parseDouble(str);
String text = NumberFormat.getIntegerInstance(Locale.US).format(x);
ed.setText(text);
/*
if (ed.length() != 0)
{
String numtoalph = NumToAlph.convert(x);
tv.setText( numtoalph + tv.getText().toString());
}
*/
NumToAlph numToAlph = new NumToAlph();
String alph = NumToAlph.Convert(text, x);
tv.setText(alph + " ریال");
}
catch (Exception ex)
{
tv.setText(ex.toString());
}
在按钮单击事件中,它运行迅速且正确。
有人知道这个问题吗?
我之前在 C# 中为 windows 编写了这个应用程序,并将其用于 textChanged 事件没有问题:
decimal Number;
if (decimal.TryParse(textBox1.Text, out Number))
{
textBox1.Text = string.Format("{0:N0}", Number);
textBox1.SelectionStart = textBox1.Text.Length;
}
try
{
NumToAlph dd = new NumToAlph();
string x = textBox1.Text.Replace(",", "").ToString();
textBox2.Text = dd.num2str(x) + " ریال";
}
catch (Exception ex)
{
MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
问题是您正在侦听 EditText 中的更改。
ed.addTextChangedListener(new TextWatcher() {
然后在该函数内,您正在更改 EditText 内容...
ed.setText(text);
您创建了一个无限循环。您需要从 TextWatcher
接口函数中删除 ed.setText
函数调用。
ed.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
String num = ed.getText().toString();
String str = num.replace(",", "");
double x = Double.parseDouble(str);
String text = NumberFormat.getIntegerInstance(Locale.US).format(x);
ed.setText(text);
/*if (ed.length() != 0)
{
String numtoalph = NumToAlph.convert(x);
tv.setText( numtoalph + tv.getText().toString());
}*/
NumToAlph numToAlph = new NumToAlph();
String alph = NumToAlph.Convert(text, x);
tv.setText(alph + " ریال");
}
catch (Exception ex)
{
tv.setText(ex.toString());
}
}
});
也许这有帮助:
if (!isChangedProgramatically) {
String num = editText.getText().toString();
if(!num.isEmpty()) {
String str = num.replace(",", "");
double x = Double.parseDouble(str);
String text = NumberFormat.getIntegerInstance(Locale.US).format(x);
isChangedProgramatically = true;
editText.setText(text);
}
} else {
editText.setSelection(editText.getText().length());
isChangedProgramatically = false;
}
isChangedProgramatically 是您的 Activity 的私有布尔值(默认为 false)。
我在一个应用程序中有一个 EditBox
和一个 TextView
。
我希望 TextView 在我输入时将输入到 EditBox 中的数字转换为单词 ( onTextChanged()
) 并显示它。
我制作了 class 用于将数字转换为单词并且它可以正常工作。
我搜索 onTextChanged
事件并找到了使用方法,但我遇到了问题。
代码如下:
ed.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
String num = ed.getText().toString();
String str = num.replace(",", "");
double x = Double.parseDouble(str);
String text = NumberFormat.getIntegerInstance(Locale.US).format(x);
ed.setText(text);
/*if (ed.length() != 0)
{
String numtoalph = NumToAlph.convert(x);
tv.setText( numtoalph + tv.getText().toString());
}*/
NumToAlph numToAlph = new NumToAlph();
String alph = NumToAlph.Convert(text, x);
tv.setText(alph + " ریال");
}
catch (Exception ex)
{
tv.setText(ex.toString());
}
@Override
public void afterTextChanged(Editable s) {}
});
*NumToAlph 是 class
当我将它安装在我的 phone 上并尝试在我按下第一个按钮时输入时应用程序冻结了,一分钟后我收到一条通知说
this app is using too much CPU
我测试了代码,我是说这个
try {
String num = ed.getText().toString();
String str = num.replace(",", "");
double x = Double.parseDouble(str);
String text = NumberFormat.getIntegerInstance(Locale.US).format(x);
ed.setText(text);
/*
if (ed.length() != 0)
{
String numtoalph = NumToAlph.convert(x);
tv.setText( numtoalph + tv.getText().toString());
}
*/
NumToAlph numToAlph = new NumToAlph();
String alph = NumToAlph.Convert(text, x);
tv.setText(alph + " ریال");
}
catch (Exception ex)
{
tv.setText(ex.toString());
}
在按钮单击事件中,它运行迅速且正确。 有人知道这个问题吗?
我之前在 C# 中为 windows 编写了这个应用程序,并将其用于 textChanged 事件没有问题:
decimal Number;
if (decimal.TryParse(textBox1.Text, out Number))
{
textBox1.Text = string.Format("{0:N0}", Number);
textBox1.SelectionStart = textBox1.Text.Length;
}
try
{
NumToAlph dd = new NumToAlph();
string x = textBox1.Text.Replace(",", "").ToString();
textBox2.Text = dd.num2str(x) + " ریال";
}
catch (Exception ex)
{
MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
问题是您正在侦听 EditText 中的更改。
ed.addTextChangedListener(new TextWatcher() {
然后在该函数内,您正在更改 EditText 内容...
ed.setText(text);
您创建了一个无限循环。您需要从 TextWatcher
接口函数中删除 ed.setText
函数调用。
ed.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
String num = ed.getText().toString();
String str = num.replace(",", "");
double x = Double.parseDouble(str);
String text = NumberFormat.getIntegerInstance(Locale.US).format(x);
ed.setText(text);
/*if (ed.length() != 0)
{
String numtoalph = NumToAlph.convert(x);
tv.setText( numtoalph + tv.getText().toString());
}*/
NumToAlph numToAlph = new NumToAlph();
String alph = NumToAlph.Convert(text, x);
tv.setText(alph + " ریال");
}
catch (Exception ex)
{
tv.setText(ex.toString());
}
}
});
也许这有帮助:
if (!isChangedProgramatically) {
String num = editText.getText().toString();
if(!num.isEmpty()) {
String str = num.replace(",", "");
double x = Double.parseDouble(str);
String text = NumberFormat.getIntegerInstance(Locale.US).format(x);
isChangedProgramatically = true;
editText.setText(text);
}
} else {
editText.setSelection(editText.getText().length());
isChangedProgramatically = false;
}
isChangedProgramatically 是您的 Activity 的私有布尔值(默认为 false)。