OnKeyListener() 未执行

发布于 2024-12-05 15:49:46 字数 3214 浏览 0 评论 0原文

有人会建议为什么 OnKey() 方法中的代码没有执行吗?请注意,我试图通过点击 Android 键盘上的“搜索”按钮来触发此操作。

这是主要代码:

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;
                 }
   };
}

我尝试添加下面的代码块作为建议的修复,但没有帮助:

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

这是我的布局 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>

非常感谢任何建议!

这是 Ted 建议的第一个实现:

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

这是我的评论中提到的双触发问题的修复:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自演自醉 2024-12-12 15:49:46

在调用 editText.setOnKeyListener(onKeyListener); 之前,您需要为 onKeyListener 赋值。只需将赋值移到方法调用之前,它就应该可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文