【问题标题】:How do you disable the SoftKeyboard key preview window?如何禁用软键盘按键预览窗口?
【发布时间】:2011-10-14 15:43:47
【问题描述】:

在创建自己的软键盘时,默认情况下会为您提供“按键预览”。

如何禁用此功能?

编辑:

您可以通过更改<KeyboardView> 属性android:keyPreviewLayout 来自定义keyPreview 布局。默认情况下,这是查看 keyboard_key_preview.xml 的样式:

编辑 2: 跟着我做红鲱鱼:

源代码建议提供 0 或不应用标签android:keyPreviewLayout 将导致不出现密钥预览:

    ...
    case com.android.internal.R.styleable.KeyboardView_keyPreviewLayout:
        previewLayout = a.getResourceId(attr, 0);
        break; 
    ...
    if (previewLayout != 0) {
        mPreviewText = (TextView) inflate.inflate(previewLayout, null);
        mPreviewTextSizeLarge = (int) mPreviewText.getTextSize();
        mPreviewPopup.setContentView(mPreviewText);
        mPreviewPopup.setBackgroundDrawable(null);
    } else {
        mShowPreview = false;
    }  

我试过了:

  • 没有 keyPreviewLayout 的样式化 KeyboardView(奇怪的是,虽然替换此值会更改预览的样式。)
  • 我为keyPreviewLayout 引用了一个值为 0 的 id(这会导致通货膨胀崩溃)。

难住了。 :( 任何帮助将不胜感激!

【问题讨论】:

    标签: android android-softkeyboard


    【解决方案1】:

    有一个方法:

    public void setPreviewEnabled(boolean previewEnabled)
    

    但我不知道从哪个版本的 API 开始。

    【讨论】:

    【解决方案2】:

    另一种方式 - 添加到 xml
    android:keyPreviewLayout="@null"

    <android.inputmethodservice.KeyboardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/keyboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:keyPreviewLayout="@null"
        android:visibility="gone"
        />
    

    【讨论】:

      【解决方案3】:

      你试过了吗:

      public static mEmptyView;
      
      //somewhere where you have the context:
      mEmptyView = new View(context);
      
      @Override public View onCreateCandidatesView() {
          return mEmptyView;
      }
      

      当候选人应该出现时,这基本上总是会返回一个空视图。

      【讨论】:

      • 这会阻止整个软键盘出现。我一直按标准返回nullInputMethodService.onCreateCandidatesView()
      • 此外,通常建议您永远不要将 View 保留为类的静态成员,因为 View 会保留对其祖先的引用,并且会很快造成内存泄漏。
      • 是的,最好在方法中创建一个...我也想过。垃圾收集器有更多工作要做,但垃圾收集比内存泄漏更好^^
      【解决方案4】:

      如果你想处理特定键的预览意味着在 onPress 方法中处理它。

      override fun onPress(primaryCode: Int) {
          handleKeyPreviews(primaryCode)
      }
      
      private fun handleKeyPreviews(code: Int) {
          when (code) {
              Keyboard.KEYCODE_DELETE, Keyboard.KEYCODE_SHIFT, Keyboard.KEYCODE_DONE, 32 ->
                  keyboardView?.isPreviewEnabled = false
              else ->
                  keyboardView?.isPreviewEnabled = true
          }
      }
      

      【讨论】:

      猜你喜欢
      • 2013-12-16
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      相关资源
      最近更新 更多