当键盘向上并滚动列表视图时,Edittext 保持焦点

发布于 2024-10-11 00:20:15 字数 3037 浏览 2 评论 0原文

我使用列表视图。列表中的每一项都包含一个编辑文本。当我点击某个edittext时,会弹出软键盘,我点击的edittext会短暂获得焦点,然后又失去焦点。我必须第二次单击才能真正获得焦点(为什么?)

聚焦于编辑文本键盘向上我滚动列表视图 ...

因为listview元素被回收,焦点重新出现在列表中其他位置的edittext上,我想这是正常的,但是为什么Android不删除这个该死的焦点!?如果我的用户开始滚动,我怎么能对 Android 说,好吧,我不想再将焦点放在编辑文本上?

这是活动的代码:

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); 

}

以及适配器的代码:

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;

}

以防万一,这里是“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"/>

以及“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 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> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伪装你 2024-10-18 00:20:15

我尝试了 Gilbou 解决方案,但它对我不起作用,因为我在所有行中都有 EditTexts。但通过一些更改,我可以使其工作:

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;
}

另外,为列表视图中的每个 EditText 添加焦点更改侦听器:

myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {  

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

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;
    }
}
手心的海 2024-10-18 00:20:15

也许我迟到了,但我希望这会对某人有所帮助。
Sangeeth Kumar 的答案是正确的,但需要在清单中添加另一行代码。

这里的问题是,当软键盘弹出时,屏幕上的内容被迫滚动并调整大小。
ListView (或 ExpandableListView) 滚动,所有视图都再次渲染,因此项目行中的 EditText 字段对象曾经的焦点现在是一个完全不同的对象。

因此,要解决此问题,您需要添加两行代码:

  1. 将以下行添加到 ListView 项 XML:

    android:descendantFocusability="beforeDescendants"

  2. 添加将以下行添加到应用程序清单中包含 ListView(或 ExpandableListView)的活动标记

    android:windowSoftInputMode="adjustPan"

这有帮助:)

Maybe I'm late but I hope this will help someone.
Sangeeth Kumar's answer is on the right track but it needs another line of code added to the manifest.

The problem here is when a soft keyboard pops out the contents on the screen are forced to scroll and resize.
When a ListView (or an ExpandableListView) is scrolled all the views are getting rendered again, so the EditText field object in the item row that used to be focused is now a completely different object.

So to solve this problem you need to add two lines of code:

  1. Add the following line to the ListView item XML:

    android:descendantFocusability="beforeDescendants"

  2. Add the following line to the activity tag containing your ListView (or ExpandableListView) in the application's manifest

    android:windowSoftInputMode="adjustPan"

Hope this helps :)

小梨窩很甜 2024-10-18 00:20:15

我认为找到了解决方案...

我用以下内容替换了适配器的 getView() 方法中的部分代码:

if (convertView != null) {
   holder = (Holder) convertView.getTag();
   if (((ListView)arg2).getSelectedItemPosition() == ListView.INVALID_POSITION) {
      arg2.requestFocus();
   }
}

I think found the solution...

I replaced a part of the code in the getView() method of my adapter by the following:

if (convertView != null) {
   holder = (Holder) convertView.getTag();
   if (((ListView)arg2).getSelectedItemPosition() == ListView.INVALID_POSITION) {
      arg2.requestFocus();
   }
}
没有伤那来痛 2024-10-18 00:20:15

您需要添加

在 XML 中

android:descendantFocusability="afterDescendants"

在 Java 中

listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

我认为它有效。

You Need to Add

In XML

android:descendantFocusability="afterDescendants"

In Java

listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

i think it works .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文