简体   繁体   English

清除前缀编辑文本值

[英]Clear the prefix edit text value

I Am trying to append the prefix with the entered edit text . 我正在尝试在前缀后面附加输入的编辑文本。 It's working perfectly with the below code. 它与下面的代码完美配合。 But the problem is am not able to clear the prefix text when i press the cancel button. 但是问题是当我按下取消按钮时,无法清除前缀文本 I want ₹ to be cleared as well when keyboard cancel button is pressed so that i can see the edit text hint again 我也希望在按下键盘取消按钮时也清除₹,以便再次看到编辑文本提示

Code : 代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText editText = (EditText)findViewById(R.id.edit);
        editText.setText("");
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                    if(!editText.getText().toString().startsWith("₹**₹**")){
                        editText.setText("₹**₹**"); // not able to use that sign , so that's why those asterisk. It's actually just ₹ 
                        Selection.setSelection(editText.getText(), editText.getText().length());

                    }
            }
        });
    }
}

Edit text value plus prefixed with dollar sign 编辑文本值以及带美元符号的前缀

in this picture , i want to clear the sign when pressing the cancel button 在这张照片中,我想在按取消按钮时清除标志

What does the cancel button do? 取消按钮有什么作用? Does it remove all the text from the EditText? 它会从EditText中删除所有文本吗? If so, you could check in the afterTextChanged method if the text in the EditText is empty. 如果是这样,您可以在afterTextChanged方法中检查EditText中的文本是否为空。 Only if it is not empty, add the sign. 仅当它不为空时,才添加符号。 Your method could look like this: 您的方法可能如下所示:

@Override
public void afterTextChanged(Editable editable) {
    if(!editText.getText().toString().startsWith("₹**₹**")){
        editText.setText("₹**₹**"); // not able to use that sign , so that's why those asterisk. It's actually just ₹ 
        Selection.setSelection(editText.getText(), editText.getText().length());

    } else {
        // The text starts with the sign, if it is equal to it, set the text to be empty
        if(editText.getText().toString().equals("₹**₹**")){
            editText.setText("");
        }
    }
}

I'm sure this could be refactored to a cleaner solution but this should work for you. 我敢肯定,可以将其重构为更清洁的解决方案,但这应该对您有用。

The problem you have to deal with is the following: By cleaning the text in the EditText you are changing the text and this triggers the addTextChangedListener methods. 您必须解决的问题如下:通过清除EditText中的文本,您可以更改文本,这将触发addTextChangedListener方法。

Set some Boolean flag while clicking your Cancel button & check that flag value inside the afterTextChanged method. 在单击“取消”按钮时设置一些布尔标志,并在afterTextChanged方法内检查该标志值。

  1. Declare boolean isCancel = false; 声明boolean isCancel = false;
  2. Your cancel button click listener 您的取消按钮点击监听器

     btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isCancel = true; editText.setText(""); } }); 
  3. Change afterTextChanged 更改afterTextChanged

     @Override public void afterTextChanged(Editable editable) { if(!isCancel) { if(!editText.getText().toString().startsWith("₹**₹**")){ editText.setText("₹**₹**"); Selection.setSelection(editText.getText(), editText.getText().length()); } } else { isCancel = false; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM