简体   繁体   中英

EditText doesn't show input keyboard on requestfocus

I'm facing a strange problem. When I add an edittext using the following piece of code:

edit = new EditText(context);
edit.setInputType(InputType.TYPE_CLASS_TEXT);
edit.layout(0, 0, (int)searchPane[W], (int)searchPane[H]);
edit.setBackgroundColor(Color.TRANSPARENT);
edit.setGravity(Gravity.CENTER_VERTICAL);
edit.addTextChangedListener(SearchBox.this);
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);

and draw it using edit.draw(Canvas) in the onDraw method of View . I'm calling edit.requestFocus() in onTouchMethod though it does not show the input keyboard on focus request. Any ideas how to make edittext accept text and show input keyboard?

Thanks in advance.

Your EditText isn't part of the View hierarchy as you've never added it as a child to your view group; as such it will not receive touch events (among other things) unless you implement that in your view group.

Add a call to addView(edit) and move edit.layout(0, 0, (int)searchPane[W], (int)searchPane[H]) into onLayout .

It probably won't be as simple as that, creating custom views is an advanced undertaking - are you sure what you're trying to do isn't possible using one (or combination) of the existing view layouts?

If not, you should read http://developer.android.com/training/custom-views/index.html

The only combination which worked for me :

edit.requestFocus()
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, InputMethodManager.SHOW_FORCED);

Paired with adding input mode to the activity

<activity android:name="..."
              android:windowSoftInputMode="adjustResize" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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