简体   繁体   English

e.preventdefault()等效于替换Java(Android)中的键入文本

[英]e.preventdefault() equivalent to replace typed text in java (Android)

I have a function in jQuery/javascript, to type replace any typed text in an input by a specific text : 我在jQuery / javascript中有一个函数,用于将输入中的所有键入文本替换为特定文本:

$('input').on('keydown', function(e){
e.preventDefault();
count++;
if (count == "1") { $("input").val("y");}
else if (count == "2") { $("input").val("ye");}
else if (count == "3") { $("input").val("yes");}
else {return false;}

(replace anything typed by 'yes' and does nothing after the user types more letters) (替换由“是”键入的任何内容,并且在用户键入更多字母后不执行任何操作)

I would like to implement the same function in an Android project on a text input. 我想在一个文本输入的Android项目中实现相同的功能。 What would be the best way? 最好的方法是什么? I don't want to do it in a webview but in native android. 我不想在网络视图中执行此操作,而是在本地android中执行此操作。

Thank you 谢谢

You should be able to use a TextWatcher to do what you want. 您应该可以使用TextWatcher来执行所需的操作。

Just do 做就是了

editText.addTextChangedListener(textWatcher);

and it should work for you. 它应该为您工作。 You'd have to extend the TextWatcher class yourself to do this. TextWatcher您必须自己扩展TextWatcher类。

You'd probably want to put your logic into the afterTextChanged method of your custom TextWatcher . 您可能希望将逻辑放入自定义TextWatcherafterTextChanged方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM