• 对于EditView软键盘的控制


    package com.example.test_soft_input;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.EditText;
    
    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        /*
         * 屏幕点击判断是否隐藏软键盘。
         * 
         * @see android.app.Activity#dispatchTouchEvent(android.view.MotionEvent)
         */
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                View v = getCurrentFocus();
                if (isShouldHideInput(v, ev)) {
                    hideSoftInput(v.getWindowToken());
                }
            }
            return super.dispatchTouchEvent(ev);
        }
    
        /**
         * 判断是否需要隐藏键盘,若点击EditText之外的区域,则表示需要隐藏键盘
         * 
         * @param v
         * @param event
         * @return
         */
        public boolean isShouldHideInput(View v, MotionEvent event) {
            if (v != null && v instanceof EditText) {
                int[] location = { 0, 0 };
                v.getLocationInWindow(location);
                int left = location[0];
                int top = location[1];
                int bottom = top + v.getHeight();
                int right = left + v.getWidth();
                if (event.getX() > left && event.getX() < right
                        && event.getY() > top && event.getY() < bottom) {
                    return false;
                } else {
                    return true;
                }
            }
            return false;
        }
    
        /**
         * 隐藏软键盘
         * 
         * @param token
         */
        public void hideSoftInput(IBinder token) {
            if (token != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(token,
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
    
        }
    
    }
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码..." />
    
    </RelativeLayout>
  • 相关阅读:
    程序的编码问题
    man DMIDECODE
    Github熟悉一
    man uname
    第一轮铁大树洞APP开发冲刺(2)
    第九周学习进度
    第九周安卓开发学习总结(3)
    第一轮铁大树洞APP开发冲刺(1)
    第九周安卓开发学习总结(2)
    第九周安卓开发学习总结(1)
  • 原文地址:https://www.cnblogs.com/zjc0514/p/4501029.html
Copyright © 2020-2023  润新知