簡體   English   中英

Android:setOnItemClickListener不適用於軟鍵盤中的ListView

[英]Android: setOnItemClickListener doesn't work for ListView in soft keyboard

我正在為Android開發自定義軟鍵盤。 我在普通鍵盤的頂部添加了一個列表。 該列表只是一個普通的ListView。 我需要做的是檢測並響應主程序SoftKeyboard.java中的click事件。 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:descendantFocussability=blocksDescendants 。它可能會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM