gpt4 book ai didi

android - 如何解决android上的 "setOnKeyListener(View.OnKeyListener) in the type View is not applicable for the arguments (new OnKeyListener(){})"错误?

转载 作者:太空狗 更新时间:2023-10-29 15:54:46 35 4
gpt4 key购买 nike

我收到一条错误消息“ 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;
}
});

最佳答案

setOnKeyListener 更改为:

myEditText.setOnKeyListener(new EditText.OnKeyListener() {

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

// your code here
}
});

或者导入android.view.View.OnKeyListener包为EditText设置OnKeyListener

关于android - 如何解决android上的 "setOnKeyListener(View.OnKeyListener) in the type View is not applicable for the arguments (new OnKeyListener(){})"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14501226/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com