我已经使用了一些应用程序,当我填写用户名时,然后转到我的密码,如果我在键盘上点击“完成”,则会自动提交登录表单,而无需单击提交按钮。这是怎么做到的?
答案 0 :(得分:151)
试试这个:
在你的布局中输入/编辑它:
<EditText
android:id="@+id/search_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionDone" />
在你的活动中把这个(例如on onCreate):
// your text box
EditText edit_txt = (EditText) findViewById(R.id.search_edit);
edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
submit_btn.performClick();
return true;
}
return false;
}
});
submit_btn
是您的提交按钮,其中附带了onclick处理程序。
答案 1 :(得分:25)
您需要在EditText
上设置IME选项。
<EditText
android:id="@+id/some_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Whatever"
android:inputType="text"
android:imeOptions="actionDone" />
然后在视图中添加OnEditorActionListener
以收听“完成”操作。
EditText editText = (EditText) findViewById(R.id.some_view);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
// TODO do something
handled = true;
}
return handled;
}
});
官方API文档:https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent
答案 2 :(得分:16)
扩展EditText
:
fun EditText.onSubmit(func: () -> Unit) {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
func()
}
true
}
}
然后使用这样的新方法:
editText.onSubmit { submit() }
其中submit()
是这样的:
fun submit() {
// call to api
}
fun EditText.on(actionId: Int, func: () -> Unit) {
setOnEditorActionListener { _, receivedActionId, _ ->
if (actionId == receivedActionId) {
func()
}
true
}
}
然后你可以用它来听你的活动:
email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })
答案 3 :(得分:6)
这就是它的完成方式
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do something
}
return false;
}
});
不要忘记添加
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:imeOptions="actionDone"/>
EditText中的actionDone 。
答案 4 :(得分:2)
在您的edittext标记内的XML文件中添加以下代码段
android:imeOptions="actionDone"
然后在Java类中编写以下代码
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int id, KeyEvent event) {
if (id == EditorInfo.IME_ACTION_DONE) {
//do your work here
return true;
}
return false;
}
});
答案 5 :(得分:1)
在edittext中添加以下行
android:imeOptions="actionDone"
快乐编码
答案 6 :(得分:1)
await-ing
答案 7 :(得分:1)
只需扩展this answer
fun EditText.onSubmit(func: () -> Unit) {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
clearFocus() // if needed
hideKeyboard()
func()
}
true
}
}
fun EditText.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.windowToken, 0)
}
答案 8 :(得分:0)
<EditText
android:id="@+id/signinscr_userName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/userName"
android:imeOptions="actionNext" />
<EditText
android:id="@+id/signinscr_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:imeOptions="actionDone"
android:inputType="textPassword" />
<。>在.java文件中
EditText userNameField = (EditText) findViewById(R.id.signinscr_userName);
EditText passwordField = (EditText) findViewById(R.id.signinscr_password);
passwordField.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
//Do your operation here.
return false;
}
});
答案 9 :(得分:0)
EditText edit_txt = (EditText) findViewById(R.id.search_edit);
edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// which is u had set a imeoption
if (actionId == EditorInfo.IME_ACTION_DONE) {
submit_btn.performClick();
return true;
}
return false;
}
});