在EditText数组上禁用Android键盘

时间:2012-08-30 17:35:03

标签: android android-layout android-intent android-widget

我有一个EditText数组,我想禁用每次点击它时出现的标准键盘Android。

这些是我正在使用的零件代码:

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);


    }

编辑:

我用这些代码行创建了一个新类:

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;
}
}

在OnCreate的主要课程中:

for (i=0;i<dim*dim;i++){

((KeyboardControlEditText) value[i]).setShowKeyboard(false);
value[i].setOnTouchListener(this);
value[i].setOnClickListener(this);


}

1 个答案:

答案 0 :(得分:5)

您需要为此创建自己的EditText课程。然后,覆盖默认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;
    }
}

确保使用正确的名称替换新EditText。例如,如果您的包是com.example.widget,则您需要使用<com.example.widget.NoKeyboardEditText ... />

如果你需要它是动态的,你可以变得更加漂亮:

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;
    }
}

这样,您可以调用((KeyboardControlEditText) myEditText).setShowKeyboard(false);在运行时更改它。