需要制作一个处理错误答案的循环,允许另一个输入EditText
mEdit.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
if(sumAsString.equals(mEdit.getText().toString())) {
Toast.makeText(getApplicationContext(), "That's right!", Toast.LENGTH_SHORT).show();
} else {
mEdit.setText(null);
Toast.makeText(getApplicationContext(), "Sorry Try again ", Toast.LENGTH_SHORT).show();
return true;
}
}
return false;
}
});
答案 0 :(得分:0)
使用IME_ACTION
代替下面的关键监听器
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"/>
并在OnEditorActionListener
上使用EditText
,如下所示
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do here your stuff f
return true;
}
return false;
}
});
参考this answer。
它会帮助你。
答案 1 :(得分:-1)
尝试删除return false;
或将其放在括号上方。
mEdit.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
if(sumAsString.equals(mEdit.getText().toString())) {
Toast.makeText(getApplicationContext(), "That's right!", Toast.LENGTH_SHORT).show();
} else {
mEdit.setText(null);
Toast.makeText(getApplicationContext(), "Sorry Try again ", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
});