gpt4 book ai didi

android - OnKeyListener 只检测返回键

转载 作者:太空狗 更新时间:2023-10-29 16:23:49 29 4
gpt4 key购买 nike

我已经创建了最简单的 Android 项目,以便使用 OnKeyListener 进行测试,以查看在 EditText 小部件中键入的内容。问题是 onKey 方法只触发返回键 - 没有其他。根据我下面的代码,什么可能会阻止 OnKeyListener 工作?

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setViewListeners();
}

private void setViewListeners() {

EditText et1 = (EditText)findViewById(R.id.text1);
EditText et2 = (EditText)findViewById(R.id.text2);

et1.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i("INFO", "keyCode=" + keyCode);
return false;
}
});
}
}

布局文件:

<?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"
>
<EditText
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
/>
<EditText
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

再一次,我得到日志语句的唯一按键是返回键(“keyCode=66”)。我还使用断点来确认那是代码执行的唯一时间。我的问题是什么?谢谢。

最佳答案

你应该使用 TextWatcher 而不是 OnClickListener

mPostEditText.addTextChangedListener(watcher);

TextWatcher watcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence charsequence, int i, int j, int k) {
// TODO Auto-generated method stub
}
}
@Override
public void beforeTextChanged(CharSequence charsequence, int i, int j, int k) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable editable) {
// TODO Auto-generated method stub
}
};

关于android - OnKeyListener 只检测返回键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7232398/

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