我想知道是否有一种方法来处理用户在输入EditText时按下Enter,就像onSubmit HTML事件。
还想知道是否有一种方法来操纵虚拟键盘,以这样的方式,“完成”按钮被标记为其他的东西(例如“Go”),并在单击时执行特定的动作(再次,像onSubmit)。
我想知道是否有一种方法来处理用户在输入EditText时按下Enter,就像onSubmit HTML事件。
还想知道是否有一种方法来操纵虚拟键盘,以这样的方式,“完成”按钮被标记为其他的东西(例如“Go”),并在单击时执行特定的动作(再次,像onSubmit)。
当前回答
I had a similar purpose. I wanted to resolve pressing the "Enter" key on the keyboard (which I wanted to customize) in an AutoCompleteTextView which extends TextView. I tried different solutions from above and they seemed to work. BUT I experienced some problems when I switched the input type on my device (Nexus 4 with AOKP ROM) from SwiftKey 3 (where it worked perfectly) to the standard Android keyboard (where instead of handling my code from the listener, a new line was entered after pressing the "Enter" key. It took me a while to handle this problem, but I don't know if it will work under all circumstances no matter which input type you use.
这是我的解决方案:
在xml中设置TextView的输入类型属性为"text":
android:inputType="text"
自定义键盘上“Enter”键的标签:
myTextView.setImeActionLabel("Custom text", KeyEvent.KEYCODE_ENTER);
将OnEditorActionListener设置为TextView:
myTextView.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event)
{
boolean handled = false;
if (event.getAction() == KeyEvent.KEYCODE_ENTER)
{
// Handle pressing "Enter" key here
handled = true;
}
return handled;
}
});
我希望这能帮助其他人避免我遇到的问题,因为它们几乎把我逼疯了。
其他回答
在编辑器中键入这段代码,以便它可以导入必要的模块。
query.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_DONE
|| keyEvent.getAction() == KeyEvent.ACTION_DOWN
|| keyEvent.getAction() == KeyEvent.KEYCODE_ENTER) {
// Put your function here ---!
return true;
}
return false;
}
});
I had a similar purpose. I wanted to resolve pressing the "Enter" key on the keyboard (which I wanted to customize) in an AutoCompleteTextView which extends TextView. I tried different solutions from above and they seemed to work. BUT I experienced some problems when I switched the input type on my device (Nexus 4 with AOKP ROM) from SwiftKey 3 (where it worked perfectly) to the standard Android keyboard (where instead of handling my code from the listener, a new line was entered after pressing the "Enter" key. It took me a while to handle this problem, but I don't know if it will work under all circumstances no matter which input type you use.
这是我的解决方案:
在xml中设置TextView的输入类型属性为"text":
android:inputType="text"
自定义键盘上“Enter”键的标签:
myTextView.setImeActionLabel("Custom text", KeyEvent.KEYCODE_ENTER);
将OnEditorActionListener设置为TextView:
myTextView.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event)
{
boolean handled = false;
if (event.getAction() == KeyEvent.KEYCODE_ENTER)
{
// Handle pressing "Enter" key here
handled = true;
}
return handled;
}
});
我希望这能帮助其他人避免我遇到的问题,因为它们几乎把我逼疯了。
这在LG安卓手机上运行良好。它防止ENTER和其他特殊字符被解释为普通字符。“下一步”或“完成”按钮自动出现,“ENTER”按预期工作。
edit.setInputType(InputType.TYPE_CLASS_TEXT);
作为Chad响应的补充(对我来说几乎完美),我发现我需要在KeyEvent操作类型上添加一个检查,以防止代码执行两次(一次在key-up事件上执行,一次在key-down事件上执行)。
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN)
{
// your code here
}
有关重复动作事件(按住enter键)的信息,请参见http://developer.android.com/reference/android/view/KeyEvent.html。
首先,您必须设置EditText监听按键
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set the EditText listens to key press
EditText edittextproductnumber = (EditText) findViewById(R.id.editTextproductnumber);
edittextproductnumber.setOnKeyListener(this);
}
其次,定义按键时的事件,例如,设置TextView的文本的event:
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
// Listen to "Enter" key press
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
TextView textviewmessage = (TextView) findViewById(R.id.textViewmessage);
textviewmessage.setText("You hit 'Enter' key");
return true;
}
return false;
}
最后,不要忘记在顶部导入EditText,TextView,OnKeyListener,KeyEvent:
import android.view.KeyEvent;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;