EditText onClick not shows Virtual Keyboard

删除回忆录丶 提交于 2019-12-19 03:41:23

问题


If i click on my EditText, the virtual keyboard simple not shows up. The cursor is shown, but no keyboard to type on.

I even tried it with manually open but just no works.

Here is my code:

public class CreateNote extends Activity {
EditText titleEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createnote);
    titleEdit = (EditText) findViewById(R.id.titleEdit);
    titleEdit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            InputMethodManager imm = (InputMethodManager) CreateNote.this
                    .getSystemService(Service.INPUT_METHOD_SERVICE);
            imm.showSoftInput(titleEdit, 0);
        }
    });
    }
   }

Snippet of Layout:

 <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#989898" >

    <EditText
        android:id="@+id/titleEdit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edittextdrawale"
        android:ems="10"
        android:textColor="#fff"
        android:textColorHint="#fff" >

        <requestFocus />
    </EditText>

</FrameLayout>

What could be the reason of playing hide and seek of my virtual keyboard ? I test on real device, not on emulator.


回答1:


Try with this, it worked for me.

EditText etHorseName = (EditText) getView().findViewById(R.id.horseName);
etHorseName.clearFocus();

in onCreate() or where you want.




回答2:


Late answer but here is how to solve it without adding code, just remove this from your XML:

<requestFocus />

No idea why the keyboard does not show up when this is set... It does show up however if you first loose the focus and then click on the edit text. I had the problem on Android 2.3.6 but it worked on 4.1.2, so maybe it was an early bug.




回答3:


It is just a default behavior , you not suppose to do it manually, remove below part from your code.

titleEdit.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        InputMethodManager imm = (InputMethodManager) CreateNote.this
                .getSystemService(Service.INPUT_METHOD_SERVICE);
        imm.showSoftInput(titleEdit, 0);
    }
});



回答4:


Try to hide and show the keyboard with this code:

InputMethodManager imm = (InputMethodManager) this.getSystemService(Service.INPUT_METHOD_SERVICE);
// To show keyboard
imm.showSoftInput(titleEdit, 0);
// To hide keyboard
imm.hideSoftInputFromWindow(titleEdit.getWindowToken(), 0);  


来源:https://stackoverflow.com/questions/14810959/edittext-onclick-not-shows-virtual-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!