3

我收到一条错误消息“View 类型中的方法 setOnKeyListener(View.OnKeyListener) 不适用于参数 (new OnKeyListener(){})”

我该如何克服这个问题?

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_first_app);

    ListView myListView =(ListView)findViewById(R.id.myListView);

    final EditText myEditText =(EditText)findViewById(R.id.myEditText);

   final ArrayList<String> toDolist =new ArrayList<String>();

    final ArrayAdapter<String> aa;

    aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,toDolist);

    myListView.setAdapter(aa);

    myEditText.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if(event.getAction()==KeyEvent.ACTION_DOWN)

                if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
                    toDolist.add(0,myEditText.getText().toString());

                    aa.notifyDataSetChanged();

                    myEditText.setText("");

                    return true;
                }
            return false;
        }
    });
4

2 回答 2

9

更改setOnKeyListener为:

myEditText.setOnKeyListener(new EditText.OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

              // your code here 
        }
    });

或导入 android.view.View.OnKeyListenerOnKeyListener为 EditText设置

于 2013-01-24T12:20:59.340 回答
0

尝试导入 android.view.View.OnKeyListener;

而不是导入 android.content.DialogInterface.OnKeyListener;

于 2016-10-24T14:01:17.917 回答