gpt4 book ai didi

java - OnKeyListener() 没有执行

转载 作者:行者123 更新时间:2023-11-30 09:45:46 25 4
gpt4 key购买 nike

请问为什么 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;
}
});
}

最佳答案

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

关于java - OnKeyListener() 没有执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7459100/

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