在文本视图外单击时,Android编辑文本不会失去焦点

时间:2017-05-16 03:46:29

标签: java android xml

我想在触摸编辑文本之外的任何内容时设置编辑文本丢失焦点。这是我的布局xml:

<?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>

在我的onCreate:

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
            }
        }
    });
}

我将最外面的线性布局设置为可聚焦。但是,当我在编辑文本外单击时,它仍然不会失去焦点。有什么想法吗?

我在网上找到了这个,但我不知道我应该把它放在哪里。它是在视图的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);
 }

2 个答案:

答案 0 :(得分:2)

为您的根布局实现触摸侦听器并调用

    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;
        }

    }

答案 1 :(得分: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;
}});

尝试此代码,这将有效。