简体   繁体   中英

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? If so, you could check in the afterTextChanged method if the text in the EditText is empty. 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.

Set some Boolean flag while clicking your Cancel button & check that flag value inside the afterTextChanged method.

  1. Declare 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

     @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; } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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