简体   繁体   English

在ArrayAdapter中执行edittext之外的操作后,如何在android中隐藏软键盘?

[英]How to hide the soft keyboard in android after doing something outside of edittext which is in ArrayAdapter?

I have an edittext, when I click in it, I get the number soft keyboard, which is what I need. 我有一个edittext,当我单击它时,我得到了所需的数字软键盘。 But when I click outside of the edittext, I get the regular keyboard, but I need to hide the keyboard after the user clicks outside the edittext. 但是,当我在edittext之外单击时,我得到了常规键盘,但是当用户在edittext之外单击时,我需要隐藏键盘。 The keyboard should disappear on scroll as well, not only on OnClick. 键盘也应该在滚动上消失,而不仅仅是在OnClick上消失。

public View getView(int position, View convertView, ViewGroup parent) {
            StoreSkuInfo storeSkuInfo = StoreSkuInfoList.get(position);  

            View row = convertView;
            String status="";
            StoreSkuHolder holder = null;

            if (row ==null) {    

                LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                row = mInflater.inflate(R.layout.pip_pickupinstore_listcell, null);

                holder = new StoreSkuHolder();

                            holder.qtyText = (EditText)row.findViewById(R.id.pip_qty_et);
                holder.qtyLL = (LinearLayout)row.findViewById(R.id.qty_LL);
                holder.pipQuantityStatus = (TextView) row.findViewById(R.id.quantity_status);
                holder.relativeLayoutStoreInfo = (RelativeLayout) row.findViewById(R.id.RelativeLayoutStoreInfo);
                holder.relativeLayoutAisleBay = (RelativeLayout) row.findViewById(R.id.RelativeLayoutAisleBay);


                row.setTag(holder);

            }else{
                holder = (StoreSkuHolder)row.getTag();
            }
}

As we know this getView method gets called many times, so putting the following code in getView method brings up the keyboard up and down every second when edittext is clicked. 我们知道该getView方法被调用过很多次,因此,在单击edittext时,将以下代码放入getView方法中会使键盘每秒上下移动。

holder.qtyText.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if(hasFocus) {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
                    }
                    if(!hasFocus) {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
//                      imm.hideSoftInputFromWindow(v.getWindowToken(), 1);
                    }
                }
            });

How can this be fixed? 如何解决?

EDIT: Tried the answer by Shruti as below. 编辑:尝试了Shruti的答案,如下所示。 I put the following code in parent class: 我将以下代码放在父类中:

public static void hideSoftKeyboard(Activity activity) 
    {

        if(activity!= null && activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null)
        {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);            
        }
    }

Then in the OnCreate, I put: 然后在OnCreate中,我将:

findViewById(android.R.id.content).getRootView().setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG, "In setOnTouchListener of root");
                hideSoftKeyboard(PIPActivity.this);
                return false;
            }
        });

But the code doesn't even go into this TouchListener. 但是代码甚至都没有进入这个TouchListener中。

EDIT 2 I tried Shashank Agarwal answers. 编辑2我尝试了Shashank Agarwal的答案。 But his answer is particular to the layout. 但是他的答案是特定于布局的。 I need the keyboard to disappear if user clicks anywhere else. 如果用户单击其他任何地方,我需要键盘消失。 Below is a picture to clarify. 下面是要澄清的图片。

活动布局

XML Layout where the EDITTEXT is below. 下面是EDITTEXT的XML布局。 I have deleted some lines from it to try and keep it to the point. 我从中删除了一些行,以尝试保持其正确性。 The EditText in discussion is in the bottom with this: android:id="@+id/pip_qty_et" 讨论中的EditText位于此底部: android:id =“ @ + id / pip_qty_et”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/shiptostore_cell"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:background="@color/ce"
    android:onClick="onClick"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal" >

    <RelativeLayout
        android:id="@+id/pip_store_cell_rl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:layout_marginBottom="20dip"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp" >

       <com.HDTextView
            android:id="@+id/pickupinstore_availtoday"
            style="@style/Normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_below="@+id/pickupinstore_milesaway"
            android:text="Available for Pick Up TODAY"
            android:textColor="@color/C7" />

        <RelativeLayout
            android:id="@+id/RelativeLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/pickupinstore_availtoday"
            android:layout_marginTop="15dip" >

            <com.HDTextView
                android:id="@+id/seperator"
                android:layout_width="0dip"
                android:layout_height="0dip"
                android:layout_centerInParent="true" />

            <RelativeLayout
                android:id="@+id/RelativeLayoutAisleBay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="3dp"
                android:layout_toLeftOf="@id/seperator"
                android:visibility="gone" >

                <com.HDTextView
                    android:id="@+id/pickupinstore_bin"
                    style="@style/Small"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="8dip"
                    android:layout_toRightOf="@+id/pickupinstore_aisle"
                    android:maxWidth="70dp"
                    android:singleLine="true"
                    android:text="Bay [x]"
                    android:textColor="@color/RGB51" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dip"
                android:layout_toRightOf="@id/storeLocationButton"
                android:gravity="center" >

                <com.HDTextView
                    android:id="@+id/quantity_status"
                    style="@style/SmallBold"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="5dp"
                    singleLine="true"
                    android:background="@drawable/bevelededge_mob_lightest_green"
                    android:gravity="center"
                    android:minWidth="20dp"
                    android:paddingLeft="2dp"
                    android:paddingRight="2dp"
                    android:text="99"
                    android:textColor="@color/White"
                    android:textSize="12dip" />

            </RelativeLayout>
        </RelativeLayout>

        <LinearLayout 
            android:id="@+id/qty_LL"
            android:layout_width="50dip"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:orientation="vertical"
            android:clickable="true"
            android:focusable="true"
            android:layout_alignParentRight="true">

                <com.HDTextView
                    style="@style/Small"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:singleLine="true"
                    android:text="QTY"
                    android:textColor="@color/Grey" />

                <EditText
                    android:id="@+id/pip_qty_et"
                    android:layout_width="45dip"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:layout_alignParentRight="true"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:imeOptions="actionDone"
                    android:gravity="center"
                    android:hint="5"
                    android:onClick="test"
                    android:inputType="number"
                    android:maxLength="3" />

        </LinearLayout>"


        <RelativeLayout
            android:id="@+id/RelativeLayoutStoreInfo"
            android:layout_width="150dip"
            android:layout_height="wrap_content"
            android:layout_above="@+id/RelativeLayout"
            android:layout_alignLeft="@+id/pickupinstore_storename"
            android:layout_alignParentTop="true">
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

Sample layout xml file. 样本布局xml文件。

<RelativeLayout android:id="@+id/rootLayout"...

<Textview.../>
<Button../>

....
...

<RelativeLayout/>

In the above sample relative layout is the rootLayout. 在上面的示例中,相对布局是rootLayout。

By rootLayout I mean the parent layout for that layout. rootLayout是指该布局的父布局。

In your case it is LinearLayout 您的情况是LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/shiptostore_cell"..../>

Try this : 尝试这个 :

public static void hideSoftKeyboard(Activity activity) 
    {

        if(activity!= null && activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null)
        {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);            
        }
    }

call this function on touch of the root layout and it will work. 只需触摸根目录布局即可调用此函数,它将起作用。

rootLayout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(QuestionTemplateActivity.this);
                return false;
            }
        });

Put this code outside the onCreate 将此代码放在onCreate之外

public void onClick(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

In you layout xml file put this line on the parent layout so it became clickable 在布局xml文件中,将此行放在父布局上,因此可以单击

android:onClick="onClick"   

I know its too late and it might not help you but this will help for someone else. 我知道为时已晚,它可能对您没有帮助,但这对其他人有所帮助。 If you want to hide the default keyboard then you can add the following line in your EditText's XML file. 如果要隐藏默认键盘,则可以在EditText的XML文件中添加以下行。

android:keyboardNavigationCluster="false" android:keyboardNavigationCluster =“ false”

This will work : 这将起作用:

    @Override
public boolean dispatchTouchEvent(MotionEvent event) {

    View v = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (v instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

        Log.d("Activity",
                "Touch event " + event.getRawX() + "," + event.getRawY()
                        + " " + x + "," + y + " rect " + w.getLeft() + ","
                        + w.getTop() + "," + w.getRight() + ","
                        + w.getBottom() + " coords " + scrcoords[0] + ","
                        + scrcoords[1]);
        if (event.getAction() == MotionEvent.ACTION_UP
                && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
                        .getBottom())) {

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
                    .getWindowToken(), 0);
        }
    }
    return ret;
}

This is what I did. 这就是我所做的。 This worked since I needed the keyboard to hide itself on click or scroll. 之所以可行,是因为我需要键盘在单击或滚动时隐藏自身。

@Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
                if (getCurrentFocus() != null) {
                      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                      imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    }
            return super.dispatchTouchEvent(ev);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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