Android:setOnItemClickListener 不适用于软键盘中的 ListView

标签 android android-listview android-softkeyboard

我正在为 Android 开发自定义软键盘。我在普通键盘的顶部添加了一个列表。该列表只是一个普通的 ListView。我需要做的是检测并响应主程序中的点击事件,即SoftKeyboard.java。 ListView 在 onCreateCandidatesView() 方法中初始化。这是我的代码。

suggestion_list.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/candidate_background"
    android:divider="#BBCFCFCF"
    android:dividerHeight="1dp"
    android:drawSelectorOnTop="false" />

SoftKeyboard.java

public class SoftKeyboard extends InputMethodService 
        implements KeyboardView.OnKeyboardActionListener {

    //dummy data for the list
    private static final String[] items = {"1","2","3"};
    //some other variables

    @Override 
    public View onCreateCandidatesView() {     
        LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ListView ls = (ListView)mInflater.inflate(R.layout.suggestion_list,null);

        ls.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items));

        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.v("index", "Clicked");
            }
        });

        return ls;
    }

    //some other methods
}

但 ListView 似乎根本不响应 setOnItemClickListener()。任何人都可以在这个问题上帮助我吗?非常感谢!


更新:

我发现即使我用的是普通的ListView,还是不行。我更新了上面的代码以使用普通的 ListView。

最佳答案

尝试添加

android:descendantFocussability=blocksDescendants 到最顶部的线性布局。它可能会起作用。

关于Android:setOnItemClickListener 不适用于软键盘中的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13739079/

相关文章:

android - 列表和图片在同一个 ScrollView 中

android - 具有单独行布局的 ListView 无法正确生成 View

android - 滚动我的 View 以在键盘打开时显示所有 editText

android - 找不到 com.android.support :appcompat-v7:25. 2.0

android - 在 map 上创建自定义叠加层

php - 从android将多个图像保存到mysql php中,但只插入一张图像

android - Android 软键盘的多语言支持

android - 如何摆脱 Android Studio 警告 "Result of getException() not thrown"?

android - 使用 android Wi-Fi Direct API : mChannel = mManager. initialize() 的问题

android - 安卓软键盘