如何在android中检查编辑文本的回车键。
问问题
229 次
4 回答
2
您必须在 Activity 中实现 OnKeyListener 并覆盖其 onKey() 方法:
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
flag = false;
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() != KeyEvent.ACTION_DOWN) {
flag = false;
} else if (event.getAction() != KeyEvent.ACTION_UP) {
// do your work on Enter key pressed.
flag = true;
}
}
return flag;
}
于 2012-10-18T08:42:41.927 回答
1
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
试试这个
于 2012-10-18T08:39:31.890 回答
1
用于onKeyListener
检查 Enter 按下
例如。
edittext.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction()!=KeyEvent.ACTION_DOWN)
return false;
if(keyCode == KeyEvent.KEYCODE_ENTER ){
//your necessary codes...
return true;
}
return false;
}
};
更多信息,查看官方文档
于 2012-10-18T08:39:33.373 回答
1
我想您有一个保存按钮,并且您想在单击它时进行验证?
saveButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String textInput = myEditText.getText().toString();
// do your validation here
}
});
于 2012-10-18T08:43:07.697 回答