2017-05-16 10 views
1

編集テキストの外側に何かが触れられたときに、編集テキストがフォーカスを失うように設定しようとしています。私のonCreateでAndroidの編集テキストは、テキスト表示の外側をクリックするとフォーカスが失われません。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/White" 
android:orientation="vertical" 
android:padding="16dp" 
android:focusable="true" 
android:focusableInTouchMode ="true"> 

<EditText 
    android:id="@+id/editCatTitle" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:hint="Enter name" 
    android:textSize="18dp"/> 

<GridView 
    android:id="@+id/gridViewColor" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="15dp" 
    android:columnWidth="100dp" 
    android:gravity="center" 
    android:numColumns="6" 
    android:stretchMode="columnWidth" 
    android:listSelector="@color/White"></GridView> 

<GridView 
    android:id="@+id/gridViewIcon" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="15dp" 
    android:columnWidth="100dp" 
    android:gravity="center" 
    android:numColumns="6" 
    android:stretchMode="columnWidth" 
    android:listSelector="@color/White"></GridView> 

</LinearLayout> 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.activity_create,container,false); 
    et = (EditText) v.findViewById(R.id.editCatTitle); 
    et.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(!hasFocus){ 
       // lost focus do something here 
      } 
     } 
    }); 
} 

は、私はすでにフォーカス可能であることを最も外側のリニアレイアウトを設定ここに私のレイアウトxmlです。しかし、編集テキストの外側をクリックすると、フォーカスが失われることはありません。何か案は?

私はこれをオンラインで見つけましたが、どこに置くべきかわかりません。ビューのonCreate()の内側にあるのですか?

@Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
    if (mEditText.isFocused()) { 
     Rect outRect = new Rect(); 
     mEditText.getGlobalVisibleRect(outRect); 
     if (!outRect.contains((int)event.getRawX(),(int)event.getRawY())) { 
      mEditText.clearFocus(); 
      // 
      // Hide keyboard 
      // 
      InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
     } 
     } 
    } 
return super.dispatchTouchEvent(event); 
} 

答えて

1

ルートレイアウトのためのタッチリスナーを実装し、これは動作しますが、このコードを試してみてください

public class SampleFragment extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) { 
       View view = inflater.inflate(R.layout.frag_layout, container, false); 
       view.setOnTouchListener(new View.OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         et.clearFocus(); 
         return true; 
        } 
       }); 
       return view; 
     } 

    } 
+0

私は質問を更新しましたが、タッチリスナーを無効にする場所がわかりません – hyperfkcb

+0

あなたのアクティビティで "dispatchTouchEvent"をオーバーライドしてください。 onCreate()をオーバーライドするのと同じように。私の答えを更新しました。 – Mahesh

+0

しかし、それはメソッドがそのスーパークラスからメソッドをオーバーライドしないと私に言った。ちなみに私の断片は断片から伸びています – hyperfkcb

0
FrameLayout touchInterceptor = (FrameLayout)findViewById(R.id.touchInterceptor);touchInterceptor.setOnTouchListener(new OnTouchListener() { 
@Override 
public boolean onTouch(View v, MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     if (mEditText.isFocused()) { 
      Rect outRect = new Rect(); 
      mEditText.getGlobalVisibleRect(outRect); 
      if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) { 
       mEditText.clearFocus(); 
       InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
      } 
     } 
    } 
    return false; 
}}); 

を呼び出します。

関連する問題