1)PDA(Android系統(tǒng))上文本獲取。
經(jīng)過(guò)多次測(cè)試發(fā)現(xiàn),使用addTextChangedListener監(jiān)聽文本根本無(wú)法,正常獲取到文本。有些情況都不觸發(fā)。后來(lái)發(fā)現(xiàn) 使用KeyEvent.KEYCODE_ENTER 加EditorInfo.IME_ACTION_DONE
搭配可以兼顧手動(dòng)輸入,跟按鍵識(shí)別。代碼如下:
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系統(tǒng))焦點(diǎn)會(huì)自動(dòng)往下跳問(wèn)題。
這個(gè)問(wèn)題開始以為屏蔽其他控件獲取焦點(diǎn),跟主動(dòng)獲取焦點(diǎn)就可以解決??墒窃趺炊际怯弥弥吞?。
后來(lái)發(fā)現(xiàn) 系統(tǒng)會(huì)按照布局從上到下,從左到右的傳遞focus??梢允褂?br>
android:nextFocusUp
android:nextFocusLeft
android:nextFocusRight
android:nextFocusDown
在不同的控件中來(lái)回切換。后來(lái)試下往自己身上切換發(fā)現(xiàn),竟然直接往自己身上切換就完事了,這應(yīng)該是正確姿勢(shì)。代碼如下:
————————————————
<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>