【问题标题】:Edit text loses focus after errasing the value and interacting with a listview删除值并与列表视图交互后,编辑文本失去焦点
【发布时间】:2020-11-19 22:27:29
【问题描述】:

我的程序有一个需要始终聚焦的编辑文本字段,但由于某种原因,在它检测到数据重复并将其删除后,它应该重新聚焦但它没有。

下面的此代码检查代码是否与最后扫描的 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 循环中。

【问题讨论】:

    标签: java android android-listview android-edittext


    【解决方案1】:

    其中一个选项是使用InputMethodManager 使其隐藏键盘,而您的输入不会失去焦点。

    使用下面定义的帮助类,您可以更新代码并摆脱使用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);
        }
    }
    

    【讨论】:

    • 我尝试了您的代码并删除了所有 setinputtype 调用,但我的问题仍然存在,我将继续调查。
    猜你喜欢
    • 2016-05-30
    • 1970-01-01
    • 2020-05-26
    • 2021-10-08
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多