OnKeyListener()不执行 [英] OnKeyListener() not executing
问题描述
会有人好心建议,为什么在安其()方法code未执行。请注意,我试图触发此我击中在Android键盘搜索按钮。
下面是主要的code:
包com.onkeyexample1;进口android.app.Activity;
进口android.os.Bundle;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.util.Log;
进口android.view.KeyEvent;
进口android.view.View;
进口android.view.View.OnKeyListener;
进口android.widget.EditText;公共类OnKeyExample1Activity延伸活动{
EDITTEXT的EditText;
OnKeyListener onKeyListener;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
EDITTEXT =(EditText上)findViewById(R.id.editText1);
editText.setOnKeyListener(onKeyListener);
editText.addTextChangedListener(inputTextWatcher);onKeyListener =新OnKeyListener(){
@覆盖
公共布尔安其(视图V,INT键code,KeyEvent的事件){
的System.out.println(点击);
返回true;
}
};
}
我尝试添加code块以下为修复建议,但它并没有帮助:
私人TextWatcher inputTextWatcher =新TextWatcher(){
公共无效afterTextChanged(编辑S){}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT)
{}
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
// Log.d(TAG,s.charAt(计数1)+字派);;
}
};
}
下面是我的布局XML:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/你好
/>
< EditText上的android:imeOptions =actionSearch的android:layout_width =FILL_PARENT
机器人:ID =@ + ID / editText1机器人:layout_height =WRAP_CONTENT>
<&requestFocus的GT;< / requestFocus的>
< /&的EditText GT;
< / LinearLayout中>
任何建议都大大AP preciated!
这是第一个实施特德的建议的:
公共类OnKeyExample1Activity延伸活动{
EDITTEXT的EditText;
OnKeyListener onKeyListener;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
EDITTEXT =(EditText上)findViewById(R.id.editText1);
editText.setOnKeyListener(onKeyListener =新OnKeyListener(){
@覆盖
公共布尔安其(视图V,INT键code,KeyEvent的事件){
的System.out.println(点击);
返回true;
}
});
}
}
下面是修复在我的评论中提到的双触发问题:
editText.setOnKeyListener(onKeyListener =新OnKeyListener(){
@覆盖
公共布尔安其(视图V,INT键code,KeyEvent的事件){
如果(event.getAction()!= KeyEvent.ACTION_DOWN){
的System.out.println(哎呀);
返回false;
}
返回true;
}
});
}
您需要分配一个值 onKeyListener
editText.setOnKeyListener你打电话之前(onKeyListener);
。只是移动分配的前方方法调用的,它应该工作。
Would someone kindly suggest why the code in the OnKey() method is not executing. Please note that I am attempting to trigger this my hitting the "Search" button on the android key board.
Here is the main code:
package com.onkeyexample1;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
public class OnKeyExample1Activity extends Activity {
EditText editText;
OnKeyListener onKeyListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.editText1);
editText.setOnKeyListener(onKeyListener);
editText.addTextChangedListener(inputTextWatcher);
onKeyListener = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.out.println("Clicked");
return true;
}
};
}
I tried adding the block of code below as a suggested fix but it didn't help:
private TextWatcher inputTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) { }
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{ }
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Log.d(TAG, s.charAt(count-1) + " character to send");;
}
};
}
Here is my layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText android:imeOptions="actionSearch" android:layout_width="fill_parent"
android:id="@+id/editText1" android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
Any suggestions are greatly appreciated!
Here is the first implementation of Ted's suggestion:
public class OnKeyExample1Activity extends Activity {
EditText editText;
OnKeyListener onKeyListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.editText1);
editText.setOnKeyListener(onKeyListener = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.out.println("Clicked");
return true;
}
});
}
}
Here's the fix to the double trigger problem mentioned in my comment:
editText.setOnKeyListener(onKeyListener = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_DOWN) {
System.out.println("Argh");
return false;
}
return true;
}
});
}
You need to assign a value to onKeyListener
before you call editText.setOnKeyListener(onKeyListener);
. Just move the assignment up ahead of the method call and it should work.
这篇关于OnKeyListener()不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!