参考:

  https://developer.android.com/training/keyboard-input/visibility.html

1,进入Activity时,EditText就获得焦点并显示输入软键盘。

  如果EditText所在的activity是Activity是主activity,那么以上行为是默认的,不是的话,要设置Activity的属性:

 <application ... >
<activity
android:windowSoftInputMode="stateVisible" ... >
...
</activity>
...
</application>

  注意,如果键盘是按键的,那么不显示键盘,只显示光标。

2,使用代码控制显示软键盘和焦点。

  调用下面的方法就可让某个view可输入文本:

 public void showSoftKeyboard(View view) {//指定想要输入键盘的view
if (view.requestFocus()) {//1,取得焦点
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);//2,用InputMethodManager显示键盘
}
}

  关闭键盘不用代码,按回车或返回键后自动消失。

3,控制键盘与页面控件的布局方式,可让键盘不挡住页面上的控件并可以滚动等。如:

     <activity android:name=".MainActivity"
android:windowSoftInputMode="stateVisible|adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

4,详细的软键盘显示参数

<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >

  Values set here (other than "stateUnspecified" and "adjustUnspecified") override values set in the theme.

Value Description
"stateUnspecified"

The state of the soft keyboard (whether it is hidden or visible) is not specified.

The system will choose an appropriate state or rely on the setting in the theme.

This is the default setting for the behavior of the soft keyboard.

默认的显示方式,不指定参数。

"stateUnchanged"

The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore.

按上次样式显示

"stateHidden"

The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates

forward to the activity, rather than backs into it because of leaving another activity.

当进入到activity且eidt有焦点时,不显示键盘。

"stateAlwaysHidden"

The soft keyboard is always hidden when the activity's main window has input focus.

总是不显示软键盘。

"stateVisible"

The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window).

显示软键盘

"stateAlwaysVisible"

The soft keyboard is made visible when the user chooses the activity — that is, when the user affirmatively navigates forward to

the activity, rather than backs into it because of leaving another activity.

当进入或离开时activity时,一直显示软键盘。

"adjustUnspecified"

It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the

window pan to make the current focus visible on-screen. The system will automatically select one of these modes depending on

whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be

resized, on the assumption that scrolling can make all of the window's contents visible within a smaller area.

This is the default setting for the behavior of the main window.

"adjustResize"

The activity's main window is always resized to make room for the soft keyboard on screen.

Activity的主窗口会缩放,以让软键盘不挡住view。

"adjustPan"

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically

panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally

less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

  This attribute was introduced in API Level 3.

最新文章

  1. 数百个 HTML5 例子学习 HT 图形组件 – 拓扑图篇
  2. Apache配置文件讲解
  3. JSP 新闻发布会
  4. OpenCascade Ray Tracing Rendering
  5. scalac error: bad option: &#39;-make:transitive&#39; on mvn package via command line
  6. 使用Java中的IO流,把A文件里的内容输入到B文件中
  7. [LeetCode]题解(python):118 Pascal&#39;s Triangle
  8. 深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)
  9. windows cmd color setup
  10. jQuery插件之ajaxFileUpload 2
  11. LeetCode Pow(x, n) (水题)
  12. wget下载FTP的文件
  13. SQL Server tables export/import with bcp
  14. hibernate初涉
  15. Sublime Text 2使用技巧汇总
  16. Android源码之Gallery专题研究(2)
  17. C++虚表(V-Table)解析
  18. 201521123016 《Java程序设计》第5周学习总结
  19. jqgrid 同列不同行的&lt;select&gt;不相同
  20. Windows 下 exe 服务注册到 service 服务中

热门文章

  1. Linux下汇编语言学习笔记64 ---
  2. Linux下汇编语言学习笔记61 ---
  3. NetworkManager的坑(如何让network manager不去管理网络端口)
  4. 怎么让Excel显示时间时候能把秒显示出来
  5. 使用NPOI将DataTable生成Excel
  6. angular 的ui.router 定义不同的state 对应相同的url
  7. 戴尔PowerEdge服务器RAID控制卡的配置
  8. [React] Make Compound React Components Flexible
  9. [Debug] Node-sass
  10. Cocos2dx 小技巧(九)现成的粒子特效