最近遇到一个关于android软键盘的问题。在ListView中每个Item中都有个EditText,在最后的几个Item中,EditText第一次点击界面还能向上弹出,正常显示,
但第二次点击时,软件盘就把最后的几个Item给正当住了。这样很影响用户体验的。
其实解决的办法只要想一下,我相信有经验的开发人员就能够想到,让软键盘在消失的时候让相应Item中的EditText消失焦点clearFouce();但是有个关键的问题,
就是在获得返回事件的时候,如果获得的事件不对那就不会达到想要的效果。这个back时间一定要是自定Layout中的back事件才可以。
直接上代码。
<cn.test.systemSetting.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_bg"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_data"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:transcriptMode="normal"
>
</ListView>
</cn.test.systemSetting.MyLayout>
自定义layout中所作的处理:
1 package cn.test.systemSetting;
2
3 import com.********.R;
4
5 import android.content.Context;
6 import android.util.AttributeSet;
7 import android.view.KeyEvent;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.inputmethod.InputMethodManager;
11 import android.widget.EditText;
12 import android.widget.LinearLayout;
13 /**
14 *
15 * 针对设备管理键盘事件的处理
16 * divid小硕
17 *
18 * **/
19
20 public class MyLayout extends LinearLayout {
21 private Context context;
22 public MyLayout(Context context) {
23 super(context);
24 // TODO Auto-generated constructor stub
25 this.context=context;
26 LayoutInflater.from(context).inflate(R.layout.device_manager, this);//此处所加载的layout就是上面的xml,即它的名字就是device_manager.xml
27 }
28 public MyLayout(Context context, AttributeSet attrs, int defStyle) {
29 super(context, attrs, defStyle);
30 // TODO Auto-generated constructor stub
31 }
32
33
34 public MyLayout(Context context, AttributeSet attrs) {
35 super(context, attrs);
36 // TODO Auto-generated constructor stub
37 }
38 @Override
39 public boolean dispatchKeyEventPreIme(KeyEvent event) {
40 // TODO Auto-generated method stub
41 if(context!=null){
42 InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
43 if(imm.isActive() && event.getKeyCode() == KeyEvent.KEYCODE_BACK){
44 View view = DeviceManagerActivity.lv_data.getFocusedChild();
45 if(view!=null){
46 view.clearFocus();
47 }
48
49 }
50 }
51
52 return super.dispatchKeyEventPreIme(event);
53 }
54 }
主界面所采用的加载方式要是这样的:
1 public class DeviceManagerActivity extends Activity implements OnClickListener{
2 public static ListView lv_data;
3 static DevMgrAdapter adapter;
4
5 protected void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 // 1.全屏
8 requestWindowFeature(Window.FEATURE_NO_TITLE); // 无标题
9 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
10 WindowManager.LayoutParams.FLAG_FULLSCREEN);
11
12 this.setContentView(new MyLayout(this));
13 init();
14 }
15
16
17
18
19
20
21
22
23 }
android的这种冲突还有很多,希望在以后工作中遇到都能一一解决,也希望朋友们多多给建议。哪里不对的地方希望指出,多多指教。
来源:https://www.cnblogs.com/dividxiaoshuo-fighting/archive/2013/04/26/divid_xiaoshuo.html