Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space Key event behaviour on Numeric Input field?

I created two EditText with "android:inputType="number" property.

here I am using hardware Keyboard, so when I perform Space Key event on textField, focus control directly shift from editText view to some other random view of screen. In normal text field type it took it as an another character, that's fine.

Any one have idea how can use Space key event to retain focus on same field.

like image 963
CoDe Avatar asked Jul 30 '13 11:07

CoDe


3 Answers

Using editText.setInputType(InputType.TYPE_CLASS_NUMBER) may probably solve your problem.

like image 192
Ashish John Avatar answered Nov 09 '22 00:11

Ashish John


Change/Add your EditText Property with android:imeOptions="actionNext" like as follows

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

Or with android:imeOptions="actionNone" for default behavior of EditText

<EditText
    android:id="@+id/edit_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:imeOptions="actionNone"
    android:inputType="text" />
like image 24
SachinS Avatar answered Nov 09 '22 00:11

SachinS


So it looks like your problem doesn't have to do with the 'input' type of your EditText, but rather the key press events coming from the keyboard....

So 'hopefully' this should fix your problem for you (by 'skipping over' the 'next' event from the 'space' button being pressed.)

 // 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;
        }
    });

This is likely what is happening (hard to tell without your XML)

So I found something that should help you figure out the pattern + possibly lead to solving it... from https://stackoverflow.com/a/17990096

if there is a android:nextFocus.... setting in your XML (or equivalent in code) and/or the physical keyboard 'space' is also signalling IME_ACTION_NEXT (or another IME action similar to it)


If the EditorInfo.IME_ACTION_NEXT isn't the IME action that is causing your problems, then you can try this... to determine WHAT is.

from: https://stackoverflow.com/a/4171427

if you have PHYSICAL keyboard you can use to detect keyDown events and handle them appropriately,

@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);
}

BUT... if you have software keyboard You need to use addTextChangedListener/TextWatcher Because the physical key press is 'eaten' by the EditText (from what I saw in another post, but seems to be correct from my testing.)

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) 
    {
    }
);

You can override what the EditText does when a 'space' is entered.

This seems pretty easy when in 'software keyboard', but physical keyboard seems to be a bit more difficult.

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

Similar to this question (but 'space' instead of 'enter') Android - Handle "Enter" in an EditText


This could help you determine the 'pattern' of the focus being changed. if it is random or not (most likely -not- random, but possibly 'interesting' to look into)

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)
like image 2
mawalker Avatar answered Nov 09 '22 00:11

mawalker