gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:41 25 4
gpt4 key购买 nike

我的代码是:

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

我也有一个拨号盘。当我在拨号盘中单击特定号码时,我需要将该号码添加到当前光标位置。另外,当我按下删除键时,我需要从当前光标位置删除数字。

拨号盘图片

enter image description here

最佳答案

试试下面的代码

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

这是从 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

关于android - 在编辑文本的光标位置之间插入字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7661104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com