###前提・実現したいこと
ナビゲーションドロワーとフラグメントの組み合わせでアプリを開発しています。
フラグメントの画面にEditTextを置き、フォーカスがある場合にはIMEを表示し、フォーカスが外れた場合はIMEを非表示にしており、ナビゲーションドロワーを開いたときにもIMEを非表示にしたいと考えています。
###発生している問題・エラーメッセージ
フォーカスが外れたかどうかは OnFocusChangeListener() でキャッチしているのですが、ナビゲーションドロワーを開いても onFocusChange のイベントが発生せず(※)、IMEを非表示にすることができません。
※AndroidStudioのデバッグモードでリスナー内にブレークポイントを設定して確認。
###該当のソースコード
xml
1<!-- 2test_fragment.xml 3--> 4<?xml version="1.0" encoding="utf-8"?> 5<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 6 android:orientation="vertical" android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 <EditText 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:inputType="textImeMultiLine" 12 android:imeOptions="actionGo" 13 android:selectAllOnFocus="true" 14 android:id="@+id/edtext" 15 android:hint="テキストを入力" 16 > 17 <requestFocus/> 18 </EditText> 19</LinearLayout>
java
1/** 2TestFragment.java 3**/ 4 public class TestFragment extends Fragment { 5 // 以下、onStart()まで省略 6 @Override 7 public void onStart() { 8 super.onStart(); 9 10 // IMEをON 11 EditText edText = (EditText)getView().findViewById(R.id.edtext); 12 ((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(edText, 0); 13 14 // リスナー:フォーカスを失った場合 15 edText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 16 @Override 17 public void onFocusChange(View v, boolean hasFocus) { 18 if (hasFocus) { 19 // Focus on 20 } else { 21 // Focus off 22 ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0); 23 } 24 } 25 }); 26 } 27}
java
1/** 2MainActivity.java 3**/ 4public class MainActivity extends AppCompatActivity 5 implements NavigationView.OnNavigationItemSelectedListener { 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 // 以下、フラグメント画面表示まで省略 11 FragmentManager fragmentManager = getSupportFragmentManager(); 12 FragmentTransaction sc = fragmentManager.beginTransaction(); 13 TestFragment test_fragment = new TestFragment(); 14 sc = fragmentManager.beginTransaction(); 15 sc.replace(R.id.container, test_fragment, "test_fragment"); 16 sc.commit(); 17 } 18//以下、省略
/** build.gradle(Project: NaviDrawAndFocusTest1) **/ buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
/** build.gradle(Module: app) **/ apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.hogehoge.navidrawandfocustest1" minSdkVersion 16 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:design:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' }
/** gradle-warapper.properties **/ #Tue Apr 11 11:25:21 JST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
###試したこと
(1)上記の再現確認用ソースコードによって本件再現を確かめました。
(2)ナビゲーションドロワーのitemを選択して他のフラグメント画面に遷移した場合にはonFocusChangeイベントが発生することも確認しました。
(3)開発中のアプリのプロト版をAndroidStudio2.2.xで開発したのですが、その際には本件は発生せず、同プロジェクトをAndroidStudio2.3.1環境でリビルド&実行しても本件は発生しませんでした。
(4)AndroidStudio2.3.1環境で全く同じ名称のプロジェクトを生成して、(3)のプロジェクトからソースコード(Javaとレイアウトのxml)を変更を加えずに移植してみたところ、本件が発生しました。
以上のことから、AndroidStudioかSDKの版の違いの影響や仕様変更かとも考え、ネットで検索して見ましたが、該当する事例は発見できませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
発生時の環境は下記です。
IDE:AndroidStudio2.3.1
minimun API:API16 を指定
テンプレート:Navigation Drawer Activity
以上、よろしくお願いいたします。