zoukankan      html  css  js  c++  java
  • 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);

    转自

  • 相关阅读:
    PeaZip 4.7.3 发布,跨平台压缩工具
    Liferea 1.8.10 发布,Linux的RSS阅读
    PyParticles 0.2.1 发布,粒子模拟工具箱
    微软公布 Windows Phone 8 多项新特性
    SecureCRT 7.0.2 发布,支持 Windows 8 系统
    Qore PostgreSQL Module 2.0 发布
    libquickmail 0.1.6 发布,邮件发送包
    Mobile Lua 6.5 发布,MoSync 的 Lua 移植版本
    企业用户缘何抓住 Windows XP 不放
    Knockout.js 2.2 发布,JavaScript UI 库
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/6397762.html
Copyright © 2011-2022 走看看