如何在android中编辑文本中添加"+91"并删除"+91"?

vis*_*was 1 android

我有一个活动,其中我有移动编辑文本我想要的当用户输入手机号码我想在号码前添加"+91",当用户删除完整号码我也想删除"+91".我怎么能这样做

TextWatcher m_MobileWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!s.toString().contains("+91")) {
            m_InputMobie.setText("+91" + s.toString());
            Selection.setSelection(m_InputMobie.getText(), m_InputMobie.getText().length());
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

Nir*_*tel 5

尝试以下代码,这工作正常,我已经检查过.

TextWatcher m_MobileWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String text_value = m_InputMobie.getText().toString().trim();
            if(text_value.equalsIgnoreCase("+91"))
            {
                m_InputMobie.setText("");
            }else
            {
                if(!text_value.startsWith("+91") && text_value.length()>0) {
                    m_InputMobie.setText("+91" + s.toString());
                    Selection.setSelection(m_InputMobie.getText(), m_InputMobie.getText().length());
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
Run Code Online (Sandbox Code Playgroud)