1

我正在开发键盘。当我在 onCreateInputView() 中为 ListView 充气并将相同的 View(或父 View)返回给它时。我已经为 ListView 实现了 setOnItemClickListener 我没有得到回调。

可能是什么问题?android 框架不适用于键盘的列表视图(InputMethodService)?

FY 参考我附上代码请看一下。

注意:我能够得到它的 ontouchlistner。

包 com.listkeyboard;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Configuration;
import android.inputmethodservice.InputMethodService;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class ListKeyBoardIME extends InputMethodService {



    private LinearLayout mainLayout;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        System.out.println("onCreate");
    }

    @Override
    public View onCreateCandidatesView() {
        // TODO Auto-generated method stub
        System.out.println("onCreateCandidatesView");
        return super.onCreateCandidatesView();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("onDestroy");
    }

    @Override
    public void onFinishInput() {
        // TODO Auto-generated method stub
        super.onFinishInput();
        System.out.println("onFinishInput");
    }

    @Override
    public void onFinishInputView(boolean finishingInput) {
        // TODO Auto-generated method stub
        super.onFinishInputView(finishingInput);
        System.out.println("onFinishInputView");
    }

    @Override
    public View onCreateInputView() {
        // TODO Auto-generated method stub

        System.out.println(" onCreateInputView");
        LayoutInflater layoutInflater = (LayoutInflater) ListKeyBoardIME.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View mMainKeyboardLayout = layoutInflater.inflate(R.layout.keyboard_list_view, null);

         mainLayout = (LinearLayout)mMainKeyboardLayout.findViewById(R.id.main_layout);

        ListView listView = (ListView)mainLayout.findViewById(R.id.list);



        ArrayList<Character> alphabets = new ArrayList<Character>();
        for (char ch = 'a'; ch  <= 'z'; ch++) {
            alphabets.add(ch);
            if (ch == 'n') {
                break;
            }
        }

        ArrayAdapter<Character> adapter = new ArrayAdapter<Character>(ListKeyBoardIME.this, android.R.layout.simple_list_item_1,alphabets);

        listView.setAdapter(adapter);

        listView.setItemsCanFocus(false);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getApplicationContext(), "on itemclicked clicked" , Toast.LENGTH_SHORT).show();

            }

        });

        setInputView(mainLayout);

        return mainLayout;
    }

    @Override
    public void onStartCandidatesView(EditorInfo info, boolean restarting) {
        // TODO Auto-generated method stub
        super.onStartCandidatesView(info, restarting);
        System.out.println("onStartCandidatesView ");
    }

    @Override
    public void onStartInput(EditorInfo attribute, boolean restarting) {
        // TODO Auto-generated method stub
        super.onStartInput(attribute, restarting);
        System.out.println("onStartInput ");
    }

    @Override
    public void onStartInputView(EditorInfo info, boolean restarting) {
        // TODO Auto-generated method stub
        super.onStartInputView(info, restarting);
        System.out.println("onStartInputView");
    }

}
4

1 回答 1

0

我也面临同样的问题,经过大量搜索,当没有答案对我有用时,我在我的列表适配器中实现了下面的代码,它对我有用。如果有人找到其他更好的答案,请告诉我们。

 view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
于 2016-01-11T10:37:47.713 回答