键盘甚至没有点击它后膨胀EDITTEXT显示 [英] Keyboard not shown on inflated editText even after clicking on it

查看:216
本文介绍了键盘甚至没有点击它后膨胀EDITTEXT显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我有一个表格状的活性,具有可动态地在pressing创建的一些看法。

我使用的XML因为这是在点击一个按钮充气每个字段。

我需要的是,在选择添加一个新的项目,它将获得焦点,如果需要滚动,并显示键盘,使用户可以键入的东西进去。键盘可以在添加字段或上的EditText点击时显示。

问题

由于某些原因,在某些设备上(我不认为这是即使是Android版本的问题),充气新的视图时,其中的EDITTEXT可以获得焦点,但它并不显示键盘,即使它有聚焦和用户点击它。

在这种情况下,以显示键盘的唯一方法是点击另一个的EditText

我测试过它,并注意到,当我当我创建仅在code中的视图不使用XML可言,这意味着它不会发生。

问题

为什么会出现这种问题?我可以为了做些什么来解决这个问题?

我已经尝试过所有设备上的许多可能的解决方案,但没有工作。

样品code

接下来的code对XPERIA j中的描述问题(安卓4.0.4)的星系迷你(安卓2.3.6)。

  @覆盖
保护无效的onCreate(最终捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    最终的ViewGroup容器=(ViewGroup中)findViewById(R.id.LinearLayout1);
    最后LayoutInflater吹气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);    findViewById(R.id.button).setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(最终查看V){
            最后查看视图= inflater.inflate(R.layout.field,NULL);
            使用下一个code ++工程,//使用它的时候,而不是膨胀的布局:
            //最后的EditText EDITTEXT =新的EditText(MainActivity.this);
            // editText.setText(项目+ container.getChildCount());
            container.addView(视图);
        }
    });
}

该领域布局文件:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / RelativeLayout1
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=#FFFFFFFF
    机器人:重力=center_vertical>    <微调
        机器人:ID =@ + ID / typeSpinner
        机器人:layout_width =80dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_centerVertical =真
        机器人:背景=@空
        机器人:填充=5DP/>    <包括
        机器人:ID =@ + ID / removeItemView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_centerVertical =真
        布局=@布局/ remove_item_view/>    <的EditText
        机器人:ID =@ + ID / fieldEditText
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerVertical =真
        机器人:layout_toLeftOf =@ + ID / removeItemView
        机器人:layout_toRightOf =@ + ID / typeSpinner
        机器人:EMS =10
        机器人:可聚焦=真
        机器人:focusableInTouchMode =真
        机器人:提示=场
        机器人:imeOptions =actionDone
        安卓的inputType =手机
        安卓了minWidth =200dp
        机器人:填充=5DP
        机器人:单线=真
        工具:忽略=硬codedText>
    < requestFocus的/>
    < /&的EditText GT;< / RelativeLayout的>

主要布局文件:

 <滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /滚动视图
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <的LinearLayout
        机器人:ID =@ + ID / LinearLayout1
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>        <按钮
            机器人:ID =@ + ID /按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=点击
            工具:忽略=硬codedText/>
    < / LinearLayout中>< /滚动型>


编辑:找到一个局部的解决方案,将不显示键盘向右走,但至少它会显示在pressing的EDITTEXT:

 公共静态无效forceFocusOnView(最终查看视图){
    如果(查看== NULL)
        返回;
    view.post(新的Runnable(){
        @覆盖
        公共无效的run(){
            view.clearFocus();
            view.post(新的Runnable(){
                @覆盖
                公共无效的run(){
                    view.requestFocus();
                }
            });
        }
    });
}


解决方案

我不知道的原因,问题的。可能会有一些missmatches当Android SDK中为了配合某些设备进行了改写。
所以我觉得你最好的解决办法是手动显示您的键盘,不要试图移动的火车,只取阻力最小的路径:

 的EditText等=(EditText上)this.findViewById(R.id.edit_text);
et.setOnFocusChangeListener(新OnFocusChangeListener(){        @覆盖
        公共无效onFocusChange(视图V,布尔hasFocus){
            InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                 imm.showSoftInput(等,0);&下; ----------------        }
    });

在这里,你走了,男人。祝你好运。

哦,我知道了...

 私人的EditText等;
私人Runnable接口的setFocus;
......
保护无效的onCreate(...){



 的setFocus =新的Runnable(){                @覆盖
                公共无效的run(){
                    et.requestFocus();                }
            };
}
....
 findViewById(R.id.button).setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(最终查看V){
        最后查看视图= inflater.inflate(R.layout.field,NULL);
      //尝试这个:
        等=(EditText上)view.findViewById(r.id.fieldEditText);
et.setOnFocusChangeListener(新OnFocusChangeListener(){    @覆盖
    公共无效onFocusChange(视图V,布尔hasFocus){
        InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
             imm.showSoftInput(等,0);&下; ----------------    }
});
        container.addView(视图);
        container.requestLayout();&下; --------------
        et.postDelayed(的setFocus,300);
    }
});

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();
                }
            });
        }
    });
}

解决方案

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);
    }
});

这篇关于键盘甚至没有点击它后膨胀EDITTEXT显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆