簡體   English   中英

刪除值並與列表視圖交互后,編輯文本失去焦點

[英]Edit text loses focus after errasing the value and interacting with a listview

我的程序有一個需要始終聚焦的編輯文本字段,但是由於某種原因,在它檢測到數據重復並將其刪除后,它應該重新聚焦,但它沒有。

下面的代碼檢查代碼是否在最后掃描的 5 個代碼上重復。 如果沒有重復,則將其添加到最后添加的 5 個代碼中,然后將其發送到數據庫(這里沒問題,該部分效果很好),如果重復,則清除該字段並嘗試重新關注編輯文本,但這是代碼失敗的地方。

筆記:

txtmanualbarcode是一個編輯文本。

setInputType(inputType.Type_NULL)隱藏鍵盤,因為它不需要。

scanneditems是最后 5 個代碼的數組列表。

allscanneditems是完整的代碼列表。

如果需要進一步的代碼或解釋,請問我,這個錯誤已經拖了我好幾天了。

    public void addScannedItem(final QrScannedItem it) {
        boolean duplicado = false;
        for (int a = 0; a < scannedItems.size(); a++) {
            if (scannedItems.get(a).qr_code.compareTo(it.qr_code) == 0) {
                txtmanualbarcode.setText("");
                txtmanualbarcode.clearFocus();
                txtmanualbarcode.requestFocus();
                txtmanualbarcode.setInputType(InputType.TYPE_NULL);
                duplicado = true;

            }
        }


        if (!duplicado) {
            scannedItems.add(0, it);
            if (scannedItems.size() > 5) {
                scannedItems.remove(5);
                allScannedItems.add(0, it);

            }

            runOnUiThread(new Runnable() {
                public void run() {
                    adapter.notifyDataSetChanged();
                }
            });
            showProgressBar();
            Thread thr = new Thread() {
                public void run() {
                    try {
                        String state = status.compareTo("in") == 0 ? "1" : "0";
                        String time = getScanTime();
                        JSONObject obj = QrCourseServer.checkin(state, it.qr_code, time, course.id, AppCache.currentUser.token);
                        String msg = obj.getString("message");
                        it.extraMessage = msg;
                        if (msg.toLowerCase().compareTo("socio") == 0) {
                            //play socio beep
                            playMp3Resource(R.raw.ding);
                        } else {
                            //play no socio beep
                            playMp3Resource(R.raw.dingdong);
                        }
                    } catch (Exception ex) {
                        Log.e(TAG, "Exception: " + ex.getMessage());
                        it.extraMessage = ex.getMessage();
                        playMp3Resource(R.raw.error);
                        //UIHelper.msbox("Error",ex.getMessage(),ScanActivity.this);
                    } finally {
                        closeProgressBar();
                        runOnUiThread(new Runnable() {
                            public void run() {
                                adapter.notifyDataSetChanged();
                                txtmanualbarcode.setText("");
                                txtmanualbarcode.requestFocus();
                                txtmanualbarcode.setInputType(InputType.TYPE_NULL);


                            }
                        });
                    }
                }
            };
            thr.start();
        } else {
            txtmanualbarcode.requestFocus();
        }
    }

我的問題(可能)存在於第一個for循環中。

一種選擇是使用InputMethodManager使其隱藏鍵盤,而您的輸入不會失去焦點。

使用下面定義的幫助器 class 可以更新代碼並擺脫使用setInputType(InputType.TYPE_NULL) 此外,在再次請求焦點之前不要清除焦點。 沒有用處,而且可能是錯誤的來源。

    for (int a = 0; a < scannedItems.size(); a++) {
        if (scannedItems.get(a).qr_code.compareTo(it.qr_code) == 0) {
            txtmanualbarcode.setText("");
            txtmanualbarcode.requestFocus();
            KeyboardHelper.hideKeyboard(txtmanualbarcode);
            duplicado = true;

        }
    }

隱藏鍵盤而不失去焦點的代碼:

public class KeyboardHelper {

    public static void hideKeyboard(View view) {
        if (view.getContext() == null) {
            Log.d(KeyboardHelper.class.getSimpleName(), "hideKeyboard failed. View detached");
            return;
        }

        InputMethodManager mgr = getInputManagerFromContext(view.getContext());

        if (mgr != null) {
            mgr.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    private static InputMethodManager getInputManagerFromContext(Context context) {
        return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM