ListView OnItemClickListener不起作用当ListView被充气到onCreateInputView()时

时间:2012-04-26 11:17:24

标签: android

我正在开发一个键盘。当我在onCreateInputView()中膨胀ListView并返回相同的View(或父视图)时。我已经为ListView实现了setOnItemClickListener,我没有回调它。

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

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

注意:我可以获得它的ontouchlistner。

package 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");
    }

}

1 个答案:

答案 0 :(得分:0)

我也面临同样的问题&amp;经过大量的搜索,当没有答案为我工作时,我在我的列表适配器&amp;中执行下面的代码它为我工作。如果有人找到任何其他更好的答案,请告诉我们。

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

            }
        });