【发布时间】:2015-09-10 01:45:32
【问题描述】:
我正在尝试制作一个计算器,当使用 GUI 输入数字时,我希望用户能够单击 EditText 以更改光标,但不启用键盘。有没有办法做到这一点?
【问题讨论】:
-
简单,改用TextView。让它像 EditText 一样。
-
@user3466786 使用 android:cursorVisible="true"
我正在尝试制作一个计算器,当使用 GUI 输入数字时,我希望用户能够单击 EditText 以更改光标,但不启用键盘。有没有办法做到这一点?
【问题讨论】:
您可以为此目的使用以下代码,
<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());
【讨论】:
我想你想禁用安卓设备的输入键盘。 在这种情况下,最好的解决方案是:(在 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
}
});
【讨论】: