开发者

how to hook key event

开发者 https://www.devze.com 2022-12-23 08:43 出处:网络
I\'d like to limit the text length of EditText widget, And if user type more charactes than the limited length,

I'd like to limit the text length of EditText widget, And if user type more charactes than the limited length, I want to show a kind of warning popup, however I can't show popup.

The problem is that we can't show popup while typing, Probably, many people think a way of utilizing OnK开发者_StackOverflow中文版eyListener or OnKeyDown. But, when the word is composing, nothing come into OnKeyListener or OnKeyDown, So, we can't show popup when we want to.

Is there anyone who have smart idea to solve this problem?


You should be able to remove focus from the widget, and show your message.

   InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
   imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0); 


OnKeyboardActionListener is for implementing software keyboards.

OnKeyListener and OnKeyDown do not get called, as you have discovered, when using a software keyboard. They only get called when using a hardware keyboard, which many Android devices don't even have.

I assume what you are trying to do is capture key events as they are occurring in an EditText area. Your best bet in this case, in order to handle both software keyboard input and hardware keyboard input, is to register a TextWatcher via the addTextChangedListener() method.

Note that on phones with Android 2.1 and later, such as the Nexus One, people have the option of using speech recognition to input text into your EditText instead of typing the text. When they do that you may get full words, or even full sentences, entered all at once. So you need to check the entire contents of the EditText field when there is a change to the contents.

0

精彩评论

暂无评论...
验证码 换一张
取 消