• android之键盘转载


    显示键盘:
    EditText editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);
    editText.requestFocus();
    InputMethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.showSoftInput(editText, 0);

    首先要对指定的输入框请求焦点。然后调用输入管理器弹出软键盘。

    警告:对于刚跳到一个新的界面就要弹出软键盘的情况上述代码可能由于界面为加载完全而无法弹出软键盘。此时应该适当的延迟弹出软键盘如998毫秒(保证界面的数据加载完成)。实例
    [代码]java代码:

    Timer timer = new Timer();
    timer.schedule(new TimerTask(){
    public void run(){
    InputMethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.showSoftInput(editText, 0);
    }
    }, 998);

    隐藏键盘:

    方法二:
    让EditText失去焦点,使用EditText的clearFocus方法
    例如:EditText edit=(EditText)findViewById(R.id.edit);
    edit.clearFocus();
    方法三:
    强制隐藏Android输入法窗口
    例如:EditText edit=(EditText)findViewById(R.id.edit);
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
    警告:对于刚跳到一个新的界面就要弹出软键盘的情况上述代码可能由于界面为加载完全而无法关闭软键盘。此时应该适当的延迟弹出软键盘如998毫秒
    Timer timer = new Timer();
    timer.schedule(new TimerTask(){
    public void run(){
    EditText edit=(EditText)findViewById(R.id.edit);
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
    }
    }, 998);
    方法四:EditText始终不弹出软件键盘
    例:EditText edit=(EditText)findViewById(R.id.edit);
    edit.setInputType(InputType.TYPE_NULL);

  • 相关阅读:
    Java读取压缩文件信息
    中国菜刀 一句话木马
    java中equals方法和hashcode方法的区别和联系,以及为什么要重写这两个方法,不重写会怎样
    Hadoop搭建全程
    instr模糊查询使用及注意事项
    idea离线安装SonarLint
    Previous operation has not finished; run 'cleanup' if it was interrupted] 的排错过程
    vue中excel文件上传总结
    IDEA 2019.1离线安装lombok
    @Data注解失效解决办法
  • 原文地址:https://www.cnblogs.com/wcLT/p/7698976.html
Copyright © 2020-2023  润新知