zoukankan      html  css  js  c++  java
  • 对于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>
  • 相关阅读:
    Silverlight/Windows8/WPF/WP7/HTML5周学习导读(8月20日8月26日)
    Silverlight/Windows8/WPF/WP7/HTML5周学习导读(9月24日9月30日)
    获取免费Windows Store开发者账户方法
    QOCIDriver: unable to create environment Unable to free Error handle: 2 Unable to free Environment
    C:\workdir\dbManager\lib>c:\Qt\Qt5.9.9\5.9.9\mingw53_32\bin\windeployqt.exe ./db ManagerDll.dll
    QT5.9.932 oracle1032 驱动编译
    sqlplus
    c++const成员函数*
    C++ 函数内静态静态变量
    c++构造/拷贝构造函数初始化变量*
  • 原文地址:https://www.cnblogs.com/zjc0514/p/4501029.html
Copyright © 2011-2022 走看看