在编辑文本中的光标位置之间插入字符

jen*_*fer 8 android

我的代码是:

EditText edt

    edt.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {

            final String number = edt.getText().toString();

            int count = arg0.length();
            edt.setSelection(count);

        }

                    }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

            }

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

            final String number = edt.getText().toString();

                    }
Run Code Online (Sandbox Code Playgroud)

我也有一个拨号盘.当我点击拨号盘中的特定号码时,我需要将该号码添加到当前光标位置.此外,当我按删除时,我需要删除当前光标位置的数字.

拨号盘图片

在此输入图像描述

Aju*_*Aju 26

请尝试以下代码

int start =editText.getSelectionStart(); //this is to get the the cursor position
String s = "Some string";
editText.getText().insert(start, s); //this will get the text and insert the String s into   the current position
Run Code Online (Sandbox Code Playgroud)

以下是从EditText中删除所选文本的代码

int start = t1.getSelectionStart();  //getting cursor starting position
int end = t1.getSelectionEnd();      //getting cursor ending position
String selectedStr = t1.getText().toString().substring(start, end);    //getting the selected Text
t1.setText(t1.getText().toString().replace(selectedStr, ""));    //replacing the selected text with empty String and setting it to EditText
Run Code Online (Sandbox Code Playgroud)