1、edittext的一些属性。用到一个edittext的时候,弹出来的软键盘是全屏的,除了软键盘,输入的容器占据了屏幕剩余的地方。很明显不是我们想要的。
其实只要设置 android:imeOptions:"flagNoExtractUi|flagNoFullscreen"就可以了 。
另外还有 弹出来的软键盘的右下键,也就是enter键怎么自定义设置呢?
首先 android:imeOptions可以有以下几种:
(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有点技巧,一般输入框前面空几个空格就是它实现的。另外如果需要实现输入框前面有几个字不能被修改可以利用这个属性
一些关于EditText和软键盘的常见属性问题,当然这些并不能满足我们真正开发的需要,我根据我的项目写了一个Demo,下面我们来过一遍。
下面是我的布局文件:很简单,上面是一个EditText,下面是一个List
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#aaaaaaaa"
tools:context="com.example.v_lzhiy.edittextdemo.MainActivity">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp"
android:background="@drawable/radius_layout"
android:drawablePadding="2dp"
android:drawableRight="@drawable/close"
android:hint="搜搜看"
android:imeOptions="actionSearch"//有了这个弹出的软键盘就会有个搜索键了
android:paddingBottom="6dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop="6dp"
android:singleLine="true"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
一般情况下我们搜索框中的X号要等有文字的时候才出来,怎么让它隐藏呢?
EditText有个方法叫getCompoundDrawables()[]和一个setCompoundDrawables(left,right,top,bottom)方法就是获取和设置EditText左右上下的Drawable的,
这里我们先获取drawableRight这张图片,再将它设为null。
drawable = editText.getCompoundDrawables()[2];
editText.setCompoundDrawables(null, null, null, null);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String str = editText.getText().toString();
if ("".equals(str)) {
editText.setCompoundDrawables(null, null, null, null);
} else {
editText.setCompoundDrawables(null, null, drawable, null);
}
}
});
当我们点击右侧“X"的时候应该清空文字,但是EditText并没有给我们提供这个方法,这个就尴尬了,我们可以通过给EditText设置触摸监听,计算当我们触摸右面的位置来触发这个事件。
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (drawable == null)
return false;
if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
if (event.getX() > editText.getWidth()
- editText.getPaddingRight()
- drawable.getIntrinsicWidth()) {
editText.setText("");
}
return false;
}
});
现在我们在搜索框内的工作基本就介绍完了,还记得我们刚开始说到我们设置的键盘里有个搜索按键吧?我们点击后得写搜索的逻辑啊,这就写:
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
String str = editText.getText().toString();
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent
.ACTION_DOWN) {
hideKeyBoard();
Toast.makeText(MainActivity.this,"搜一搜",Toast.LENGTH_SHORT).show();
}
return false;
}
});
这里我们获取键盘搜索的点击事件,好像不对,应该改成了Search,怎么还是KEYCODE_ENTER,没写错,虽然图标换了,但是这个KeyCode还是KEYCODE_ENTER。这里我们只做了简单的Toast,当然,还需要我们手动收键盘:
private void hideKeyBoard() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
editText.clearFocus();
}
关于收键盘我们android有强大的back键可以收,但是苹果没有啊,苹果有时候是键盘里有取消,有时候是当我们滚动的时候收,我下面写的List就是干这个用的。
我们可以在滚动的时候收键盘,监听ListView的setOnScrollListener(),当滚动的时候收键盘。
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
hideKeyBoard();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
}
});
还有一点:当我们写聊天应用或其他需求时,都会用到ListView,就拿聊天说吧,当软键盘弹出来的时候,会覆盖在我们的ListView外面,这样体验就很不好,我们希望当有软键盘弹起来的时候ListView跟着一起滚动的话,只需要在xml文件中,给Listview加上
android:transcriptMode="alwaysScroll"
这样一条属性就可以了。