我已经使用了一些应用程序,当我填写用户名,然后转到我的密码,如果我在键盘上点击"完成",登录表单会自动提交,而不必单击提交按钮.这是怎么做到的?
Har*_*ran 174
试试这个:
在你的布局中输入/编辑:
<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" />
Run Code Online (Sandbox Code Playgroud)
在你的活动中把这个(例如在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;
}
});
Run Code Online (Sandbox Code Playgroud)
submit_btn
附带onclick处理程序的提交按钮在哪里.
flx*_*flx 25
您需要在您的上设置IME选项EditText
.
<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" />
Run Code Online (Sandbox Code Playgroud)
然后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;
}
});
Run Code Online (Sandbox Code Playgroud)
官方API文档:https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent
Fra*_*llo 19
延伸EditText
:
fun EditText.onSubmit(func: () -> Unit) {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
func()
}
true
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用这样的新方法:
editText.onSubmit { submit() }
Run Code Online (Sandbox Code Playgroud)
哪里submit()
是这样的:
fun submit() {
// call to api
}
Run Code Online (Sandbox Code Playgroud)
fun EditText.on(actionId: Int, func: () -> Unit) {
setOnEditorActionListener { _, receivedActionId, _ ->
if (actionId == receivedActionId) {
func()
}
true
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以用它来听你的活动:
email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })
Run Code Online (Sandbox Code Playgroud)
这就是它的完成方式
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do something
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
别忘了添加
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:imeOptions="actionDone"/>
Run Code Online (Sandbox Code Playgroud)
EditText中的actionDone.
归档时间: |
|
查看次数: |
53186 次 |
最近记录: |