【问题标题】:When EditView is touched cursor must be displayed but system soft keyboards must not be displayed?触摸EditView时必须显示光标但不能显示系统软键盘?
【发布时间】:2018-06-17 11:39:34
【问题描述】:

我想在触摸编辑文本时显示光标,但不能显示软键盘。下面的代码在触摸时不显示光标。有人请帮我解决这个问题。我使用三星 Galaxy S6 设备测试我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText edit=(EditText)findViewById(R.id.edit);

    edit.setInputType(InputType.TYPE_NULL);

    edit.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            edit.setCursorVisible(true);
            return false;
        }
    });

}

XML 代码:

<EditText
    android:layout_width="wrap_content"
    android:id="@+id/edit"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

edit.setInputType(InputType.TYPE_NULL); 隐藏了软键盘,但是为什么 edit.setCursorVisible(true); ,不让光标可见。这是某些设备特定的吗?如果是这样,我该如何解决?

【问题讨论】:

    标签: android editview


    【解决方案1】:

    在您的 Manifest.xml 中

    <activity android:name="com.your.package.ActivityName"
      android:windowSoftInputMode="stateHidden"  />
    

    或者你可以使用

      final edit = (EditText) findViewById(R.id.edit);
      edit.setInputType((InputType.TYPE_NULL);
    

    【讨论】:

    • 我试过你的答案,但它显示软键盘
    • edit.setInputType((InputType.TYPE_NULL); 在最后的 EditText 之后添加这个 edit=(EditText)findViewById(R.id.edit);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多