EDITTEXT保​​持专注,当键盘的上和滚动列表视图 [英] Edittext keeps focus when keyboard up AND scrolling the listview

查看:121
本文介绍了EDITTEXT保​​持专注,当键盘的上和滚动列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个ListView。列表中的每个项目都包含一个EditText上。当我点击一个EditText,软键盘弹出时,我的EditText收益上点击的焦点很短的时间,然后失去它。我必须单击第二次真正的焦点(为什么?)

专注于一个editext 键盘了我滚动列表视图 ...

由于列表视图元素的回收利用,重点就重新出现在的EditText在列表中的其他位置,我想这是正常的,但为什么Android的不会删除这该死的焦点!?我怎么能拒绝到Android确定,如果我的用户开始滚动我不希望再重点对一个EditText?

下面是活动的code:

 公共类simplelist中延伸活动{
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.user_picker);    ListView控件列表视图=(ListView控件)this.findViewById(R.id.user_picker_list);
    新UserPickerAdapter(这一点,列表视图);}

和适配器的code:

 公共类UserPickerAdapter延伸BaseAdapter {私人最终LayoutInflater mInflater;私有String [] mUsers = {Guib,乔纳斯,弗雷德,丹尼斯,阿诺德,弗朗索瓦,国宝,亚历克斯,萨斯基亚,弗纳,阿纳托利 };公共UserPickerAdapter(上下文的背景下,ListView控件列表视图){    mInflater = LayoutInflater.from(上下文);
    listview.setAdapter(本);}@覆盖
公众诠释的getCount(){
    返回mUsers.length;
}@覆盖
公共对象的getItem(INT位置){
    返回mUsers [位置]
}@覆盖
众长getItemId(INT位置){
    返回的位置;
}静态私有类持有人{
    TextView的用户名;
}@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中ARG2){    持有人持有人;    如果(convertView!= NULL){        支架=(座)convertView.getTag();    }其他{        持有人=新的持有人();
        convertView = mInflater.inflate(R.layout.user_picker_list_item,NULL);
        holder.username =(TextView中)convertView.findViewById(R.id.user_picker_list_item_name);
        convertView.setTag(保持器);    }    字符串str =(字符串)的getItem(位置);
    holder.username.setText(STR);
    返回convertView;}

以防万一,这里是'user_picker.xml

在code<?pre> &LT; XML版本=1.0编码=UTF-8&GT;&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk / RES / Android的机器人:layout_height =FILL_PARENT的android:layout_width =FILL_PARENT机器人:方向=垂直机器人:填充=0dp&GT;&LT; ListView的机器人:背景=@色/ color_grey1机器人: cacheColorHint =@色/ color_grey1机器人:ID =@ + ID / user_picker_list机器人:分=@色/ color_blueGrey1机器人:layout_height =match_parent的android:layout_width =match_parent/&GT;

和一个在'user_picker_list_item.xml

<?pre> &LT; XML版本=1.0编码=UTF-8&GT;&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk / RES / Android的机器人:layout_width =FILL_PARENT机器人:方向=垂直机器人:layout_height =FILL_PARENT&GT;&LT; EditText上的android:layout_height =WRAP_CONTENT机器人:ID =@ + ID / user_picker_list_item_name机器人:layout_width =WRAP_CONTENT安卓了minWidth =200dp机器人:layout_gravity =中心&GT;&LT; /&的EditText GT;&LT; / LinearLayout中&GT;


解决方案

我试过Gilbou解决方案,但因为我在所有行EditTexts它没有为我工作。但也有一些变化,我可以让它工作:

 的EditText currentEdit;公共查看getView(INT Q,查看convertView,父母的ViewGroup){    ViewHolder持有人;    如果(convertView == NULL){        convertView =(的LinearLayout)inflater.inflate(R.layout.grade_row,NULL);        持有人=新ViewHolder();
        //充气支架
        convertView.setTag(保持器);
    }其他{
        支架=(ViewHolder)convertView.getTag();
    }    如果(listView.getSelectedItemPosition()== ListView.INVALID_POSITION){
        如果(currentEdit!= NULL)
            currentEdit.requestFocus();
    }    //更新行项目    返回convertView;
}

此外,添加一个焦点变化监听每一个的EditText列表视图里:

  myEditText.setOnFocusChangeListener(新OnFocusChangeListener(){    公共无效onFocusChange(视图V,布尔hasFocus){
        如果(!hasFocus)
            currentEdit = myEditText;
    }
}

I use a listview. Each item in the list contains one edittext. When I click on an edittext, the soft keyboard pops up, the edittext I clicked on gains the focus for a short time and then lose it. I have to click a second time to really get the focus (why?)

(focus on an editext and keyboard up) I scroll the listview...

Because the listview elements are recycled, the focus reappears on edittext at other positions in the list and I guess this is normal BUT why does Android not remove this damned focus!? How could I say to Android ok I do not want the focus anymore on an edittext if my user begins to scroll ?

Here is the code of the activity:

public class SimpleList extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_picker);

    ListView listview = (ListView)this.findViewById(R.id.user_picker_list);
    new UserPickerAdapter(this, listview); 

}

And the code of the adapter:

public class UserPickerAdapter extends BaseAdapter {

private final LayoutInflater mInflater;

private String[] mUsers = {"Guib", "Jonas", "Fred", "Denis", "Arnold", "Francois", "David", "Alex", "Saskia", "Verner", "Anatole"};

public UserPickerAdapter(Context context, ListView listview) {

    mInflater = LayoutInflater.from(context);   
    listview.setAdapter(this);  

}

@Override
public int getCount() {
    return mUsers.length;
}

@Override
public Object getItem(int position) {
    return mUsers[position];
}

@Override
public long getItemId(int position) {
    return position;
}

static private class Holder {
    TextView username; 
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {

    Holder holder;  

    if (convertView != null) {  

        holder = (Holder) convertView.getTag(); 

    } else {    

        holder = new Holder();  
        convertView = mInflater.inflate(R.layout.user_picker_list_item,null);
        holder.username = (TextView) convertView.findViewById(R.id.user_picker_list_item_name);
        convertView.setTag(holder);

    }

    String str = (String)getItem(position);
    holder.username.setText(str);
    return convertView;

}

Just in case, here is the code in 'user_picker.xml'

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" android:padding="0dp"><ListView android:background="@color/color_grey1" android:cacheColorHint="@color/color_grey1" android:id="@+id/user_picker_list" android:divider="@color/color_blueGrey1" android:layout_height="match_parent" android:layout_width="match_parent"/>

And the one in 'user_picker_list_item.xml'

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent"><EditText android:layout_height="wrap_content"android:id="@+id/user_picker_list_item_name" android:layout_width="wrap_content" android:minWidth="200dp" android:layout_gravity="center"></EditText></LinearLayout> 

解决方案

I tried Gilbou solution but it didn't work for me because I had EditTexts in all rows. But with some changes I could make it work:

EditText currentEdit;

public View getView(int q, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView==null) {

        convertView = (LinearLayout)inflater.inflate(R.layout.grade_row, null);

        holder = new ViewHolder();
        //Inflate holder
        convertView.setTag(holder);     
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    if (listView.getSelectedItemPosition() == ListView.INVALID_POSITION) {
        if (currentEdit!=null)
            currentEdit.requestFocus();
    }

    //update row items

    return convertView;
}

Also, add a focus change listener for every EditText inside the listview:

myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {  

    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) 
            currentEdit = myEditText;
    }
}

这篇关于EDITTEXT保​​持专注,当键盘的上和滚动列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆