【问题标题】:How to show keyboard only when user click on edit text box to insert text? [duplicate]仅当用户单击编辑文本框以插入文本时如何显示键盘? [复制]
【发布时间】:2018-07-31 10:17:30
【问题描述】:
<EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:drawableLeft="@drawable/search"
    android:background="@drawable/search_bar"
    android:paddingLeft="15dp"
    android:drawablePadding="10dp"
    android:inputType="text"
    android:id="@+id/srchbar"
    android:onClick="searchBarClick">
</EditText>
public void searchBarClick(View view){

}

我有一个编辑文本框和 onClick 功能。请告诉如何仅在用户单击编辑文本框以插入数据时显示键盘。在正常情况下,键盘会在我运行我的应用程序时出现,但我只希望它在何时显示用户点击编辑文本框。

【问题讨论】:

标签: android android-studio


【解决方案1】:

在布局的根元素中添加以下代码。

 <LinearLayout
     android:layout_width="0px"
     android:layout_height="0px"
     android:focusable="true"
     android:focusableInTouchMode="true" />

【讨论】:

    【解决方案2】:
    editText.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager)getSystemService(
            Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
            }
        }, 50);
    

    有时edittext可能会在手动隐藏软键盘后获得焦点。

    所以,延迟一段时间,然后在你的活动或片段的 onResume() 方法中强制隐藏它

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 1970-01-01
      • 2016-07-25
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多