【问题标题】:Android: Have a non-editable EditText, but still have a cursorAndroid:有一个不可编辑的EditText,但仍然有一个光标
【发布时间】:2015-09-10 01:45:32
【问题描述】:

我正在尝试制作一个计算器,当使用 GUI 输入数字时,我希望用户能够单击 EditText 以更改光标,但不启用键盘。有没有办法做到这一点?

【问题讨论】:

  • 简单,改用TextView。让它像 EditText 一样。
  • @user3466786 使用 android:cursorVisible="true"

标签: android android-edittext


【解决方案1】:

您可以为此目的使用以下代码,

<EditText
   android:id="@+id/editext1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:textCursorDrawable="@null"
   android:cursorVisible="true">

</EditText>

然后,

EditText input = (EditText)findViewById(R.id.edittext1);
input.setSelection(input.getText().length());

【讨论】:

    【解决方案2】:

    我想你想禁用安卓设备的输入键盘。 在这种情况下,最好的解决方案是:(在 Manifest 文件和特定活动的活动构造中)

    <activity android:name=".MainActivity" 
    
        android:windowSoftInputMode="stateHidden" />
    

    或者您可以使用它来使用 onTouch 事件:

    editText_input_field.setOnTouchListener(otl);
    
    private OnTouchListener otl = new OnTouchListener() {
    public boolean onTouch (View v, MotionEvent event) {
            return true; // the listener has consumed the event
    }
    };
    

    这是另一个例子。

    MyEditor.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    int inType = MyEditor.getInputType(); // backup the input type
    MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
    MyEditor.onTouchEvent(event); // call native handler
    MyEditor.setInputType(inType); // restore input type
    return true; // consume touch even
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 2011-07-08
      相关资源
      最近更新 更多