android EditText处理焦点问题和键盘收起问题

android EditText处理焦点问题和键盘收起问题

android经常需要用到搜索框,一个EditText和一个搜索按钮,本文介绍的是处理EditText焦点和键盘收起问题。为了方便起见,所有函数都写在MainActivity中了。主要达到效果:进入界面后EditText默认不获取焦点,点击后才获取。点击搜索按钮后EditText失去焦点,开始搜索,收起软键盘。

  1. 主要是写了三个功能函数,dispatchTouchEvent函数根据点击位置判断是否需要隐藏键盘,IsShouldHideKeyboard是是否需要隐藏键盘的实现,HideKeyboard函数实现键盘的隐藏。
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText editText=findViewById(R.id.editText);
        
        Button search=findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"开始搜索",Toast.LENGTH_SHORT).show();
                editText.clearFocus();
                HideKeyboard(editText);
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN) {
            View currentFocus = getCurrentFocus();
            if(IsShouldHideKeyboard(currentFocus,ev)) {
                HideKeyboard(currentFocus);
            }
        }
        return super.dispatchTouchEvent(ev);
    }


    private boolean IsShouldHideKeyboard(View view, MotionEvent event) {
        if((view instanceof EditText)) {
            int[] coord = {0,0};
            view.getLocationInWindow(coord);
            int left = coord[0], top = coord[1], right = left + view.getWidth(), bottom = top + view.getHeight();
            int evX = (int) event.getRawX(), evY = (int) event.getRawY();
            return !((left <= evX && evX <= right) && (top <= evY && evY <= bottom));
        }
        return false;
    }

    public  void HideKeyboard(View v) {
        InputMethodManager manager = ((InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE));

        if (manager != null)
            manager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        v.clearFocus();
    }
}

2.对应的activity_main.xml只是简单的布局一个EditText和一个Button。注意,EditText默认获取焦点,为了达到EditText点击后才获取到焦点,我在外层布局加了三行代码,默认外层布局获取焦点,以达到想要的效果。

		android:focusable="true"
        android:clickable="true"
        android:focusableInTouchMode="true"

总的布局文件如下:


<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:clickable="true"
        android:focusableInTouchMode="true"
        tools:context=".MainActivity">


    <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="点击搜索"
            android:ems="10"
            android:id="@+id/editText" android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="16dp"
            app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@+id/search"
            android:layout_marginEnd="8dp"/>
    <Button
            android:text="搜索"
            android:layout_width="88dp"
            android:layout_height="wrap_content"
            android:id="@+id/search"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"/>
android.support.constraint.ConstraintLayout>

你可能感兴趣的:(android)