1

編集テキストのダイアログがあります。ユーザーがキーパッドで[完了]をクリックしているときにアクションを実行したい。私のコードは次のようになります

私の編集テキストxmlは次のようになります

<EditText
            android:id="@+id/commondialog_userinput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:imeOptions="actionDone"
            android:selectAllOnFocus="true"
            android:inputType="text" />

追加されたリスナーは

final EditText inputField = (EditText)dialog.findViewById(R.id.commondialog_userinput);
            inputField.setInputType(EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
            inputField.setText(AndroidGlobalVariables.getDocumentName(), TextView.BufferType.EDITABLE);// No I18N
            inputField.setFocusableInTouchMode(true);
            inputField.requestFocus();
            inputField.selectAll();

        inputField.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    Toast.makeText(EditorActivity.getActivity(), inputField.getText(),Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }

        }); 
4

3 に答える 3

2

次のようにキーパッドで実行してアクションを実行するEditTextに対してsetOnEditorActionListenerを使用します。

 your_editText.setOnEditorActionListener(new OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE)
                {
                      ** PUT YOUR ACTION HERE !!! **
                }
        return false;
    }
});
于 2013-03-27T10:28:36.180 に答える
0

必要なのはhttp://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_ACTION_DONEです。Androidの実装とバージョンが異なれば、使用するキーも異なる可能性があり、「Enter」キーコードに依存することはできません。

于 2013-03-27T10:27:00.920 に答える
0

これをテキストリスナーの編集に使用します

e_inputField .setOnEditorActionListener(new OnEditorActionListener() {

                public boolean onEditorAction(TextView arg0,
                        int actionId, KeyEvent arg2) {
                    // TODO Auto-generated method stub

                    if (actionId == EditorInfo.IME_ACTION_NEXT) {
                        //do your stuff here

                    }
                    return false;
                }
            });
于 2013-03-27T10:33:54.540 に答える