17

"android:inputType="number"我创建了两个带有属性的EditText 。

这里我使用的是硬件键盘,所以当我在 textField 上执行 Space Key 事件时,焦点控制直接从 editText 视图转移到屏幕的其他一些随机视图。在普通文本字段类型中,它将它作为另一个字符,这很好。

任何人都知道如何使用 Space 键事件来保持对同一领域的关注。

4

3 回答 3

3

使用 editText.setInputType(InputType.TYPE_CLASS_NUMBER) 可能会解决您的问题。

于 2015-12-22T11:14:40.447 回答
2

因此,看起来您的问题与 EditText 的“输入”类型无关,而与来自键盘的按键事件有关....

所以“希望”这应该为您解决问题(通过“跳过”按下“空格”按钮的“下一个”事件。)

 // do for both EditText(s)
 editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        log.d("main","KeyPress:"+ actionID);
        if (event != null ) { log.d("main","KeyPress:" + event.getKeyCode() ); }
            if ( actionId == EditorInfo.IME_ACTION_NEXT) {
                // do 'nothing' or 'add a space' if you want that
                return true;
            }
            return false;
        }
    });

这可能是正在发生的事情(没有您的 XML 很难说)

所以我发现了一些可以帮助你找出模式+可能导致解决它的东西......来自https://stackoverflow.com/a/17990096

如果您的 XML 中有android:nextFocus....设置(或代码中的等效项)和/或物理键盘“空格”也发出信号IME_ACTION_NEXT(或其他类似的 IME 操作)


如果这EditorInfo.IME_ACTION_NEXT不是导致您的问题的 IME 操作,那么您可以尝试这个...以确定是什么。

来自:https ://stackoverflow.com/a/4171427

如果您有物理键盘,您可以使用它来检测 keyDown 事件并适当地处理它们,

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {
    log.d("main","KeyPress:" + keyCode);
    if (event != null ) { log.d("main","KeyPress:" + event.getKeyCode() ); }
   // if you want to 'handle' the keyPess here, best to use switch like below
   // switch (keyCode) {
   //     case KeyEvent.KEYCODE_A:
    //    {
    //        //your Action code
    //        return true;
    //    }
   /// }
    return super.onKeyDown(keyCode, event);
}

但是......如果你有软件键盘你需要使用 addTextChangedListener/TextWatcher 因为物理按键被 EditText“吃掉”(从我在另一篇文章中看到的,但从我的测试来看似乎是正确的。)

mMyEditText.addTextChangedListener(new TextWatcher()
{
    public void afterTextChanged(Editable s) 
    {
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) 
    {
        /*This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after. It is an error to attempt to make changes to s from this callback.*/ 
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {
    }
);

您可以覆盖 EditText 在输入“空格”时所做的事情。

在“软件键盘”中这似乎很容易,但物理键盘似乎有点困难。

https://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html

与此问题类似(但使用“空格”而不是“输入”)Android - 在 EditText 中处理“输入”


这可以帮助您确定正在更改的焦点的“模式”。如果它是随机的(很可能 - 不是 - 随机的,但可能是“有趣的”调查)

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

// try this to see
View.OnFocusChangeListener changeListener = new View.OnFocusChangeListener()                
    {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            log.d("YourClassName", "FocusChanged" + hasFocus)
        }
    };

 EditText1.setOnFocusChangeListener(changeListener);
 EditText2.setOnFocusChangeListener(changeListener);
 Button.setOnFocusChangeListener(changeListener);
 // ... etc... (add to views to see if there is pattern)
于 2015-12-24T00:28:30.057 回答
2

更改/添加您的 EditText 属性,android:imeOptions="actionNext"如下所示

<EditText
    android:id="@+id/edit_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:imeOptions="actionNext"
    android:inputType="text" /> 

或使用android:imeOptions="actionNone"EditText 的默认行为

<EditText
    android:id="@+id/edit_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:imeOptions="actionNone"
    android:inputType="text" />
于 2015-12-23T09:10:04.980 回答