当使用imeOption = actionSend时,EditText为空时禁用发送按钮(Disabling the send button when the EditText is empty wh

编程入门 行业动态 更新时间:2024-10-28 16:30:21
当使用imeOption = actionSend时,EditText为空时禁用发送按钮(Disabling the send button when the EditText is empty when using imeOption= actionSend)

当文本为空时,我们要禁用按钮(这是如何设置肖像模式)任何想法?

编辑:我不认为这是明确的,但我可以启用/禁用我自己的按钮..但是,当使用横向模式,当键盘弹出时,屏幕被覆盖了一个Android自己的按钮的特定文本区域(因此imeOption)所以我没有启用/禁用我有的按钮问题..这是这个Android按钮,我想禁用文本区域为空时..

When the text is empty, we want to disable the button (which is how it's set on portrait mode) Any ideas?

Edit: I don't think it's clear but I can enable/disable my own button.. But when using the landscape mode, when the keyboard pops up, the screen is covered by an android specific text area with it's own button (hence the imeOption) So I don't have a problem enabling/disabling the button I have.. It's this Android button that I want to disable when the text area is empty..

最满意答案

添加一个TextChangedListener ,只要EditText的文本被更改,它就会被调用。

message.addTextChangedListener(new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) {} public void afterTextChanged(Editable s) { if (s == null || s.length() == 0) { send.setEnabled(false); message.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION); } else { send.setEnabled(true); message.setImeOptions( /* whatever you previously had */ ); } }

或者,你也可以让你的类实现TextWatcher接口,这使得代码更清洁一些。

public class MyDialogFragment implements TextWatcher { ... }

Add a TextChangedListener which will get called whenever the text inside the EditText gets changed.

message.addTextChangedListener(new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) {} public void afterTextChanged(Editable s) { if (s == null || s.length() == 0) { send.setEnabled(false); message.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION); } else { send.setEnabled(true); message.setImeOptions( /* whatever you previously had */ ); } }

Alternatively, you can also let your class implement the TextWatcher interface which makes the code a bit cleaner.

public class MyDialogFragment implements TextWatcher { ... }

更多推荐

本文发布于:2023-07-15 05:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   按钮   actionSend   EditText   imeOption

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!