【问题标题】:How to hide keyboard completely in activity?如何在活动中完全隐藏键盘?
【发布时间】:2020-02-23 20:13:35
【问题描述】:

如何在 Activity 中隐藏键盘并阻止它打开,即使通过单击编辑文本(以编程方式)?

我已经解决了: 我在 onCreate 事件中使用了这段代码:

edittext1.setShowSoftInputOnFocus(false);

这将禁用edittext中的键盘,而不会干扰选择器或光标。

【问题讨论】:

标签: android keyboard android-softkeyboard soft-keyboard


【解决方案1】:

在活动的 onCreate() 方法中隐藏键盘

/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
   if(getCurrentFocus()!=null) {
       InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
   }
}

或者干脆在 AndroidManifest.xml 文件中使用这个 ("android:windowSoftInputMode="stateHidden")

<activity
 android:name="com.example.stockquote.StockInfoActivity"
 android:windowSoftInputMode="stateHidden
 android:label="@string/app_name" />

【讨论】:

  • 我不想在点击edittext时打开键盘
  • 在布局文件中的Edittext控制器中添加以下属性
【解决方案2】:

有两种方法可以实现:

在清单中执行以下操作:

<activity
    android:name=".MyActivity"
    android:windowSoftInputMode="stateAlwaysHidden"/>

或者在您的 java 代码中执行以下操作:

View view = this.getCurrentFocus();
    if (view != null) {  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

详细解释请参考此SO answer

【讨论】:

  • 我不想在点击edittext时打开键盘
  • @MahabubKarim 准备好了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多