[英]Disable the Android keyboard on an array of EditText
I have an array of EditText and I want to disable the standard keyboard Android that appears every time I click on them. 我有一个EditText数组,我想禁用每次单击它们时出现的标准键盘Android。
these are the parts code I am using: 这些是我正在使用的零件代码:
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
for (i=0;i<dim*dim;i++){
imm.hideSoftInputFromWindow(value[i].getWindowToken(), 0);
value[i].setOnTouchListener(this);
value[i].setOnClickListener(this);
value[i].setOnFocusChangeListener(this);
}
EDIT: 编辑:
I created a new class, with these lines of code: 我用这些代码行创建了一个新类:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
public class KeyboardControlEditText extends EditText {
private boolean mShowKeyboard = false;
public void setShowKeyboard(boolean value) {
mShowKeyboard = value;
}
// This constructor has to be overriden
public KeyboardControlEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
// Now tell the VM whether or not we are a text editor
@Override
public boolean onCheckIsTextEditor() {
return mShowKeyboard;
}
}
and in my main class in OnCreate: 在我的OnCreate主类中:
for (i=0;i<dim*dim;i++){
((KeyboardControlEditText) value[i]).setShowKeyboard(false);
value[i].setOnTouchListener(this);
value[i].setOnClickListener(this);
}
You need to create your own EditText
class for this. 您需要为此创建自己的EditText
类。 Then, override the default onCheckIsTextEditor
and return false
. 然后,覆盖默认的onCheckIsTextEditor
并返回false
。
public class NoKeyboardEditText extends EditText {
// This constructor has to be overriden
public NoKeyboardEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
// Now tell the VM that we are not a text editor
@Override
public boolean onCheckIsTextEditor() {
return false;
}
}
Make sure you substitute in the correct name for the new EditText
. 确保使用正确的名称替换新的EditText
。 For example, if your package is com.example.widget
, you'd want to use <com.example.widget.NoKeyboardEditText ... />
. 例如,如果您的程序包是com.example.widget
,则需要使用<com.example.widget.NoKeyboardEditText ... />
。
If you need this to be dynamic, you can get even fancier: 如果您需要使它动态变化,您甚至可以得到更出色的选择:
public class KeyboardControlEditText extends EditText {
private boolean mShowKeyboard = false;
public void setShowKeyboard(boolean value) {
mShowKeyboard = value;
}
// This constructor has to be overriden
public KeyboardControlEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
// Now tell the VM whether or not we are a text editor
@Override
public boolean onCheckIsTextEditor() {
return mShowKeyboard;
}
}
That way, you can call ((KeyboardControlEditText) myEditText).setShowKeyboard(false);
这样,您可以调用((KeyboardControlEditText) myEditText).setShowKeyboard(false);
to change it at runtime. 在运行时进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.