edittext的imeOptions属性和android软键盘的使用

来源:互联网 发布:淘宝几分一颗心 编辑:程序博客网 时间:2024/04/30 00:10

1、edittext的一些属性。用到一个edittext的时候,弹出来的软键盘是全屏的,除了软键盘,输入的容器占据了屏幕剩余的地方。很明显不是我们想要的。

其实只要设置 android:imeOptions:"flagNoExtractUi|flagNoFullscreen"就可以了 。

     另外还有 弹出来的软键盘的右下键,也就是enter键怎么自定义设置呢?

     首先 android:imeOptions可以有以下几种:(下面这段xml属性和常量值抄来的) 

(1)actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED效果:

(2)actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE效果:

(3)actionGo去往,对应常量EditorInfo.IME_ACTION_GO 效果:

(4)actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH效果: 

(5)actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND效果:

(6)actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT效果:

(7)actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE效果:

 


举个例子:加入我们要使用enter键点击了去搜索

也就是说一个edittext的imeOptions属性可以为:

<EditText                android:id="@+id/editText"                android:layout_width="470dp"                android:layout_height="38dp"                android:paddingLeft="8dp"                android:background="@drawable/edittext_shape"                android:ems="10"                android:singleLine="true"                android:imeOptions="actionSearch|flagNoExtractUi|flagNoFullscreen">                <requestFocus />            </EditText>


那么 怎么 在代码中根据常量来使用我们自己的逻辑呢?

  et =(EditText)findViewById(R.id.editText);

et.setOnEditorActionListener(new EditText.OnEditorActionListener() {            @Override            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {                Log.i(TAG, "onEditorAction ----------- actionId:" + actionId);                if (actionId == EditorInfo.IME_ACTION_SEARCH) {                    InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);                    onClickListener.onClick(goBtn);                }                return false;            }        });


2、上面是关闭软键盘,打开软键盘,android内部已经封装了:在edittext获取焦点之后,点击enter则弹出软键盘

弹出软键盘的代码: 

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);


3、有时候软键盘弹出来会影响布局:比如顶上去了。。

怎么办?

我们可以在AndroidManifest.xml的Activity标签中设置android:windowSoftInputMode为adjustPan(没有滚动条的android页面),或者adjustResize(有滚动条的android页面)


4、注意这里的edittext属性paddingleft有点技巧,一般输入框前面空几个空格就是它实现的。另外如果需要实现输入框前面有几个字不能被修改可以利用这个属性

还有textview来实现,很不错。

0 0
原创粉丝点击