2

我有一个 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);


}
4

1 回答 1

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);在运行时调用来更改它。

于 2012-08-30T17:38:15.027 回答