onTextChanged事件在Android中不起作用

时间:2016-01-18 15:25:47

标签: android cpu textwatcher textchanged

我在应用中有EditBoxTextView。 我希望TextView将我键入EditBox的数字转换为单词,因为我正在键入(onTextChanged())并显示它。 我创建了将数字转换为单词的类,它可以正常工作。 我搜索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是班级

当我在手机上安装它并尝试键入时按下第一个按钮,应用程序冻结,一分钟后我收到通知,说明

  

这个应用程序使用了太多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#中编写了这个应用程序的应用程序,并将其用于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);
}

3 个答案:

答案 0 :(得分:3)

问题在于您正在侦听EditText中的更改。

ed.addTextChangedListener(new TextWatcher() {

然后在该函数内部,您正在更改EditText内容......

ed.setText(text);

您创建了一个无限循环。您需要从ed.setText接口函数中删除TextWatcher函数调用。

答案 1 :(得分:1)

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());
            }
        }
    });

答案 2 :(得分:1)

也许这会有所帮助:

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)。

相关问题