Android TextWatcher在键入时替换下划线
简介: 我目前正在尝试为我的纵横填字游戏实现一个Android TextWatcher在键入时替换下划线,android,textwatcher,Android,Textwatcher,简介: 我目前正在尝试为我的纵横填字游戏实现一个EditText输入法,用户在EditText中看到类似“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。下划线标记缺少的字母,输入的第一个字符将填充第一个下划线。 当然,谜题中的其他单元格可能已经解决,因此EditText文本可能是“ST_CKOV_RF_OW”。我在自己的输入视图中已经有了所有这些功能,它是视图的一个子类,具有一个被重写的onDraw()。这工
EditText
输入法,用户在EditText
中看到类似“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。下划线标记缺少的字母,输入的第一个字符将填充第一个下划线。
当然,谜题中的其他单元格可能已经解决,因此EditText
文本可能是“ST_CKOV_RF_OW”
。我在自己的输入视图中已经有了所有这些功能,它是视图的一个子类,具有一个被重写的onDraw()
。这工作得很好,除了视图不会出现在一些较低的Android版本上,并且Back键滑过了我的输入例程,无法访问
所以我想我应该使用EditText
,实现一个TextWatcher
,这样就可以了,但我无法让它正常工作。我现在所做的是工作,我可以使用键盘输入字母,但退格也不起作用,当然,如果用户触摸到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
上不起作用(我可以获取退格事件,没有其他内容)
所以,也许我完全走错了方向,但请帮助我,给我一个如何实现我的目标的线索。谢谢。我放弃了它,实现了一种简单但有效的黑客程序。我现在在我的(隐藏的)EditText中收到输入,输出转到visible TextView,中间的一个函数用来自EditText的输入填充“_”。
前
hint=“A_A_A_A”
edittext输入=“BBB”
文本视图显示“阿巴巴”