zoukankan      html  css  js  c++  java
  • [转]android自动弹出软键盘(输入键盘)

    转自:http://www.devdiv.com/home.php?mod=space&uid=65729&do=blog&id=11847

    很多应用中对于一个界面比如进入搜索界面或者修改信息等等情况,为了用户体验应该自动弹出软键盘而不是让用户主动点击输入框才弹出(因为用户进入该界面必然是为了更改信息)。具体实现这种效果如下:

    [代码]java代码:

    1      EditText  editText.setFocusable(true);
    2         editText.setFocusableInTouchMode(true);
    3         editText.requestFocus();
    4 InputMethodManager inputManager =
    5                     (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    6                 inputManager.showSoftInput(editText, 0);
     

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

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

    [代码]java代码:

     1 Timer timer = new Timer();
     2      timer.schedule(new TimerTask()
     3      {
     4          
     5          public void run()
     6          {
     7              InputMethodManager inputManager =
     8                  (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
     9              inputManager.showSoftInput(editText, 0);
    10          }
    11          
    12      },
    13          998);
     
  • 相关阅读:
    [HEOI2013]Eden 的新背包问题
    [UOJ#77]A+B Problem
    [CodeForces]786B Legacy
    [LUOGU]P4098[HEOI2013]ALO
    [BZOJ3207]花神的嘲讽计划
    [LUOGU]P2633 Count on a tree
    【东莞市选2007】拦截导弹
    [JZOJ] 3462. 【NOIP2013模拟联考5】休息(rest)
    [BZOJ] 2705: [SDOI2012]Longge的问题
    [BZOJ] 1191: [HNOI2006]超级英雄Hero
  • 原文地址:https://www.cnblogs.com/DarkMaster/p/4004665.html
Copyright © 2011-2022 走看看