AutoCompleteTextView失去焦点

3

我有一个ListView,其中我使用AutoCompleteTextViewEditText填充它。用户可以编程方式添加新行并编辑它们。但是,当我添加两个或多个行时,我无法在第二个/第三个等AutoCompleteTextView中输入文本-当我触摸TextView时,键盘出现,但我无法输入任何内容,因为AutoCompleteTextView失去了焦点。以下是我的适配器getView和具有行设计的xml。

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null) {
                convertView = mInflater.inflate(R.layout.add_meal_activity_list_view_row, null);
                holder = new ViewHolder();
                holder.productNameTextView = (AutoCompleteTextView) convertView.
                        findViewById(R.id.mealPartAutoCompleteTextView);
                holder.foodQuantityEditText = (EditText) convertView.
                        findViewById(R.id.mealQuantityEditText);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.foodQuantityEditText
                .setText(Utilities.trimDecimalsToString(_mealParts.get(position).foodQuantity));
            holder.foodQuantityEditText.addTextChangedListener(new FoodQuantityTextWatcher(position, holder.foodQuantityEditText));

            //TODO here fetch real product names from web API
            //TODO on item selected - pass to mealPart proudctId of chosen product
            ArrayAdapter<String> productNamesAdapter = new ArrayAdapter<String>(_context, android.R.layout.simple_list_item_1, DATA);
            holder.productNameTextView.setAdapter(productNamesAdapter);
            holder.productNameTextView.addTextChangedListener(new ProductNameTextWatcher(position,holder.productNameTextView));

            return convertView;
        }

这是行定义。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:orientation="horizontal" 
    android:padding="5dp"
    android:focusable="true"
    android:focusableInTouchMode="true" >

    <AutoCompleteTextView
        android:id="@+id/mealPartAutoCompleteTextView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:completionThreshold="2"
        android:inputType="textNoSuggestions"
        android:textSize="15sp"
        style="@style/ListTextView" />

    <EditText android:id="@+id/mealQuantityEditText"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:inputType="number"
        android:textSize="15sp"
        style="@style/ListTextView"
        />
</LinearLayout>

这里是一个列表的行被填充的活动。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    style="@style/ListViewStyle"
    android:focusableInTouchMode="true" 
    android:focusable="true">
    <EditText android:id="@+id/mealNameTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/mealNameTextViewHint"
        style="@style/TitleTextViewStyle" />

   <ListView android:id="@+id/mealPartsListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <ImageButton
                android:id="@+id/okMealButton"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:gravity="left"
                android:src="@drawable/ic_action_accept"
                android:contentDescription="@string/addMealPartButtonContentDescription"
                android:onClick="onOkMealClick"
                android:background="?android:selectableItemBackground"
                android:padding="5dp">
        </ImageButton>
        <ImageButton
                android:id="@+id/addMealPartButton"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:gravity="left"
                android:src="@drawable/ic_action_new"
                android:contentDescription="@string/addMealPartButtonContentDescription"
                android:onClick="onAddMealPartButtonClick"
                android:padding="5dp"
                android:background="?android:selectableItemBackground">
        </ImageButton>
        <ImageButton
                android:id="@+id/addFavouriteMeal"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:gravity="left"
                android:src="@drawable/ic_action_favorite"
                android:contentDescription="@string/addMealPartButtonContentDescription"
                android:onClick="onAddMealPartButtonClick"
                android:padding="5dp"
                android:background="?android:selectableItemBackground">
        </ImageButton>
    </LinearLayout>
</LinearLayout>

更新

我查看了LogCat,在点击AutoComplete时,无法获取焦点时会记录类似以下内容的日志:

01-07 12:35:08.685: E/SpannableStringBuilder(29361): SPAN_EXCLUSIVE_EXCLUSIVE 跨度不能具有零长度


仍然有同样的问题,没有帮助。 - VsMaX
对于AutoCompleteTextView和EditText,请将其设置为不可聚焦,并从同一布局中的LinearLayout中删除android:focusable="true" android:focusableInTouchMode="true"。 - Nas
现在我无法将焦点放在它们上面,更不用说在它们里面输入了。键盘也没有出现。 - VsMaX
@MichaelCwienczek 你是从代码还是从布局中添加?我认为你只需要动态调用适配器就可以添加了……对吗? - Satyaki Mukherjee
@VsMaX,你解决了这个问题吗? - Mani
显示剩余2条评论
4个回答

1
这似乎是您的特定设备出现了键盘错误。请参见Android-SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
您是否尝试在模拟器或其他设备上进行测试?
此外,一些人报告称更改inputType设置可以解决此问题。尝试一些不同的选项。由于您的AutoCompleteTextView的选项是下载的,因此您可以尝试将此字段设置为none
android:inputType="none"

这是用来回答 Android Google Analytics SDK 显示 "SPAN_EXCLUSIVE_EXCLUSIVE spans 不能有零长度"


我已经在Galaxy S3和Galaxy Note 2上尝试过了,问题仍然存在,无论是Swiftkey键盘还是三星键盘。但是在模拟器上问题并不存在。我看到了你提供的链接,不幸的是,这只是关于SPAN_EXCLUSIVE_EXCLUSIVE意义的理论争议,并没有太大帮助。 - VsMaX
@MichaelCwienczek 你尝试过我的回答的第二部分了吗 - 你改变了 inputType 吗? - Phil
我刚刚测试了一下,问题仍然存在。我将切换到ScrollView,或许能解决问题。 - VsMaX

1
您可以使用以下代码使EditText获取焦点-
holder.foodQuantityEditText = (EditText) convertView.                 findViewById(R.id.mealQuantityEditText);

holder.foodQuantityEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(final View v, boolean hasFocus) 
            {
                v.post(new Runnable() 
                {
                    @Override
                    public void run() 
                    {
                        if (!v.hasFocus()) {
                            v.requestFocus();
                            v.requestFocusFromTouch();
                        }
                    }
                });
            }
        });

0
尝试类似这样的东西。
EditText etselect;
etselect.requestFocus(); // This will provide focus on that.

希望它能在某种程度上对你有所帮助!


我应该在哪里做这个?在单击事件中吗?此外,这似乎只是临时解决方案,而不是真正的解决方案。 - VsMaX
没问题,我会再次检查。我会回复你的。 - AndroidHacker

-1

AutoCompleteTextView 无法与 ListView 兼容。

当键盘显示时,布局将被调整大小或滚动以适应键盘和您的 AutoCompleteTextView。由于这个原因,ListView 必须刷新自己,这会导致 AutoCompleteTextView 失去焦点并且下拉列表永远不会显示。

我建议您改用 ScrollView。

我不确定,但如果您的适配器使用稳定的项 ID,则 ListView 将为特定项重复使用相同的视图,可能 AutoCompleteTextView 将不会失去焦点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接