OnClick for Android" arrow" EditText键盘上的按钮

时间:2015-11-19 03:30:12

标签: android onclick onclicklistener

我已经搜索过,我似乎无法抓住"箭头"在Android EditText键盘上的键。对我来说,它是退格键下方的按钮,看起来像箭头。

我尝试过使用:

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


            System.out.println(actionId);

            if (event.getAction() != KeyEvent.ACTION_DOWN) {
                System.out.println("event.getAction() != KeyEvent.ACTION_DOWN");
                return false;
            }

            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                System.out.println("keyCode == KeyEvent.FLAG_EDITOR_ACTION");

                trialNumber++;
                trialNumberTextView.setText("Trial" + Integer.toString(trialNumber));

                return true;
            }

            System.out.println("false");

            return false;
        }

    });

的EditText:

<EditText
    android:id="@+id/sack_edit_text"
    android:textSize="30dp"
    android:textAlignment="center"
    android:hint="@string/trial_hint"
    android:layout_centerInParent="true"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:digits="0123456789."
    android:maxLength="2"/>

但是当我点击箭头按钮时,根本没有打印出任何内容。这适用于键盘上的数字。我已经尝试将EditText xml中的箭头按钮更改为Done按钮,但这也不起作用。有谁知道这是为什么?

2 个答案:

答案 0 :(得分:0)

在您的布局中,只需设置XML属性android:imeOptions="actionNext"即可。您需要在xml中将edittext单行设为true。

android:singleLine="true"

并在键盘上捕捉下一个动作,如:

 some_edittext.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                some_button.performClick();
                return true;
            }
            return false;
        }
    });

另请查看This So post以获取有关软键盘操作的详细说明。

答案 1 :(得分:0)

android:imeOptions="actionNext"添加到XML格式的EditText中。

接下来,在你的编辑文字上设置OnEditorActionListener,如下所示,

etSack.setOnEditorActionListener(new TextView.OnEditorActionListener() {

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

            ...your code...

            if (actionId == EditorInfo.IME_ACTION_NEXT) {
              System.out.println("keyCode == KeyEvent.FLAG_EDITOR_ACTION");

              trialNumber++; 
              trialNumberTextView.setText("Trial" +        Integer.toString(trialNumber));

              return true; 
            } 

            System.out.println("false");
            return false;
        }

    });
相关问题