EditText(5)如何控制输入键盘的显示方式及参数表

 

参考:

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

 

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

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

1 <application ... >
2     <activity
3         android:windowSoftInputMode="stateVisible" ... >
4         ...
5     </activity>
6     ...
7 </application>

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

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

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

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

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

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

1     <activity android:name=".MainActivity"
2             android:windowSoftInputMode="stateVisible|adjustResize" >
3             <intent-filter>
4                 <action android:name="android.intent.action.MAIN" />
5 
6                 <category android:name="android.intent.category.LAUNCHER" />
7             </intent-filter>
8         </activity>

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

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

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

ValueDescription
"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.

 

转载于:https://www.cnblogs.com/sjjg/p/5575301.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值