我想在“textEdit”中按下“enter”时尝试做某事。我正在使用onkeylistener,但它在textedit部分显示错误。我的代码是
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent keyevent) {
//When "Enter" or "Search" Pressed)
if ((keyevent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
EditText editText = (EditText)findViewById(R.id.editText);
WebView mw = (WebView) findViewById(R.id.webView2);
mw.setWebViewClient(new WebViewClient());
mw.getSettings().setJavaScriptEnabled(true);
mw.loadUrl("http://i.word.com/idictionary/" + editText.getText());
return true;
}
return false;
}
});
}
你能帮我吗?
答案 0 :(得分:0)
在您的代码中,“editText”变量在“if”子句中声明并初始化。 以下代码应该可以正常工作:
EditText editText = (EditText)findViewById(R.id.editText);
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent keyevent) {
//When "Enter" or "Search" Pressed)
if ((keyevent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
WebView mw = (WebView) findViewById(R.id.webView2);
mw.setWebViewClient(new WebViewClient());
mw.getSettings().setJavaScriptEnabled(true);
mw.loadUrl("http://i.word.com/idictionary/" + editText.getText());
return true;
}
return false;
}
});
}