简体   繁体   English

Android:getText 和 setText 无法使用 onKeyListener 处理 EditText

[英]Android: getText and setText not working on EditText with onKeyListener

I'm working on an Android application and have a view with 3 EditTexts.我正在开发一个 Android 应用程序并查看 3 个 EditTexts。 What I'm trying to achieve is that on a numeric key being pressed in the last one should call the okKey event for that activity.我想要实现的是,在最后一个按下的数字键上应该调用该活动的 okKey 事件。 The problem is that for the EditText on which I set the onKeyListener, neither getText nor setText methods work.问题是对于我设置 onKeyListener 的 EditText,getText 和 setText 方法都不起作用。 Allow me to explain:请允许我解释一下:

Suppose I initialize an empty string假设我初始化一个空字符串

String string1 = "";

In my onCreate method, I set the onKeyListener on one of the EditTexts在我的 onCreate 方法中,我在其中一个 EditTexts 上设置了 onKeyListener

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    findViewById(R.id.EditText3).setOnKeyListener(this);
    ...
}

Now, if I write the following code in the overriding okKey method现在,如果我在覆盖 okKey 方法中编写以下代码

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
        string1 += ((EditText) findViewById(R.id.EditText1)).getText();
        string1 += ((EditText) findViewById(R.id.EditText2)).getText();
        string1 += ((EditText) findViewById(R.id.EditText3)).getText();

        ((EditText) findViewById(R.id.EditText1)).setText("");
        ((EditText) findViewById(R.id.EditText2)).setText("");
        ((EditText) findViewById(R.id.EditText3)).setText("");
    }
return false;
}

Only EditText1 and EditText2 get cleared and the text in EditText3 does not get concatenated to string1.只有 EditText1 和 EditText2 被清除,并且 EditText3 中的文本不会连接到 string1。 If I replace the condition of the If statement with如果我将 If 语句的条件替换为

if (keyCode == KeyEvent.KEYCODE_ENTER)

then on pressing Enter key (set as Next), it works as expected.然后按 Enter 键(设置为 Next),它按预期工作。 I tried to add the setText("") in the onResume method of the activity too but with no difference in the result.我也尝试在活动的 onResume 方法中添加 setText("") 但结果没有区别。 I appreciate the time you took to read my question!感谢您花时间阅读我的问题!

Edited: Added the "return false;"编辑:添加了“return false;” at the end of onKey method, I had it in my work but missed putting it in this question, was reminded by Chris's answer.在 onKey 方法的最后,我把它放在了我的工作中,但错过了把它放在这个问题中,克里斯的回答提醒了我。

When your setting the OnKeyListener your EditText3, your replacing what happens when you hit a key with that.当您将 OnKeyListener 设置为您的 EditText3 时,您将替换当您按下某个键时发生的情况。 Try return false;尝试return false; at the end of the onKey.在 onKey 的末尾。 This should pass the value to the proper location.这应该将值传递到正确的位置。

http://developer.android.com/reference/android/view/View.OnKeyListener.html http://developer.android.com/reference/android/view/View.OnKeyListener.html

Returns退货

True if the listener has consumed the event, false otherwise.如果侦听器已经消费了事件,则为 true,否则为 false。

In addition to Chris' answer, a check should be added to insure that event.getAction() is equal to KeyEvent.ACTION_UP;除了 Chris 的回答之外,还应添加检查以确保 event.getAction() 等于 KeyEvent.ACTION_UP; onKey gets called twice, and if getAction() is ACTION_DOWN (first time the method is called), the character won't yet have been added to the CharSequence returned by getText(). onKey 被调用两次,如果 getAction() 为 ACTION_DOWN(第一次调用该方法),则该字符尚未添加到 getText() 返回的 CharSequence。

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

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