我有一个按钮,当我在EditText中键入一些数字时会做一堆东西。我想知道如何通过简单地按下" Enter"来触发这个按钮。键。 现在我只有一个onClickListener,用户在将数据输入EditText后必须按下。我希望Button保留在那里并为用户提供额外的选项以便继续进行。
有人可以帮忙吗? 下面的代码什么也没做。当我按下键盘上的Enter键时,它就转到下一行......
EditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)){
Button.performClick();
}
return false;
}
});
答案 0 :(得分:2)
此代码适用于我
youredittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Enter pressed");
}
return false;
}
});
答案 1 :(得分:0)
见:
edittext.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
Toast.makeText(MainActivity.this,edittext.getText(), Toast.LENGTH_LONG).show();
return true;
}
答案 2 :(得分:0)
请参阅下面的解决方案代码,
editText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent)
{
if (id == EditorInfo.IME_NULL)
{
//Do your button action here
return true;
}
return false;
}
});