如何防止在单击EditText时弹出键盘?

8

当我从编辑文本中选择文本时,如何防止键盘打开?我希望内部的文本仍然可以被选择,但是在点击它时不要打开键盘。

 <EditText
        android:id="@+id/displayalllinks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

3
如果您不希望出现软键盘,为什么要使用“EditText”? 为什么不使用带有“android:textIsSelectable =“true””属性的“TextView”? - CommonsWare
为什么你不想让它弹出来?如果你想在按钮被点击后让它消失,可以尝试这个链接 - Appafly
你的问题不够清晰,你应该提供更多细节(例如不需要在视图中键入文本,只需选择已设置的文本),或者根本不使用 EditText - Dimitar
我的理由是创建一个计算器应用程序,它不需要软键盘,而是使用自定义布局和按钮。使用EditText的原因是它可以显示光标,这样您就可以将其移动并在其中添加数字,而无需首先删除所有输入。 - Denny
3个回答

3
editText = (EditText) findViewById(R.id.editText_1);
boolean disabled = false;

//We listen for selection
editText.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {

        if (MotionEvent.ACTION_UP == event.getAction()) {

          //When selected, we disable input.
          if (editText.hasSelection()) {
              editText.setInputType(InputType.TYPE_NULL);
              disabled = true;
          } else 
              //Then we restore it, if previously unset.
              if (disabled) {  
                  editText.setInputType(TYPE_CLASS_TEXT);
                  disabled = false;
              }  

        return false;
      }
});

希望这能帮到你!

2
你可以通过添加inputType为none和textIsSelectable来实现这一点:
 <EditText
        android:id="@+id/displayalllinks"
        android:inputType="none"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10" />  

And

 EditText editText = (EditText)findViewById(R.id.edit_mine);
    editText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null  && editText.hasSelection()){
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }                
            return true;
        }
    });


这将禁用键盘在所有情况下,而不仅仅是在选定时。 - Dimitar
好的,我更新了我的回答。请检查一下是否是您想要做的。 - Joel

2
您IP地址为146.190.162.183,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。
public class MyEditText extends AppCompatEditText {
    public MyEditText(Context context) {
        super(context);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int currentInputType = getInputType();
        setInputType(InputType.TYPE_NULL);
        boolean b = super.onTouchEvent(event);
        setInputType(currentInputType);
        return b;
    }
}

这是翻译的结果:

这是结果:

点击EditText不打开软键盘

在这个gif中,你可以看到它不会破坏长按功能。


1
但这并不能阻止键盘的出现。 - Denny
1
@Denny,问题是“当单击EditText时如何使键盘显示”,它没有提到长按。 - azizbekian

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