###前提・実現したいこと
AndroidStudioで、EditTextのエンターキー入力時の処理を指定したいので、xmlでedittextにimeOptionsを指定してjava側でsetOnEditorActionListenerを使って押された後の処理を書いているのですが、エンターが押されてもうまく処理が行われません。押された判定を取得できていないように思います。
###該当のソースコード
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.root.myapplication.MainActivity"> 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="Hello World!" 13 app:layout_constraintBottom_toBottomOf="parent" 14 app:layout_constraintLeft_toLeftOf="parent" 15 app:layout_constraintRight_toRightOf="parent" 16 app:layout_constraintTop_toTopOf="parent" /> 17 18 <EditText 19 android:id="@+id/editText" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:ems="10" 23 android:inputType="text" 24 android:text="Name" 25 app:layout_constraintBottom_toBottomOf="parent" 26 app:layout_constraintHorizontal_bias="0.502" 27 app:layout_constraintLeft_toLeftOf="parent" 28 app:layout_constraintRight_toRightOf="parent" 29 app:layout_constraintTop_toTopOf="parent" 30 app:layout_constraintVertical_bias="0.189" 31 android:imeOptions="actionNone"/> 32 33</android.support.constraint.ConstraintLayout>
java
1package com.example.root.myapplication; 2 3import android.support.v7.app.AppCompatActivity; 4import android.os.Bundle; 5import android.util.Log; 6import android.view.KeyEvent; 7import android.view.inputmethod.EditorInfo; 8import android.widget.EditText; 9import android.widget.TextView; 10 11public class MainActivity extends AppCompatActivity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 EditText editText = (EditText)findViewById(R.id.editText); 18 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 19 @Override 20 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 21 if (actionId == EditorInfo.IME_ACTION_NONE) { 22 Log.d("", "true"); 23 return true; 24 } 25 Log.d("", "false"); 26 return false; 27 } 28 }); 29 } 30} 31
Logにはtrueもfalseも出ません。
###試したこと
ActionNoneをSearchやDoneに変えてもダメでした。
xmlの、
android:imeOptions="actionNone"
この一文を消して、java側で
if (actionId == EditorInfo.IME_NULL) {
と変更したら、Logでfalseが出ましたが、falseでは意味がないです...
原因がわかる方いらっしゃいましたら回答をお願いいたします。
あなたの回答
tips
プレビュー