欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android基础教程之android的listview与edittext冲突解决方法

程序员文章站 2023-11-08 14:39:40
最近遇到一个关于android软键盘的问题。在listview中每个item中都有个edittext,在最后的几个item中,edittext第一次点击界面还能向上弹出,正...

最近遇到一个关于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中所作的处理:

复制代码 代码如下:

package cn.test.systemsetting;

import com.********.r;

import android.content.context;
import android.util.attributeset;
import android.view.keyevent;
import android.view.layoutinflater;
import android.view.view;
import android.view.inputmethod.inputmethodmanager;
import android.widget.edittext;
import android.widget.linearlayout;
/**
 *
 * 针对设备管理键盘事件的处理
 * divid小硕
 *
 * **/

public class mylayout extends linearlayout {
    private context context;
    public mylayout(context context) {
        super(context);
        // todo auto-generated constructor stub
        this.context=context;
        layoutinflater.from(context).inflate(r.layout.device_manager, this);//此处所加载的layout就是上面的xml,即它的名字就是device_manager.xml
    }
    public mylayout(context context, attributeset attrs, int defstyle) {
        super(context, attrs, defstyle);
        // todo auto-generated constructor stub
    }

   
    public mylayout(context context, attributeset attrs) {
        super(context, attrs);
        // todo auto-generated constructor stub
    }
    @override
    public boolean dispatchkeyeventpreime(keyevent event) {
        // todo auto-generated method stub
        if(context!=null){
            inputmethodmanager imm = (inputmethodmanager) context.getsystemservice(context.input_method_service);
            if(imm.isactive() && event.getkeycode() == keyevent.keycode_back){
                view view = devicemanageractivity.lv_data.getfocusedchild();
                if(view!=null){
                    view.clearfocus();
                }

            }
        }

        return super.dispatchkeyeventpreime(event);
    }
}

主界面所采用的加载方式要是这样的:

复制代码 代码如下:

public class devicemanageractivity extends activity implements onclicklistener{
    public static listview lv_data;
    static devmgradapter adapter;

    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        // 1.全屏
        requestwindowfeature(window.feature_no_title); // 无标题
        getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,
                windowmanager.layoutparams.flag_fullscreen);

        this.setcontentview(new mylayout(this));
        init();
    }
}