Android EditText keyboard not shown in snippet

I added EditTextin Fragment, but mine EditTextdoes not show the keyboard, even if I press EditText.

Here is my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <EditText
        android:id="@+id/setting_nickname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="8"
        android:gravity="right"
        android:hint="Samantha"
        android:textColorHint="@color/nav_selected"
        android:textSize="14dp"
        android:textColor="@color/nav_selected"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" >
    </EditText>

    <EditText
        android:id="@+id/setting_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="8"
        android:gravity="right"
        android:hint="Samantha"
        android:textColorHint="@color/nav_selected"
        android:textSize="14dp"
        android:textColor="@color/nav_selected"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" >
    </EditText>

</LinearLayout>

I'm trying to get a listener EditText

final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
edit_nickname.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        imm.showSoftInput(edit_nickname, 0);

        //edit_nickname.requestFocus();  --> fail
        //imm.showSoftInput(edit_nickname, InputMethodManager.SHOW_IMPLICIT);  --> fail
        //imm.showSoftInput(edit_nickname, InputMethodManager.SHOW_FORCED);  --> fail
    }
});

And this code cannot ...

final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

In addition, I am trying to add LinearLayoutto have focusableInTouchModeand focusable, but failed.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="10dp"
    android:padding="10dp"
    android:background="@color/black_transparent"
    android:focusableInTouchMode="true"
    android:focusable="true">
    <EditText .../>
    <EditText .../>
</LinearLayout>

If I use <requestFocus />what the keyboard displays, but only applies to one of EditText. How can i do And why doesn't the keyboard appear?

+4
source share
4 answers

Try executing the following code in a separate thread or handler.

 editText.postDelayed(new Runnable() {
                @Override
                public void run() {

                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if(imm != null)
                    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                }
            },1000);
0
source

,

EditText myEd= (EditText)getActivity().findViewById(R.id.myEd);
myEd.requestFocus(); 
InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(myEd, InputMethodManager.SHOW_IMPLICIT);
0

:

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.showSoftInput(edit_nickname, 0);

By implementing the ListText Touch Touch method, and you get the result as follows:

edit_nickname.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.showSoftInput(edit_nickname, 0);


            return false;
        }
    });
0
source

Try it.

private InputMethodManager mInputMethodManager;

EditText mRecordFileNameET = (EditText) findViewById(R.id.recorder_recordFileName_et);
mRecordFileNameET.setOnKeyListener(this);

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.recorder_recordFileName_tv:
        mRecordFileNameET.requestFocus();
        showSoftKeyboard(true);
        break;
    }

}

//this method is used to show and hide soft keyboard
private void showSoftKeyboard(boolean showkeyboard) {
    if (showkeyboard) {
        mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.showSoftInput(mRecordFileNameET, InputMethodManager.SHOW_IMPLICIT);
    } else {
        mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.hideSoftInputFromWindow(mRecordFileNameET.getWindowToken(), 0);
    }

}

Hope this helps ...

0
source

All Articles