这是 Deminetix 提供的更完整的答案代码。
我已经使用 Deminetix 的答案来过滤 android 上的手持条形码阅读器并得到结果。
为了使其仅在带有按钮的屏幕上可用,我添加了一个带有 android:textColor="#FF000000" android:background="#00FFFFFF" android:enabled="false" 的 EditText 禁用 EditText 仍然会获取键盘事件。
可选地,我可以使用以下命令隐藏软件键盘,但在禁用 EditText 后,它不是必需的。
//InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//imm.showSoftInput(textPatientId, InputMethodManager.HIDE_IMPLICIT_ONLY);
MainActivity.java:
package com.doodkin.keyboardtest;
import com.doodkin.keyboardtest.ListenerEditText.KeyImeChange;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
public class MainActivity extends Activity {
private static final String TAG = "keyboard test";
//private EditText editText1;
ListenerEditText editText1=null;
public String barcodebuffer="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (ListenerEditText) findViewById(R.id.editText1);
editText1.setKeyImeChangeListener(new KeyImeChange() {
@Override
public boolean onKeyIme(int keyCode, KeyEvent event) {
String deviceName=event.getDevice().getName();
int keyboardType=event.getDevice().getKeyboardType();
int indexof=deviceName.indexOf("USB");
if(indexof!=-1 && keyboardType==InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC)
{
if(event.getKeyCode()==KeyEvent.KEYCODE_ENTER)
{
if(barcodebuffer!="")
{
Log.d(TAG, "filterBarcodeKeys Chars Flush: " + barcodebuffer );
barcodebuffer="";
}
}
else
{
barcodebuffer+=Character.toString((char)event.getUnicodeChar());
//Log.d(TAG, "filterBarcodeKeys Char: " + Character.toString((char)event.getUnicodeChar()) );
}
return true;
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
活动主.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.doodkin.keyboardtest.ListenerEditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10" >
<requestFocus />
</com.doodkin.keyboardtest.ListenerEditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="69dp"
android:text="Button" />
</RelativeLayout>
ListenerEditText.java:
package com.doodkin.keyboardtest;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.EditText;
/*
* example:
myListenerEditText.setKeyImeChangeListener(new KeyImeChange() {
@Override
public boolean onKeyIme(int keyCode, KeyEvent event) {
// All keypresses with the keyboard open will come through here!
// You could also bubble up the true/false if you wanted
// to disable propagation.
}
});
*/
public class ListenerEditText extends EditText {
private KeyImeChange keyImeChangeListener;
public ListenerEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setKeyImeChangeListener(KeyImeChange listener){
keyImeChangeListener = listener;
}
public interface KeyImeChange {
public boolean onKeyIme(int keyCode, KeyEvent event);
}
@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event){
if(keyImeChangeListener != null){
return keyImeChangeListener.onKeyIme(keyCode, event);
}
return false;
}
}