在Android中设置textIsSelectable(true)后,软键盘无法打开

4
为了使EditText可以被选择而不显示软键盘,我设置了edittext.textIsSelectable(true)属性并隐藏了软键盘。
当我尝试再次打开软键盘时,它没有打开。
我尝试在EditText中设置setFocusable(true),但仍然无法打开键盘。欢迎提出任何建议。
谢谢。

在你的Java代码中使用edittext.requestFocus(); - Harshad Pansuriya
我尝试了,但仍然不起作用。 - Vijay
3个回答

3

通过使用以下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中选择一些文本时,软键盘不应该打开。在您的情况下,只有在选择剪切、复制或粘贴后,我们才能隐藏软键盘。但在我的情况下,在选择剪切、复制或粘贴之前,键盘应该被隐藏。 谢谢。 - Vijay

1

如果不设置属性edittext.textIsSelectable(true),你将无法选择输入的文本并复制它,为什么不使用具有基本属性的edittext?


我不希望在编辑文本中选择文本时键盘打开。我认为这是隐藏键盘而不影响选择的唯一方法。如果您知道其他隐藏键盘的方法,请随时回答。 :-) - Vijay
我移除了textSelectable属性,现在它可以正常工作了。而且即使没有这个属性,文本也是可选的。 - Rasel

1
我找到了答案。
只需在EditText中覆盖isTextSelectable()方法即可。
如果您想打开键盘,请返回super.isTextSelectable()
如果您不想打开键盘,请返回true
就是这样。这对我有用。

将此属性设置为false甚至从XML中删除它是相同的。 - Cícero Moura

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接