Keyboard not shown on inflated editText even after clicking on it

不想你离开。 提交于 2019-12-24 00:58:47

问题


Background

I have a form-like activity, which has some views that can be created dynamically upon pressing.

I'm using an xml for each field that is inflated upon clicking on a button.

What I need is that upon choosing to add a new item, it will get focus, scroll if needed, and show the keyboard so that the user can type things into it. The keyboard may be shown upon adding the field or when clicking on the EditText.

The problem

For some reason, on some devices (and I don't think it's even an android version issue) when inflating the new view, the editText within it can get focus but it doesn't show the keyboard, even if it has focus and the user clicks on it.

In such a case, the only way to show the keyboard is to click on another EditText.

I've tested it, and noticed that it doesn't occur when I don't use xml at all, meaning when i create the views only in code.

The question

Why does it occur? What can I do in order to fix it?

I've already tried so many possible solutions, but none work on all devices.

Sample Code

the next code has the described issue on xperia j (android 4.0.4) an galaxy mini (android 2.3.6) .

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ViewGroup container = (ViewGroup) findViewById(R.id.LinearLayout1);
    final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    findViewById(R.id.button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) {
            final View view = inflater.inflate(R.layout.field, null);
            // using the next code works when using it instead of inflating a layout:
            // final EditText editText = new EditText(MainActivity.this);
            // editText.setText("item " + container.getChildCount());
            container.addView(view);
        }
    });
}

the field layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFffffff"
    android:gravity="center_vertical" >

    <Spinner
        android:id="@+id/typeSpinner"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerVertical="true"
        android:background="@null"
        android:padding="5dp" />

    <include
        android:id="@+id/removeItemView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        layout="@layout/remove_item_view" />

    <EditText
        android:id="@+id/fieldEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/removeItemView"
        android:layout_toRightOf="@+id/typeSpinner"
        android:ems="10"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:hint="field"
        android:imeOptions="actionDone"
        android:inputType="phone"
        android:minWidth="200dp"
        android:padding="5dp"
        android:singleLine="true"
        tools:ignore="HardcodedText" >
    <requestFocus />
    </EditText>

</RelativeLayout>

the main layout file:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="click"
            tools:ignore="HardcodedText" />
    </LinearLayout>

</ScrollView>

EDIT: found a partial solution which won't show the keyboard right away, but at least it will be shown when pressing the editText:

public static void forceFocusOnView(final View view) {
    if (view == null)
        return;
    view.post(new Runnable() {
        @Override
        public void run() {
            view.clearFocus();
            view.post(new Runnable() {
                @Override
                public void run() {
                    view.requestFocus();
                }
            });
        }
    });
}

回答1:


I'm not sure in a reason of that problem. Could be some missmatches when Android sdk were rewritten in order to match some devices. So I think that the best solution to you is to show your keyboard manually and do not try to move a train, just take the path of the least resistance:

EditText et=(EditText)this.findViewById(R.id.edit_text);
et.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                 imm.showSoftInput(et, 0);<----------------

        }
    });

Here you go , man. Wish you luck.

Oh i see...

private EditText et;
private Runnable setFocus;
......
protected void onCreate(...){
.
.
.
 setFocus=new Runnable(){

                @Override
                public void run() {
                    et.requestFocus();

                }
            };
}
....
 findViewById(R.id.button).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(final View v) {
        final View view = inflater.inflate(R.layout.field, null);
      //try this:
        et=(EditText)view.findViewById(r.id.fieldEditText);
et.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
             imm.showSoftInput(et, 0);<----------------

    }
});
        container.addView(view);
        container.requestLayout();<--------------
        et.postDelayed(setFocus,300);
    }
});


来源:https://stackoverflow.com/questions/17149800/keyboard-not-shown-on-inflated-edittext-even-after-clicking-on-it

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