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

2023-09-04 23:41:25 作者:缠心绊情

我的code是:

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();

                    }

我有一个拨号盘太多。当我点击拨号盘一个特定的数字,我需要那个号码添加到当前光标现在的位置....还..当我preSS也删除,我需要要删除当前光标现在的位置是多少?请帮助

I have a dialpad too.. When i click a particular number in the dial pad , i need to add that number to the current cursor postion.... also.. when i press delete also, i need to delete the number from the current cursor postion... Pls help

拨号盘图片

推荐答案

请尝试以下code

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

下面是code到删除的EditText选中的文本

Here is the code to delete selected text from 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