【问题标题】:Android execute action on enter press in edit textAndroid在编辑文本中输入按时执行操作
【发布时间】:2014-04-13 09:33:30
【问题描述】:

我正在尝试执行一个操作,一个搜索,在用户输入编辑文本时按下回车。它不工作。代码是:

edtSearch.setOnKeyListener(new OnKeyListener(){
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER){
                    System.out.println("test");
                    return true;
                }
                return false;
            }}); 

编辑文本的 XML,

<EditText
            android:id="@+id/edtSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >
            <requestFocus />
        </EditText>

知道为什么它不起作用。

【问题讨论】:

    标签: android android-edittext keypress


    【解决方案1】:

    您应该设置输入类型和 IME 选项,例如:

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:imeOptions="actionDone">`
    

    在您的活动中将 OnEditorActionListener 设置为 EditText:

    edit.setOnEditorActionListener(new OnEditorActionListener() {
    
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if(actionId==EditorInfo.IME_ACTION_DONE){
                        Log.i(TAG,v.getText().toString());
                    }
                    return false;
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多