Domanda

I need to intercept when a user in my Android App want to paste a number that have copied somewhere in a EditText. If a number have international prefix +39 XXXXXXXX I want remove +39 before the paste on selected EditText. I've try to search for solve this issue and try to implement in various solutions but nothing work for me. For example I've try this:

  txtNumber1.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d("txtNumber1","onTextChanged");
            Log.d("txtNumber1",s.toString());
            }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d("txtNumber1","afterTextChanged");
            Log.d("txtNumber1",s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            Log.d("txtNumber1","beforeTextChanged");
            Log.d("txtNumber1 sequence",s.toString());
            Log.d("txtNumber1 start",Integer.toString(start));
            Log.d("txtNumber1 count",Integer.toString(count));
            Log.d("txtNumber1 after",Integer.toString(after));
        }
    });

But beforeTextChanged doesn't show string that I want manipulate. Edit: For be more clear. I want intercept if a user want paste in a EditText a text that he have copy somewhere and manipulate this string before paste it. My problem isn't how to manipulate a string but how to intercept paste event before it happens.

È stato utile?

Soluzione

Ok I've solved.

I intercept the contents of the clipboard before paste, manipulate it and come back to normal flow of execution. Here the code:

txtNumber.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d("txtNumber","onTextChanged");
            Log.d("txtNumber",s.toString());
            }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d("txtNumber","afterTextChanged");
            Log.d("txtNumber",s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            Log.d("txtNumber","beforeTextChanged");
            Log.d("txtNumber sequence",s.toString());
            Log.d("txtNumber start",Integer.toString(start));
            Log.d("txtNumber count",Integer.toString(count));
            Log.d("txtNumber after",Integer.toString(after));

            // Gets a handle to the clipboard service.
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

            String pasteData = "";
            // If the clipboard doesn't contain data, disable the paste menu item.
            // If it does contain data, decide if you can handle the data.
            if (clipboard.hasPrimaryClip()) {


                // Examines the item on the clipboard. If getText() does not return null, the clip item contains the
                // text. Assumes that this application can only handle one item at a time.
                 ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

                // Gets the clipboard as text.
                pasteData = item.getText().toString();

                // If the string contains data, then the paste operation is done
                if (pasteData != null) {
                    Log.d("Paste",pasteData);
                    pasteData = pasteData.replace("+39", "");
                    pasteData = pasteData.replace(" ", "");                     
                    if (pasteData.length()!=10){
                        pasteData="";
                    }else{
                        String pattern= "^[0-9]*$";
                        if(!pasteData.matches(pattern)){
                            pasteData="";
                        }
                    }
                    ClipData clip = ClipData.newPlainText("simple text",pasteData);
                    clipboard.setPrimaryClip(clip);
                }
            }

        }

    });

Altri suggerimenti

Why don't you just use String methods to remove the first 3 characters of your string, I don't think you need to use the Text Changed events on the EditText.

String phone = txtNumber1.getText().toString();
String changedString = phone.substring(2, phone.length())
txtNumber1.setText(changedString);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top