PDA(Android系統(tǒng))EditText獲取文本方法及焦點(diǎn)自動(dòng)跳轉(zhuǎn)問(wèn)題處理

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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容