处理输入按钮软键盘

时间:2014-05-26 10:35:59

标签: android

我试图处理Android软键盘上的Enter按钮。我尝试了它在this post中从StackOverflow中所说的内容,但没有运气。

我有两个EditText字段:一个用于询问用户名,另一个用于用户电子邮件。完成用户名后,我想执行Next操作,然后转到下一个EditText

这是我的.xml文件:

<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
              android:id="@+id/text_user_name" android:layout_marginTop="20dp"
              android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
              android:layout_weight="1"
              android:imeOptions="actionNext"
              android:inputType="text"
              android:nextFocusDown="@+id/text_user_email"
              android:hint="name"/>
    <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@id/text_user_email"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:imeOptions="actionDone"
                android:hint="***@email.com"/>

这是我的OnEditorActionListener

return new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId == EditorInfo.IME_ACTION_DONE && event.getKeyCode() == KeyEvent.KEYCODE_ENTER){

                if (!finished)
                {
                    // Check user information
                    if (CheckUserInfo()) finished = true;
                }
                return true;
            }
            else
            if (actionId == EditorInfo.IME_ACTION_NEXT && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                Toast.makeText(getApplicationContext(), "go a line down", Toast.LENGTH_SHORT).show();
                return true;
            }
            else
            {
                return false;
            }
        }
    };

我还尝试更改Enter按钮的可见文字,对于第一个Next看起来像EditText,为最后{Done看起来像EditText 1}}像这样:

textUserName.setImeActionLabel("Next", KeyEvent.KEYCODE_ENTER);
.... 
textUserEmail.setImeActionLabel("Done", KeyEvent.KEYCODE_ENTER);

但这部分也没有用。

任何人都可以帮助我理解为什么我无法做任何事情吗? :_(谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用

     android:imeActionLabel="your text here"
     android:imeOptions="actionNext"

下一次imeoption

    android:imeActionLabel="your text here"
    android:imeOptions="actionSend"

完成按钮

答案 1 :(得分:0)

你也可以这样做

editText.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

             if (actionId == KeyEvent.KEYCODE_ENTER) { 
                 doSomeThing();

             }


            return false;
        }
    });