【问题标题】:onTextChanged event doesn't work in AndroidonTextChanged 事件在 Android 中不起作用
【发布时间】:2016-01-18 15:25:47
【问题描述】:

我在应用程序中有一个EditBox 和一个TextView。 我希望 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# 为 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);
}

【问题讨论】:

    标签: android cpu textwatcher textchanged


    【解决方案1】:

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

    ed.addTextChangedListener(new TextWatcher() {
    

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

    ed.setText(text);
    

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

    【讨论】:

    • 你知道我已经用 C# 为 windows 编写了这个应用程序,我将它用于 textchanged 事件没有问题
    • 尝试在没有这些行的情况下对其进行测试以设置文本。看看它是否仍然没有响应。
    【解决方案2】:

    也许这会有所帮助:

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

    【讨论】:

    • 那么当您在编辑框中输入时,没有任何方法可以对数字进行分组吗?
    • 当您以编程方式设置 editText 时,它将再次调用您的 Listener,并且永远如此。也许有一种方法可以告诉侦听器是否由于用户输入或您的程序而触发了更改事件。
    • 是否可以制作工具提示并在键入时显示。你知道一些资源来教这种东西吗?
    • 快速搜索发现以下stackoverflow-thread。 stackoverflow.com/a/13817777/4924595 我认为您从网站上知道的标准工具提示在 android 上并不那么花哨。
    • 您能告诉我布尔值是否有助于解决您的问题并可能对答案进行评分吗?谢谢^^
    【解决方案3】:
    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());
                }
            }
        });
    

    【讨论】:

    • *只允许数字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多