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);

    转自

  • 相关阅读:
    音频(一)_音频认知(1.音频释义)
    音频_写在前面的话
    SignInWithAppleId(Apple登录接入)_unity篇
    编程工具~用了都说好的快捷键大杂烩
    Unity的PlayerPrefs存储路径
    unity如何判断应用的运行平台
    Unity资源加载机制www的坑
    VSCode快捷键
    MD5加密字符串并转化为base64(C#和PHP代码相同实现)
    转载:关于 Google Chrome 中的全屏模式和 APP 模式
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/6397762.html
Copyright © 2011-2022 走看看