これは、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;
}
}
activity_main.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;
}
}