6

I created a custom view:

public class MyCustomView extends LinearLayout {...}

When user touch it, I show keyboard like this:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            requestFocus();
            showKeyboard(true);
        }
        return super.onTouchEvent(event);
    }
    public void showKeyboard(boolean show) {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (show) {
          imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);
        } else {
          imm.hideSoftInputFromWindow(getWindowToken(), 0);
        }
    }

But how can I show a number keyboard, which user can only input digits, just like this for EditText?

mEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
mEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
wizardlee
  • 881
  • 8
  • 13

2 Answers2

3

You have to add an override for onCreateInputConnection

public class MyCustomView extends LinearLayout implements View.OnFocusChangeListener {

    //...

    // Make sure to call this from your constructor
    private void initialize(Context context) {
        setFocusableInTouchMode(true);
        setFocusable(true);
        setOnFocusChangeListener(this);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (hasFocus) {
            imm.showSoftInput(v, 0);
        } else {
            imm.hideSoftInputFromWindow(getWindowToken(), 0);
        }
    }

    // Here is where the magic happens
    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        outAttrs.inputType = InputType.TYPE_CLASS_NUMBER;
        outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
    }

    //...
}
Senne
  • 31
  • 4
  • Missing a return of type "InputConnection" in "onCreateInputConnection". It can be a simple "return null". – Gauss Nov 20 '17 at 00:38
0

try to add

 EditText mEditText = new EditText(mContext);
 mEditText.setInputType(InputType.TYPE_CLASS_NUMBER);

and change

imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);

to

imm.showSoftInput(mEditText, InputMethodManager.SHOW_FORCED);

i.e. rewrite your code as

if (show) {
    EditText mEditText = new EditText(mContext);
    mEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
    imm.showSoftInput(mEditText, InputMethodManager.SHOW_FORCED);
}

replace mContext is your activity context.

Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • Do you mean add an invisible EditText into my custom view, and make the EditText to be keyboard focus? – wizardlee Mar 11 '15 at 04:07
  • 1
    To show keyboard on the invisible EditText, I have to set keyboard focus to it, i.e, editText.requestFocus(), and all keyboard event will also be sent to the EditText. But I want to handle keyboard event in my custom view itself. How to do? – wizardlee Mar 11 '15 at 04:22
  • Also interested. Faced the same problem. Did you find any solution? – konunger Oct 26 '15 at 10:54