简介:我目前正在尝试为我的填字游戏实现一种输入法,用户可以在EditText
其中看到类似. 下划线标记缺少的字母,输入的第一个字符将填充第一个下划线。当然,拼图中的其他单元格可能已经解决,所以文本可能是. 我已经在我自己的输入视图中拥有了所有这些功能,这是一个视图的子类,带有一个被覆盖的. 这工作得很好,除了视图不会出现在某些较低的 Android 版本上,并且后退键滑过我的输入例程并且无法访问。"____"
EditText
EditText
"ST_CKOV_RF_OW"
onDraw()
所以我想我会用EditText
, 实现 aTextWatcher
并且没问题,但我不能让它正常工作。我现在正在工作,我可以使用键盘输入字母,但是 Backspace 再次不起作用,当然,如果用户触摸到EditText
该位置就会搞砸。
public void beforeTextChanged(CharSequence s,int start,int count, int after){
et.removeTextChangedListener(textWatcher);
int position = text.indexOf("_");
if(position==-1) onAnswerEntered(et.getText().toString().replace("_", "")); //finished
else {
et.setSelection(et.getText().toString().indexOf("_"));
et.addTextChangedListener(textWatcher);
}
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(textWatcher);
try {
String currentKey = s.toString().substring(start, start+1);
Logger.log("Current Key", currentKey);
int position = text.indexOf("_");
Logger.log("Current Position _ ", position+"");
//replace _ with key
String sbefore=text.substring(0, position);
String safter=text.substring(position+1, text.length());
text=sbefore+currentKey+safter;
int positionNext = text.indexOf("_");
Logger.log("Next Position _ ", positionNext+"");
if(positionNext==-1) onAnswerEntered(et.getText().toString().replace("_","")); //finished
else {
et.setText(text);
et.setSelection(et.getText().toString().indexOf("_"));
et.addTextChangedListener(textWatcher);
}
} catch(IndexOutOfBoundsException ioobe) {
ioobe.printStackTrace();
}
}
我也尝试设置一个OnKeyListener
,但它不起作用EditText
(我可以得到退格事件,没有别的)
所以也许我完全走错了路,但请帮助我,给我一个线索,告诉我如何实现我的目标。谢谢。