这旨在检测用户在键入字母A后何时按下空格键。如果按下空格键,则应将A替换为C。否则,应将其单独放置。
此外,如果用户按空格键然后按退格键,则替换的字母(C)应该变为A,并且不再被替换。 基本上是一个较弱的自动更正系统。
我的问题是,当我只是键入随机字母然后返回false时,报告是否按下空格键的布尔值会随机报告true。
对此将有任何新的关注!我似乎无法弄清楚是什么原因导致了这种情况的发生。预先感谢。
public class SpellingsClient extends Activity implements SpellCheckerSession.SpellCheckerSessionListener
{
TextWatcher tt = null;
private EditText suggestions;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
suggestions = new EditText(this);
setContentView(suggestions);
// fetchSuggestionsFor(suggestions.getText().toString());
tt = new TextWatcherExtended() {
boolean movedOn;
public void afterTextChanged(Editable s, boolean backSpace, boolean autoCorrect){
//Checks if the backspaced was pressed, sets movedOn to false if true
suggestions.setSelection(s.length());
if (backSpace){
Log.d("test2", "Backspace pressed!" + backSpace);
boolean movedOn = false;
}
Log.d("movedOn", "is " + movedOn);
movedOn = true;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){
//Checks if spacebar pressed and user moved on, then replaces the word
//if user pressed backspace, it sets the letters back to what they were
suggestions.removeTextChangedListener(tt);
if (spacePressed = true && movedOn) {
suggestions.setText(suggestions.getText().toString().replace("A", "C"));
} else if (!movedOn) {
suggestions.setText(suggestions.getText().toString().replace("C", "A"));
}
suggestions.addTextChangedListener(tt);
}
};
suggestions.addTextChangedListener(tt);
}
boolean spacePressed;
public boolean dispatchKeyEvent(KeyEvent event){
//checks if spacebar was pressed and sets spacePressed to true if so
if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE && event.getAction() == KeyEvent.ACTION_UP){
Log.d("test", "Space bar pressed!" + event);
spacePressed = true;
return true;
}
Log.d("Spacepressed", "it's" + spacePressed);
spacePressed = false;
return super.dispatchKeyEvent(event);
}
答案 0 :(得分:4)
spacePressed = true
是您的问题。这是一项分配,而不是比较。
改为使用==
;或者,更容易使用spacePressed
,因为condition == true
与condition
相同,并且避免了可能出现错字的情况。