1)PDA(Android系统)上文本获取。
经过多次测试发现,使用addTextChangedListener监听文本根本无法,正常获取到文本。有些情况都不触发。后来发现 使用KeyEvent.KEYCODE_ENTER 加EditorInfo.IME_ACTION_DONE
搭配可以兼顾手动输入,跟按键识别。代码如下:
public class BarCodeEditText extends ClearableEditText {
public interface Callback<String> {
void callback(String t);
}
public BarCodeEditText(Context context) {
super(context);
}
public BarCodeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BarCodeEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void afterTextChanged(Callback<String> callback) {
afterTextChanged(InputType.TYPE_CLASS_TEXT,callback);
}
public void afterTextFinish(Callback<String> callback) {
setImeOptions(EditorInfo.IME_ACTION_DONE);
setInputType(InputType.TYPE_CLASS_TEXT);
setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
String s = v.getText().toString().trim();
if (actionId == EditorInfo.IME_ACTION_DONE) {
callback.callback(s);
return true;
}
return false;
}
});
setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER && !TextUtils.isEmpty(getText().toString())){
callback.callback(getText().toString());
return true;
}
return false;
}
});
}
}
2)PDA(Android系统)焦点会自动往下跳问题。
这个问题开始以为屏蔽其他控件获取焦点,跟主动获取焦点就可以解决。可是怎么都是用着用着就跳了。
后来发现 系统会按照布局从上到下,从左到右的传递focus。可以使用
android:nextFocusUp
android:nextFocusLeft
android:nextFocusRight
android:nextFocusDown
在不同的控件中来回切换。后来试下往自己身上切换发现,竟然直接往自己身上切换就完事了,这应该是正确姿势。代码如下:
————————————————
<EditText
android:id="@+id/et_barcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:nextFocusDown="@id/et_barcode"
android:cursorVisible="true"
android:inputType="text|textMultiLine"
android:padding="10dp"
android:gravity="left|center_vertical"
><requestFocus /> </EditText>