首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏AutoCompleteTextView键盘

隐藏AutoCompleteTextView键盘
EN

Stack Overflow用户
提问于 2013-11-07 09:47:31
回答 4查看 8.1K关注 0票数 0

我的布局中有两个EditText和两个AutoCompleteTextView。但是我喜欢只为AutoCompleteTextView隐藏键盘。但是它没有隐藏键盘,当我触摸AutoCompleteTextView时,键盘仍然出现,我如何隐藏键盘?我实现为

代码语言:javascript
复制
final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);                     
        final TrackerInfo newInfo = new TrackerInfo();          

        String[] type = {"Vehicle", "Person", "Pet", "Others"}; 
        final AutoCompleteTextView actvtype1 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView1);             
        ArrayAdapter<String> typeadapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_multiple_choice,type);          
        actvtype1.setThreshold(1);
        actvtype1.setAdapter(typeadapter);
        actvtype1.setTextColor(Color.BLACK);            

        String[] model = {"TS102", "TS103"};
        final AutoCompleteTextView actvtype2 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView2);
        ArrayAdapter<String> modeladapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_multiple_choice,model);
        actvtype2.setThreshold(1);
        actvtype2.setAdapter(modeladapter);
        actvtype2.setTextColor(Color.BLACK);            

        final AlertDialog.Builder alert = new AlertDialog.Builder(this).setTitle("New Tracker").setView(addView);
        InputMethodManager keyboard = (InputMethodManager)
        getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.hideSoftInputFromInputMethod(actvtype1.getWindowToken(), 0);
        keyboard.hideSoftInputFromInputMethod(actvtype2.getWindowToken(), 0); 
        alert.setPositiveButton("ADD", new DialogInterface.OnClickListener() 
        {


            @SuppressLint("SimpleDateFormat")
            public void onClick(DialogInterface dialog, int whichButton) 
            {                   


            }
        }).setNegativeButton("Cancel", null).show();

我的布局如下

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/IDnumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/IDnumber" />

    <EditText
        android:id="@+id/IDeditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:inputType="text">        
        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/SimCardNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Sim_card_number" />

    <EditText
        android:id="@+id/SimCardEdit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
         />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/description" />   

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10"
        android:dropDownHeight="100dp"  
        android:text=""/>

    <TextView
        android:id="@+id/model"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Tracker_model"         
        />    
    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10"         
        android:dropDownHeight="100dp"  
        android:text=""/>



</LinearLayout>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-11-07 09:54:13

我不确定这是否正是您要寻找的内容,但这是用来迫使键盘从屏幕底部弹出的代码:

代码语言:javascript
复制
actvtype1.setInputType(InputType.TYPE_NULL);

当您单击该TextView时,这肯定会阻止键盘出现。希望能帮上忙!

票数 8
EN

Stack Overflow用户

发布于 2013-11-07 10:10:38

将重点放在自动文本上的请求如下:

代码语言:javascript
复制
   <AutoCompleteTextView
            android:id="@+id/autoCompleteTextView2"
                ..
                .../>
         <requestFocus />

然后使用以下代码:

代码语言:javascript
复制
 if(autocomplete.hasfocus()){
    hideSoftKeyboard();
    }


    private void hideSoftKeyboard() {
        if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText)
        {
             InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
             imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }

     }
票数 2
EN

Stack Overflow用户

发布于 2016-12-30 18:52:50

单击onItemClick中的列表项后隐藏键盘

代码语言:javascript
复制
public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
     InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
     // whatever else should be done
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19832637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档