最後のEditTextでキーボードのDoneを押した後の暗黙の「送信」


96

ユーザー名を入力してパスワードにアクセスするアプリをいくつか使用しました。キーボードで[完了]を押すと、ログインフォームが自動的に送信され、送信ボタンをクリックする必要はありません。これはどのように行われますか?



ドキュメントへのクイックリンク:インプットメソッドアクションの指定
FirstOne

回答:


185

これを試して:

あなたのレイアウトにこれを入れて/編集してください:

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

あなたの活動にこれを入れてください(例えばonCreateに):

 // your text box
 EditText edit_txt = (EditText) findViewById(R.id.search_edit);

 edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
         if (actionId == EditorInfo.IME_ACTION_DONE) {
             submit_btn.performClick();
             return true;
         }
         return false;
     }
 });

submit_btnonclickハンドラーがアタッチされた送信ボタンはどこにありますか。


15
submit_btn.performClick();私の目が焼けています。Srsly?submitメソッドを呼び出さないのはなぜですか?
Laurent Meyer

28
@LaurentMeyerユーザー入力のシミュレーションは、通常、これらの状況で基本となるロジックを直接呼び出すよりも優れています。たとえば、送信ボタンが現在無効になっている可能性があるため、performClick()は(意図したとおりに)何もしませんが、submitメソッドを直接呼び出した場合は、ボタンが最初に無効になっていないことを確認する必要があります。ボタンがタップされたかのように「クリック」音も再生されます
Extragorey

3
@LaurentMeyer UIセンシティブとはどういう意味ですか?確かに、過去6か月間に5人です。彼らに時間を与えてください、そうすれば人々も私に同意するでしょう。;)
Extragorey 2017年

ボタンを別の目的で使用するUIを変更するとします。コードは実際の混乱状態になり、さらに悪いことに、この種のバグを検出するには、非常に広範なテスト手順が必要になります。さらに悪いのは、UIコンポーネントをそのようなプラクティスと共有する場合です。
Laurent Meyer

1
TWIMC、imeActionLabel私のEditTextで使用すると、このすべての動作が無効になっていました。気をつけて
アルウィンケスラー2018年

25

でIMEオプションを設定する必要がありますEditText

<EditText
    android:id="@+id/some_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Whatever"
    android:inputType="text"
    android:imeOptions="actionDone" />

次にOnEditorActionListener、ビューにを追加して、「完了」アクションをリッスンします。

EditText editText = (EditText) findViewById(R.id.some_view);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // TODO do something
            handled = true;
        }
        return handled;
    }
});

公式APIドキュメント:https : //developer.android.com/guide/topics/ui/controls/text.html#ActionEvent


22

Kotlinによるシンプルで効果的なソリューション

延長EditText

fun EditText.onSubmit(func: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->

       if (actionId == EditorInfo.IME_ACTION_DONE) {
           func()
       }

       true

    }
}

次に、次のような新しいメソッドを使用します。

editText.onSubmit { submit() }

submit()このようなものはどこにありますか:

fun submit() {
    // call to api
}

より一般的な拡張

fun EditText.on(actionId: Int, func: () -> Unit) {
    setOnEditorActionListener { _, receivedActionId, _ ->

       if (actionId == receivedActionId) {
           func()
       }

        true
    }
}

そして、それを使ってイベントを聞くことができます:

email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })

6

これがそのやり方です

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //do something
        }
    return false;
    }
});

追加することを忘れないでください

<EditText android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:imeOptions="actionDone"/>

EditTextの actionDone。


2

edittextタグ内のXMLファイルで、以下のスニペットを追加します

android:imeOptions="actionDone"

次に、Javaクラス内で、以下のコードを記述します

editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { 


@Override 
  public boolean onEditorAction(TextView v, int id, KeyEvent event) { 
   if (id == EditorInfo.IME_ACTION_DONE) { 
      //do your work here 
      return true;
    } 

        return false; 
   } 
  });

1

edittextに次の行を追加します

android:imeOptions="actionDone"

ハッピーコーディング


1
etParola = (EditText) findViewById(R.id.etParola); 
 btnGiris = (Button) findViewById(R.id.btnGiris);
  etParola.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    btnGiris.performClick();
                    return true;
                }
                return false;
            }
        });

 and;


layout xml etParola
android:imeOptions="actionDone" add

これとまったく同じ答え、この1。これがOPの問題をどのように解決するかを少し説明する必要があります。
エイドリアンW

1

この答えを拡張するだけです

fun EditText.onSubmit(func: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            clearFocus() // if needed 
            hideKeyboard()
            func()
        }
        true
    }
}

fun EditText.hideKeyboard() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.windowToken, 0)
}

0
<EditText
        android:id="@+id/signinscr_userName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/userName"
        android:imeOptions="actionNext" />

    <EditText
        android:id="@+id/signinscr_password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/password"
        android:imeOptions="actionDone"
        android:inputType="textPassword" />

.javaファイル内

EditText userNameField = (EditText) findViewById(R.id.signinscr_userName);
    EditText passwordField = (EditText) findViewById(R.id.signinscr_password);
    passwordField.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
            //Do your operation here.
            return false;
        }
    });

0
 EditText edit_txt = (EditText) findViewById(R.id.search_edit);

 edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// which is u had set a imeoption
         if (actionId == EditorInfo.IME_ACTION_DONE) {
             submit_btn.performClick();
             return true;
         }
         return false;
     }
 });
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.