input.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if( -1 != input.getText().toString().indexOf( "\n" ) ){
input.setText("Enter was pressed!");
}
}
});
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;
}
});
15
这里's what you do. It'也隐藏在Android Developer 's sample code ' Bluetooth Chat'中 . 用您自己的变量和方法替换说 "example" 的粗体部分 .
import android.view.inputmethod.EditorInfo;
import android.view.KeyEvent;
import android.widget.TextView.OnEditorActionListener;
listener=new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (event==null) {
if (actionId==EditorInfo.IME_ACTION_DONE);
// Capture soft enters in a singleLine EditText that is the last EditText.
else if (actionId==EditorInfo.IME_ACTION_NEXT);
// Capture soft enters in other singleLine EditTexts
else return false; // Let system handle all other null KeyEvents
}
else if (actionId==EditorInfo.IME_NULL) {
// Capture most soft enters in multi-line EditTexts and all hard enters.
// They supply a zero actionId and a valid KeyEvent rather than
// a non-zero actionId and a null event like the previous cases.
if (event.getAction()==KeyEvent.ACTION_DOWN);
// We capture the event when key is first pressed.
else return true; // We consume the event when the key is released.
}
else return false;
// We let the system handle it when the listener
// is triggered by something that wasn't an enter.
// Code from this point on will execute whenever the user
// presses enter in an attached view, regardless of position,
// keyboard, or singleLine status.
if (view==multiLineEditText) multiLineEditText.setText("You pressed enter");
if (view==singleLineEditText) singleLineEditText.setText("You pressed next");
if (view==lastSingleLineEditText) lastSingleLineEditText.setText("You pressed done");
return true; // Consume the event
}
};
这在LG Android手机上运行良好 . 它可以防止 ENTER 和其他特殊字符被解释为普通字符 . Next 或 Done 按钮自动出现, ENTER 按预期工作 .
edit.setInputType(InputType.TYPE_CLASS_TEXT);
3
在EditText中响应<enter>的可靠方法是使用TextWatcher,LocalBroadcastManager和BroadcastReceiver . 您需要添加v4 support library才能使用LocalBroadcastManager . 我在vogella.com:7.3 "Local broadcast events with LocalBroadcastManager"使用教程,因为它的代码完整简洁 . 在onTextChanged before 是更改之前的更改结束的索引>;减去start . 当在TextWatcher中UI线程忙于更新editText的可编辑时,因此当UI线程完成更新editText时,我们发送一个Intent来唤醒BroadcastReceiver .
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.Editable;
//in onCreate:
editText.addTextChangedListener(new TextWatcher() {
public void onTextChanged
(CharSequence s, int start, int before, int count) {
//check if exactly one char was added and it was an <enter>
if (before==0 && count==1 && s.charAt(start)=='\n') {
Intent intent=new Intent("enter")
Integer startInteger=new Integer(start);
intent.putExtra("Start", startInteger.toString()); // Add data
mySendBroadcast(intent);
//in the BroadcastReceiver's onReceive:
int start=Integer.parseInt(intent.getStringExtra("Start"));
editText.getText().replace(start, start+1,""); //remove the <enter>
//respond to the <enter> here
文本字段上的InputType必须是 text 才能满足CommonsWare的工作要求 . 刚尝试了所有这些,在试用之前没有输入类型且没有任何效果,Enter保持注册为软输入 . 在 inputType = text 之后,包括setImeLabel在内的所有内容都有效 .
Example :android:inputType="text"
0
这是一个简单的静态函数,您可以将其放入 Utils 或 Keyboards 类中,当用户点击硬件或软件键盘上的返回键时,该类将执行代码 . 它's a modified version of @earlcasper'的优秀答案
/**
* Return a TextView.OnEditorActionListener that will execute code when an enter is pressed on
* the keyboard.<br>
* <code>
* myTextView.setOnEditorActionListener(Keyboards.onEnterEditorActionListener(new Runnable()->{
* Toast.makeText(context,"Enter Pressed",Toast.LENGTH_SHORT).show();
* }));
* </code>
* @param doOnEnter A Runnable for what to do when the user hits enter
* @return the TextView.OnEditorActionListener
*/
public static TextView.OnEditorActionListener onEnterEditorActionListener(final Runnable doOnEnter){
return (__, actionId, event) -> {
if (event==null) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Capture soft enters in a singleLine EditText that is the last EditText.
doOnEnter.run();
return true;
} else if (actionId==EditorInfo.IME_ACTION_NEXT) {
// Capture soft enters in other singleLine EditTexts
doOnEnter.run();
return true;
} else {
return false; // Let system handle all other null KeyEvents
}
} else if (actionId==EditorInfo.IME_NULL) {
// Capture most soft enters in multi-line EditTexts and all hard enters.
// They supply a zero actionId and a valid KeyEvent rather than
// a non-zero actionId and a null event like the previous cases.
if (event.getAction()==KeyEvent.ACTION_DOWN) {
// We capture the event when key is first pressed.
return true;
} else {
doOnEnter.run();
return true; // We consume the event when the key is released.
}
} else {
// We let the system handle it when the listener
// is triggered by something that wasn't an enter.
return false;
}
};
}
20 回答
XML
这应该工作
是 .
也是的 .
您将需要查看android:imeActionId和android:imeOptions属性以及setOnEditorActionListener()方法,所有这些都在
TextView
上 .要将“完成”按钮的文本更改为自定义字符串,请使用:
这里's what you do. It'也隐藏在Android Developer 's sample code ' Bluetooth Chat'中 . 用您自己的变量和方法替换说 "example" 的粗体部分 .
首先,将您需要的内容导入主Activity,您希望返回按钮执行一些特殊操作:
现在,为你的返回键创建一个TextView.OnEditorActionListener类型的变量(这里我使用 exampleListener );
然后,您需要告诉听众有关按下返回按钮时要执行的操作的两件事 . 它需要知道我们正在谈论的EditText(这里我使用 exampleView ),然后它需要知道当按下Enter键时要做什么(这里, example_confirm() ) . 如果这是Activity中的最后一个或唯一的EditText,它应该与提交(或确定,确认,发送,保存等)按钮的onClick方法相同 .
最后,设置监听器(最有可能在你的onCreate方法中);
硬件键盘总是产生输入事件,但软件键盘在singleLine EditTexts中返回不同的actionID和null . 无论EditText或键盘类型如何,每次用户按下此侦听器已设置的EditText时,此代码都会响应 .
在singleLine = false中输入键的默认外观给出一个弯曲的箭头输入键盘 . 当最后一个EditText中的singleLine = true时,键表示DONE,而在之前的EditTexts上表示NEXT . 默认情况下,此行为在所有vanilla,android和Google模拟器中都是一致的 . scrollHorizontal属性没有任何区别 . 空测试很重要,因为手机对软输入的响应留给了制造商,甚至在模拟器中,vanilla Level 16仿真器响应多线的长软输入和scrollHorizontal EditTexts,其actionId为NEXT,为null事件 .
我知道这已经有一年了,但我发现这对EditText非常有效 .
除了文本和空间之外,它可以防止任我无法标签,“返回”(“\ n”)或任何其他内容 .
就像Chad的回复(对我来说几乎完美无缺)的附录一样,我发现我需要在KeyEvent动作类型上添加一个检查,以防止我的代码执行两次(一次在键盘上,一次在键盘上)事件) .
有关重复操作事件(按住回车键)等信息,请参阅http://developer.android.com/reference/android/view/KeyEvent.html .
working perfectly
}
working perfectly
我有一个类似的目的 . 我想在一个扩展TextView的AutoCompleteTextView中解决键盘上的“Enter”键(我想自定义) . 我从上面尝试了不同的解决方案,它们似乎有效 . 但是当我将设备上的输入类型(带有AOKP ROM的Nexus 4)从SwiftKey 3(它完美地工作)切换到标准的Android键盘时,我遇到了一些问题(而不是从监听器处理我的代码,新的一行是按“Enter”键后输入 . 我花了一段时间来处理这个问题,但我不知道无论你使用哪种输入类型,它都能在所有情况下都能正常工作 .
所以这是我的解决方案:
将xml中TextView的输入类型属性设置为“text”:
自定义键盘上“Enter”键的标签:
将OnEditorActionListener设置为TextView:
我希望这可以帮助别人避免我遇到的问题,因为他们几乎让我疯狂 .
此页面准确描述了如何执行此操作 .
https://developer.android.com/training/keyboard-input/style.html
设置android:imeOptions然后你只需检查onEditorAction中的actionId . 因此,如果您将imeOptions设置为'actionDone',那么您将在onEditorAction中检查'actionId == EditorInfo.IME_ACTION_DONE' . 另外,请确保设置android:inputType .
这是上面链接的示例中的EditText:
您也可以使用setImeOptions(int)函数以编程方式设置它 . 这是上面链接的示例中的OnEditorActionListener:
在xml中,将imeOptions属性添加到editText
然后,在您的Java代码中添加OnEditorActionListener到同一个EditText
以下是解释 - imeOptions = actionDone将“actionDone”分配给EnterKey . 键盘中的EnterKey将从“Enter”更改为“Done” . 因此,当按下Enter键时,它将触发此操作,因此您将处理它 .
你也可以这样做..
对我来说非常好
另外隐藏键盘
首先,你必须设置EditText听取按键
其次,在按键时定义事件,例如,设置TextView文本的事件:
最后,不要忘记在顶部导入EditText,TextView,OnKeyListener,KeyEvent:
这在LG Android手机上运行良好 . 它可以防止
ENTER
和其他特殊字符被解释为普通字符 .Next
或Done
按钮自动出现,ENTER
按预期工作 .在EditText中响应<enter>的可靠方法是使用TextWatcher,LocalBroadcastManager和BroadcastReceiver . 您需要添加v4 support library才能使用LocalBroadcastManager . 我在vogella.com:7.3 "Local broadcast events with LocalBroadcastManager"使用教程,因为它的代码完整简洁 . 在onTextChanged before 是更改之前的更改结束的索引>;减去start . 当在TextWatcher中UI线程忙于更新editText的可编辑时,因此当UI线程完成更新editText时,我们发送一个Intent来唤醒BroadcastReceiver .
Jared Law的回答对我来说就像是一种魅力 .
刚刚添加了这些依赖性:
文本字段上的InputType必须是
text
才能满足CommonsWare的工作要求 . 刚尝试了所有这些,在试用之前没有输入类型且没有任何效果,Enter保持注册为软输入 . 在inputType = text
之后,包括setImeLabel在内的所有内容都有效 .Example :
android:inputType="text"
这是一个简单的静态函数,您可以将其放入
Utils
或Keyboards
类中,当用户点击硬件或软件键盘上的返回键时,该类将执行代码 . 它's a modified version of @earlcasper'的优秀答案