Android:从使用addTextChangedListener更改为使用setOnKeyListener来处理退格键。

huangapple go评论76阅读模式
英文:

Android:Changing from addTextChangedListener to setOnKeyListener to use Backspace

问题

这是我的现有代码:

txt.addTextChangedListener(new TextWatcher(){

    @Override
    public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
    {
        // TODO: Implement this method
    }

    @Override
    public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
    {
        Mymethod1;   //将输入的文本显示在TextView中
        Mymethod2;   //一个复杂的方法,根据一些条件在EditText中插入一些字符串,如果满足条件,还会移动文本光标(条件还取决于先前输入的文本)
    }

    @Override
    public void afterTextChanged(Editable p1)
    {
        // TODO: Implement this method
    }
});

如果我描述Mymethod1,它只是获取输入文本,稍微修改一下,然后在TextView中显示出来。
而Mymethod2会在编辑文本中插入一些字符串,并根据条件更改文本光标的位置,例如:
RandomText_{}MoreRandomText_<cursor is here> 会被更改为 RandomText_{}MoreRandomText_{cursor is here>},所以你可以看到,如果下划线后面没有紧跟着一个花括号,它会插入它们并将光标放在其中。
另外,我让我的方法与p1、p2、p3、p4参数无关。

我能够正确实现它,它也可以正常工作,但是当我们使用 Backspace 键删除编辑文本中的文本时,会出现问题,当删除过程中出现 _{} 时,在删除 {} 后,我的方法2 开始生效。并且由于光标仍然位于 {} 中,它会生成类似这样的内容:TextNotBeingDeleted_{<cursor>}}}}}}}}}}}}},越多的退格键-->越多的 } 符号。

所以我决定使用如下的 setOnKeyListener:

txt.setOnKeyListener(new OnKeyListener(){

    @Override
    public boolean onKey(View p1, int p2, KeyEvent p3)
    {
        if(keyCode == KeyEvent.KEYCODE_DEL){
            Mymethod1;
        }
        else{
            Mymethod1;
            Mymethod2;
        }
        return false;
    }
});

但是这并不能正常工作,只有在使用退格键时才会执行 Mymethod1,使用其他键时不会发生任何事情(就好像方法1、2不存在一样)。
请指导我如何解决这个问题,我是否应该继续使用 addTextChangedListener 并相应地进行更改(以及如何进行更改),或者是否应该更正我的 setOnKeyListener(以及如何更正)。

谢谢

英文:

This is my present code:

		txt.addTextChangedListener(new TextWatcher(){

				@Override
				public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
				{
					// TODO: Implement this method
				}

				@Override
				public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
				{
					Mymethod1;   //displays the text entered into a TextView
                    Mymethod2;   //a complicated method which inserts some strings, moves the text-cursor if some conditions are satisfied(conditions also depend on the previous text entered 
				}

				@Override
				public void afterTextChanged(Editable p1)
				{
					// TODO: Implement this method
				}
			});
		
        

If I describe Mymethod1, it simply gets the input text, changes it a bit and displays it on a TextView
Whereas Mymethod2, inserts some strings in the edittext and changes the postion of text cursor using conditions, for example:
RandomText_{}MoreRandomText_<cursor is here> is changed to RandomText_{}MoreRandomText_{cursor is here>} so as you see, if an underscore is not followed by a curly bracket, it inserts them and places the cursor inside it.
Also, i made my methods argument independent from the p1,p2,p3,p4 parameters.

I am able to implement this correctly and it also works flawlessly, but a problem occurs when we use Backspace to delete the text in the edit text, in the process of deleting, when the _{} comes, after {} getting deleted my method2 starts getting applied. and since the cursor is still placed inside the {}, it generates something like this TextNotBeingDeleted_{<cursor>}}}}}}}}}}}}} more backspaces--> more }.

So i decided to use setOnKeyListener like this:

		txt.setOnKeyListener(new OnKeyListener(){

				@Override
				public boolean onKey(View p1, int p2, KeyEvent p3)
				{
					if(keyCode == KeyEvent.KEYCODE_DEL){
                    Mymethod1;
                    }
                    else{
                    Mymethod1;
                    Mymethod2;
                    }
					return false;
				}
			});

but this isn't working properly, only Mymethod1 gets executed when backspace is used, nothing happens when other keys are used(works like method1,2 doesnt exist).
Please guide me on how to solve this problem, if i should continue using addTextChangedListener and change it appropriately(and how)or if I should correct my setOnKetListener(and how).

Thanks

答案1

得分: 0

所以我找到了一些东西,虽然不是最好的,但我的工作完成了。

        txt.addTextChangedListener(new TextWatcher(){
                int before_length=0;
                @Override
                public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
                {
                    before_length=txt.getText().length();
                }

                @Override
                public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
                {
                    Mymethod1;   
                    if(before_length < txt.getText().length())
                    {
                    Mymethod2;    
                    }
                }

                @Override
                public void afterTextChanged(Editable p1)
                {
                    // TODO: 实现这个方法
                }
            });

beforeTextChanged中的before_length给我们提供了在按下退格键之前字符串的长度。将其与按下退格键后的长度进行比较,我可以阻止调用方法2。

我会感激任何编辑、改进或想法。

英文:

So I figured out something, its not the best, but my work is done.

        txt.addTextChangedListener(new TextWatcher(){
                int before_length=0;
                @Override
                public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
                {
                    before_length=txt.getText().length();
                }

                @Override
                public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
                {
                    Mymethod1;   
                    if(before_length&lt;txt.getText().length())
                    {
                    Mymethod2;    
                    }
                }

                @Override
                public void afterTextChanged(Editable p1)
                {
                    // TODO: Implement this method
                }
            });

before_length in beforeTextChanged gives us the length of string before backspace was pushed. Comparing it with the length after backspace is pushed, i can prevent method2 from getting called.

I would appreciate any edits, improvements or ideas.

huangapple
  • 本文由 发表于 2020年8月31日 18:02:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63668700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定