以前的 EditText 值按要求清除,但不允许新用户输入

Previous EditText value clears as requested but does not allow for new user input

我正在处理包含搜索栏的片段,搜索栏值会在 editText 元素中更新(如果使用)。用户还可以通过在 editText 上输入值来更新搜索栏。(这一切都有效,直到我实现了先前值的清除)

seekbar默认为500(中点),textView显示500。随着seekbar进度的变化,editText中的值与之匹配。 (此类作品)

当用户通过 selecting EditText 元素输入新值时,我希望 500(或任何值)对于新输入消失。 (这有效)。

问题是:每次我在软键盘上输入一个新值并打勾 select 时,新值就会被清除,软键盘仍然保持打开状态,并且进度条或编辑文本没有更新。做错了什么?

SEEKBAR - 更新 EditText

private int radius = 500;
//START SEEKBAR DISTANCE
distControl=(SeekBar)view.findViewById(R.id.sb_loc_dist_set);
txtInMaxMiles=(EditText)view.findViewById(R.id.txt_max_num_input);
distControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()

{
    int progressChange = 0;

    @Override
    public void onProgressChanged (SeekBar seekBar,int progress, boolean fromUser){
    radius = progress;
    txtInMaxMiles.setText(String.valueOf(radius));
}
    @Override
    public void onStartTrackingTouch (SeekBar seekBar){
    distControl.setProgress(radius);
}
    @Override
    public void onStopTrackingTouch (SeekBar seekBar){
    txtInMaxMiles.setText(String.valueOf(radius));
}});//END: SEEK BAR DISTANCE

编辑文本 - 更新进度条

 txtInMaxMiles.addTextChangedListener(new TextWatcher() {
        String txtIn;

        @Override //Before screen fully loads
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
           // txtIn = txtInMaxMiles.getText().toString();
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            txtIn = txtInMaxMiles.getText().toString();
        }
        @Override
        public void afterTextChanged(Editable s) {

            txtIn = txtInMaxMiles.getText().toString();
            if (txtIn == null || txtIn.trim().equals("")) {
                radius = 0;
            }
            if (radius < 1 || radius > 1000) {
                txtInMaxMiles.setError("Please input a number between 0 and 1001");
            } else {
                txtInMaxMiles.setError(null);
                distControl.setProgress(Integer.parseInt(s.toString()));
               // txtInMaxMiles.setText(Integer.toString(radius));
            }
        }
    });

点击EditText元素

  txtInMaxMiles.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtInMaxMiles.setText("");
            }
        });
        return view;

我认为这是一个快速修复,但我只是遗漏了一些东西,因此我们将不胜感激。

好的,成功实现了 out.This 我没有对半径进行硬编码(调皮),使代码更清晰。然而,即使在这样做之后......OnClickStill 没有工作。所以我尝试了另一种方法,当用户输入新值时突出显示并替换文本,这个线程很有用:Select all text inside EditText when it gets focus 但是,我仍然尝试了 onClick ......但它没有用,即使它更接近了一步。 最后,Lukas Batteau 在此线程上挽救了这一天 Android EditText onClickListener 我没有使用 onClick,而是改为使用 OnTouchListener。

 txtInMaxMiles.setSelectAllOnFocus(true);
        txtInMaxMiles.addTextChangedListener(new TextWatcher() {
            int txtIn;

            @Override //Before screen fully loads
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                txtInMaxMiles.clearFocus();
                txtInMaxMiles.requestFocus();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                txtIn = Integer.parseInt(txtInMaxMiles.getText().toString());

                if (txtInMaxMiles.getText().toString() == null || txtInMaxMiles.getText().toString().trim().equals("")) {
                }
                if (txtIn < 1 || txtIn > 1000) {
                    txtInMaxMiles.setError("Please input a number between 0 and 1001");
                } else {

                    txtInMaxMiles.setError(null);
                    distControl.setProgress(Integer.parseInt(s.toString()));
                }
            }
        });

        txtInMaxMiles.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_UP == event.getAction()) {
                    txtInMaxMiles.clearFocus();
                    txtInMaxMiles.requestFocus();
                }
                return false; // return is important...
            }
        });
        return view;
    }

好的,所以我仍然是一个新手,所以任何明确的编码建议将不胜感激,如果有人知道 onClickListener 不起作用的原因,请告诉我,因为我不知道为什么。