【问题标题】:Soft keyboard not opening after setting textIsSelectable(true) in Android在Android中设置textIsSelectable(true)后软键盘未打开
【发布时间】:2017-02-15 05:50:38
【问题描述】:

为了在不显示软键盘的情况下选择edittext,我设置了edittext.textIsSelectable(true) 属性并隐藏了软键盘。

当我再次尝试打开软键盘时,它没有打开。

我尝试在编辑文本中设置setFocusable(true),但键盘仍然无法打开。 任何建议将不胜感激。

谢谢

【问题讨论】:

  • 在你的java代码中使用edittext.requestFocus();
  • 我试过了还是不行

标签: android keyboard android-edittext android-softkeyboard


【解决方案1】:

通过使用下面的 EditText 代码,您可以获得Cut/Copy/Paste 的事件 你必须像下面这样创建editText。你可以在事件触发后隐藏软键盘......

<com.example.textlink.EditTextMonitor
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:hint="EditText" />

在你的包中创建一个java文件...

public class EditTextMonitor extends EditText {
private final Context mcontext; // Just the constructors to create a new
                                // EditText...

public EditTextMonitor(Context context) {
    super(context);
    this.mcontext = context;
}

public EditTextMonitor(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mcontext = context;
}

public EditTextMonitor(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.mcontext = context;
}

@Override
public boolean onTextContextMenuItem(int id) {
    // Do your thing:
    boolean consumed = super.onTextContextMenuItem(id);
    // React:
    switch (id) {
    case android.R.id.cut:
        onTextCut();
        break;
    case android.R.id.paste:
        onTextPaste();
        break;
    case android.R.id.copy:
        onTextCopy();
    }
    return consumed;
}

/**
 * Text was cut from this EditText.
 */
public void onTextCut() {
    InputMethodManager imm = (InputMethodManager) getContext()
            .getApplicationContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    Toast.makeText(mcontext, "Event of Cut!", Toast.LENGTH_SHORT).show();
}

/**
 * Text was copied from this EditText.
 */
public void onTextCopy() {
    InputMethodManager imm = (InputMethodManager) getContext()
            .getApplicationContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    Toast.makeText(mcontext, "Event of Copy!", Toast.LENGTH_SHORT).show();
}

/**
 * Text was pasted into the EditText.
 */
public void onTextPaste() {
    InputMethodManager imm = (InputMethodManager) getContext()
            .getApplicationContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    Toast.makeText(mcontext, "Event of Paste!", Toast.LENGTH_SHORT).show();
}}

【讨论】:

  • 您好 Santhosh,感谢您的回复。对此,我真的非常感激。但是问题陈述是当我们在edittext软键盘中选择一些文本时不应该打开。在您选择剪切、复制或粘贴后的情况下,我们只能隐藏软键盘。但在我的情况下,在选择剪切、复制或粘贴之前应该隐藏键盘。谢谢。
【解决方案2】:

不设置属性edittext.textIsSelectable(true)

你可以选择输入的文本并正确复制它,为什么不盲目使用具有基本属性的edittext

【讨论】:

  • 当我在 edittext 中选择某些文本时,我不想打开键盘,这就是原因。我认为这是隐藏键盘而不影响选择的唯一方法。如果您知道在选择文本时隐藏键盘的其他方法,欢迎您回答。 :-)
  • 我删除了 textSelectable,现在它可以工作了。没有这个属性也可以选择文本。
【解决方案3】:

我找到了答案。

只需覆盖 Edittext 中的 isTextSelectable() 方法即可。

如果要打开键盘,请返回super.isTextSelectable()

如果您不想打开键盘,请返回true

就是这样。它对我有用。

【讨论】:

  • 将此属性设置为 false 甚至将其从 XML 中删除都是一样的。
猜你喜欢
  • 1970-01-01
  • 2011-08-05
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多