清除之前在 EditText 字段中输入的文本

发布于 2024-11-05 09:48:43 字数 2158 浏览 0 评论 0原文

嘿, 我试图让我的 editText 窗口在以下情况下清除其内容: 1)重新选择输入不同的数据或 2) 按下按钮输入预定数据

这不是提示文本。目前,一旦输入预定数据,它就会保留在那里,即使使用删除,我也无法清除它。 我尝试将 editBox.setText("") 放入 onClick Listener 和 addTextChangedListener (之前和 on 方法)中,使用布尔值来确定代码是否已输入,但它只是忽略它。

foodBox.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {

        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            addFood();
            valueAdded=true;
        }           
    });


    dogButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (valueAdded==true){
                foodBox.setText("");
                setFood(0.0);
            }
            isClicked=true;
            animal=1;
            addFood();
            valueAdded=true;//value in the box
                }


    });


    catButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (valueAdded){
               foodBox.setText("");}
            isClicked=true;
            animal=2;
            addFood();
            valueAdded=true;
            }

    });
            private void addFood() {
    try{
        aClass aob = new aClass();
        setFood(0.0);
        String a = this.foodBox.getText().toString();
        if(isClicked&&TextUtils.isEmpty(a)){
            if(animal==1){
                a=mob.getDog(); 
            }
            if(animal==2){
                a=mob.getCat();
            }
            this.foodBox.setText(a);
            double d=Double.parseDouble(a);
            setFood(d);
        }else{
            if(TextUtils.isEmpty(a)){
            a="0";
        }
        double d=Double.parseDouble(a);
        setFood(d);
        }
    } 
        catch (Exception e){
        this.showAnswer.setText("Please input animals in numbers");
        addFood();
    }
}

谢谢

Hey,
I'm trying to get my editText window to clear its contents when either:
1) It is reselected to enter different data or
2) A button is pressed to input predetermined data

This is not hint text. At the moment once the predetermined data is input it stays there and I can't clear it, even using delete.
I have tried putting editBox.setText("") in the onClick Listener and the addTextChangedListener (both before and on methods) using a boolean to determine if code has already been input, but it just ignores it.

foodBox.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {

        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            addFood();
            valueAdded=true;
        }           
    });


    dogButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (valueAdded==true){
                foodBox.setText("");
                setFood(0.0);
            }
            isClicked=true;
            animal=1;
            addFood();
            valueAdded=true;//value in the box
                }


    });


    catButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (valueAdded){
               foodBox.setText("");}
            isClicked=true;
            animal=2;
            addFood();
            valueAdded=true;
            }

    });
            private void addFood() {
    try{
        aClass aob = new aClass();
        setFood(0.0);
        String a = this.foodBox.getText().toString();
        if(isClicked&&TextUtils.isEmpty(a)){
            if(animal==1){
                a=mob.getDog(); 
            }
            if(animal==2){
                a=mob.getCat();
            }
            this.foodBox.setText(a);
            double d=Double.parseDouble(a);
            setFood(d);
        }else{
            if(TextUtils.isEmpty(a)){
            a="0";
        }
        double d=Double.parseDouble(a);
        setFood(d);
        }
    } 
        catch (Exception e){
        this.showAnswer.setText("Please input animals in numbers");
        addFood();
    }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丶视觉 2024-11-12 09:48:43

呃...

我用来实现 1) 的唯一代码是:

    // Clear text when clicked
    host.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            host.setText("");
        }
    });

host 只是一个 EditText。效果好吗?我认为你让事情变得比实际情况更复杂。

Errr...

The only code I use to achieve 1) is:

    // Clear text when clicked
    host.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            host.setText("");
        }
    });

host just being an EditText. Works just fine? I think you are making this more complicated than it is.

梦屿孤独相伴 2024-11-12 09:48:43

单击“清除”按钮时清除:

firstName = (EditText) findViewById(R.id.firstName);

clear = (Button) findViewById(R.id.clearsearchSubmit);

clear.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.clearsearchSubmit);
        firstName.setText("");
    }
});

它对我有用。希望有帮助。

To clear When Clear Button is Clicked:

firstName = (EditText) findViewById(R.id.firstName);

clear = (Button) findViewById(R.id.clearsearchSubmit);

clear.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.clearsearchSubmit);
        firstName.setText("");
    }
});

It Worked For me. Hope It Helps.

等风来 2024-11-12 09:48:43

1)onClick 对我不起作用,因为我必须按两次才能调用 onClickListener。其他方法是 setOnFocusChangeListener()

editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    editTextView.getText().clear();
                }
            }
        });

For 2) 由ashim888 给出的解决方案效果很好。

For 1) onClick did not work for me as I had to press twice to call onClickListener. Other approach would be setOnFocusChangeListener()

editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    editTextView.getText().clear();
                }
            }
        });

For 2) solution given by ashim888 works well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文