add android:configChanges="keyboardHidden" in manifestfile to the activity
我想在Java代码中设置我的EditText
,以便当我单击时仅显示Cursor
,而不显示android标准键盘。我怎样才能做到这一点?谢谢
我编辑:
我有一个EditText数组:
for (i=0;i<N*N;i++) {
value[i].setOnTouchListener(this);
value[i].setOnClickListener(this);
value[i].setOnFocusChangeListener(this);
}
和此方法:
@Override
public void onClick(View v) {
for (i =0 ; i < N*N; i++) {
if(v == value[i]) {
variable = 1;
if(jey!=i) {
jey=i;
showDialog(CUSTOM_DIALOG);
}
}
}
}
@Override
public void onTouch(View v, MotionEvent event) {
for (i =0 ; i < N*N; i++) {
if(v == value[i]) {
variable = 1;
if(jey!=i) {
jey=i;
showDialog(CUSTOM_DIALOG);
}
}
}
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
for (i =0 ; i < N*N; i++){
if(v == value[i]) {
variable =1;
if(jey!=i) {
jey=i;
imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(value[jey].getWindowToken(), 0);
}
}
}
}
但是这种方式不起作用,我仍然看到标准键盘
要将其应用于一个EditText(而不是针对Activity如何告诉其他答案),只需将其隐藏在focusChangeListener中即可。
mEditText.setOnFocusChangeListener(new CustomListener());
和CustomListener:
protected class CustomListener implements View.OnFocusChangeListener {
@Override
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
您可以检查hasFocus
并仅在为true时隐藏。
或使用此:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
编辑:
这是我在其他线程上发现的常见解决方案:
public class NoImeEditText extends EditText {
public NoImeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onCheckIsTextEditor() {
return false;
}
}
由于这是我的第三次尝试,而且我开始感到愚蠢,因此这次我进行了个性化测试,并且可以正常工作。如果您使用布局,请不要忘记更改布局的类型。
您在这里。
/**
* EditText which suppresses IME show up.
*/
public class DigitsEditText extends EditText {
public DigitsEditText(Context context, AttributeSet attrs) {
super(context, attrs);
setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
final InputMethodManager imm = ((InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE));
if (imm != null && imm.isActive(this)) {
imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final boolean ret = super.onTouchEvent(event);
// Must be done after super.onTouchEvent()
final InputMethodManager imm = ((InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE));
if (imm != null && imm.isActive(this)) {
imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
}
return ret;
}
@Override
public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED) {
// Since we're replacing the text every time we add or remove a
// character, only read the difference. (issue 5337550)
final int added = event.getAddedCount();
final int removed = event.getRemovedCount();
final int length = event.getBeforeText().length();
if (added > removed) {
event.setRemovedCount(0);
event.setAddedCount(1);
event.setFromIndex(length);
} else if (removed > added) {
event.setRemovedCount(1);
event.setAddedCount(0);
event.setFromIndex(length - 1);
} else {
return;
}
} else if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED) {
// The parent EditText class lets tts read "edit box" when this View has a focus, which
// confuses users on app launch (issue 5275935).
return;
}
super.sendAccessibilityEventUnchecked(event);
}
}
@ Lupsaa here的最佳解决方案:
将标志textIsSelectable设置为true将禁用软键盘。
您可以像这样在您的xml布局中设置它:
<EditText
android:id="@+id/editText"
...
android:textIsSelectable="true"/>
或以编程方式,如下所示:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
光标将仍然存在,您将可以选择/复制/剪切/粘贴,但软键盘将永远不会显示。
public void hideSoftKeyboard(){if(getCurrentFocus()!= null){InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);inputMethodManager.hideSoftInputFromWindow(getCurrentFocus()。getWindowToken(),0);}}
public void showSoftKeyboard(View view){InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);view.requestFocus();inputMethodManager.showSoftInput(view,0);}
然后,只需在edittext上添加另一个相同大小的布局,然后在其上单击事件,然后在无法单击edittext后。 <EditText
android:id="@+id/etAnswer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:cursorVisible="false"
android:inputType="number"
android:maxLength="6"
android:maxLines="1"
android:paddingLeft="10dp"
android:singleLine="true"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick" />
add android:configChanges="keyboardHidden" in manifestfile to the activity
add android:configChanges="keyboardHidden" in manifestfile to the activity