Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法处理android edittext中的Enter_Android_Event Handling_Android Edittext - Fatal编程技术网

无法处理android edittext中的Enter

无法处理android edittext中的Enter,android,event-handling,android-edittext,Android,Event Handling,Android Edittext,我有多个文本框。他们每个人都有一个TextWatcher。现在我想为最后一个强制文本框设置一个OnKeyListener,这样我就可以处理Enter键了。当在其文本框中按Enter键时,我想将此文本框的文本设置为默认值,然后我想保存所有文本框中的数据,清除所有文本框并将焦点设置为第一个文本框,以便用户可以输入新数据。但是,显然,我无法有效地将注意力集中在TextBox.onkeylister的onKey事件处理程序中的任何其他TextBox或按钮上 myLastTextBox.setOnKeyL

我有多个文本框。他们每个人都有一个TextWatcher。现在我想为最后一个强制文本框设置一个OnKeyListener,这样我就可以处理Enter键了。当在其文本框中按Enter键时,我想将此文本框的文本设置为默认值,然后我想保存所有文本框中的数据,清除所有文本框并将焦点设置为第一个文本框,以便用户可以输入新数据。但是,显然,我无法有效地将注意力集中在TextBox.onkeylister的onKey事件处理程序中的任何其他TextBox或按钮上

myLastTextBox.setOnKeyListener( new EditText.OnKeyListener() {  
    @Override
    public boolean onKey(View arg0, int arg1, KeyEvent event) {
        if (event!= null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
            Button  nextB = (Button) findViewById(R.id.myBtn);
            nextB.performClick(); //the function called validates all fields, 
                                  //saves the data to lists, sets all editText 
                                  //to "", requests focus to the first editText.                
            res= true;
        }
        else if (event!= null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER &&  event.getAction() == KeyEvent.ACTION_UP) {
            res=true; //consume also the keyUp event of the enter key.
        }
        return res;
    }
});
当程序从onKey事件处理程序返回时,焦点仍在同一文本框中。谢谢你的帮助

编辑:
好的,我想简化这个问题,但也许我把它做得太简单了。按下Enter键时,我不仅要设置焦点,还要调用一个函数,该函数读取、保存、清除所有文本框,然后将焦点设置为first editText。

因此,您应该查看IME操作。假设您有一个包含三个
EditText
视图的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <EditText
        android:id="@+id/username"
        android:hint="@string/username_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nextFocusDown="@+id/password"
        android:imeOptions="actionNext" />

    <EditText
        android:id="@+id/password"
        android:hint="@string/password_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nextFocusDown="@+id/confirm_password"
        android:inputType="text|textPassword"
        android:imeOptions="actionNext" />

    <EditText
        android:id="@+id/confirm_password"
        android:hint="@string/confirm_password_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text|textPassword"
        android:imeOptions="actionGo" />

</LinearLayout>

您是否尝试过在XML布局中设置下一个焦点键?我想它可能会做你想做的事谢谢,试过了,但在这种情况下不起作用。非常感谢。我将深入研究IME的操作,并在晚上尝试一下。如果有人能指出是什么概念阻止在onKeyListener的onKey事件中将焦点设置为另一个元素,我会很高兴。非常感谢,这解决了我的问题,甚至Enter按钮的文本也变得更可读。
final EditText username = (EditText) findViewById(R.id.username);
final EditText password = (EditText) findViewById(R.id.password);
final EditText confirm = (EditText) findViewById(R.id.confirm_password);

confirm.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (EditorInfo.IME_ACTION_GO == actionId) {
            String un = username.getText().toString();
            String pass = password.getText().toString();
            String conf = confirm.getText().toString();

            username.setText(null);
            password.setText(null);
            confirm.setText(null);

            /* Save your stuff */
            // ...

            username.requestFocus();
            return true;
        } else {
            return false;
        }
    }
});