如何在点击Spinner后隐藏虚拟键盘

22

我有一个EditText和一个Spinner。当我点击EditText时,键盘会弹出,编辑文本结束后,我点击Spinner的下拉箭头,但是键盘不会自动消失。请给我一些解决方案。我尝试了这段代码:

 InputMethodManager imm=(InputMethodManager)getApplicationContext().getSystemService(SetUpProfileActivity.this.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mDateOfBirth.getWindowToken(), 0);

这是XML文件

<LinearLayout
            android:id="@+id/outerlayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp" 

            >

            <TextView
                android:id="@+id/name_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/profile_name"
                android:textColor="#ffffff" />

            <EditText
                android:id="@+id/profile_name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:background="@drawable/txtbox"
                android:singleLine="true" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="@string/dateofbirth"
                android:textColor="#ffffff" />

            <Spinner
                android:id="@+id/dob"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:background="@drawable/dropdown" />

软键盘在您的EditText失去焦点时会自动消失...所以您可能正在使用EditText的其他属性,请展示您的XML和代码。 - Niranj Patel
@CapDroid,我已经发布了我的XML,请检查并告诉我哪里出错了。 - swati
3个回答

48

尝试这段代码,希望它对你有用。

mSpinner.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                InputMethodManager imm=(InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
                return false;
            }
        }) ;

有趣的是,您可以使用视图上的任何元素来获取WindowToken。无论您使用哪个EditText。 - Patapoom

1
如果您在一个Activity中同时使用了Spinner和EditText,那么您肯定会遇到这个问题,
在Spinner上调用onTouchListener,在其中引用您的EditText并隐藏软键盘。
 mySpinner.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager inputMethodManager=(InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(mReuestBloodActNotes.getWindowToken(), 0);
            return false;
        }
    }) ;

0

试试这个:

// hide the keyboard if open
            inputManager.hideSoftInputFromWindow(getParent().getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

这是我用来设置Spinner的代码。你能建议我在哪里使用你的代码吗?ArrayAdapter<String> dataAdapterage = new ArrayAdapter<String>(this, R.layout.spinner_adapter, ageValue); dataAdapterheight.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mDateOfBirth.setAdapter(dataAdapterage); - swati
你应该在下拉列表的点击监听器中编写这个。 - Anup Cowkur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接