我想逐个删除编辑文本中的字符。我研究了很多,但有一些问题,请指教。这是我的示例代码。
我创建了一个删除按钮"ImageButton buttonDelete;"// XML imageButton1
我的edittext是"EditText display;"
display = (EditText) findViewById(R.id.editText1);
buttonDelete.setOnClickListener(new View.OnClickListener()
{
public void onClick()
{
// Get edit text characters
String textInBox = display.getText():
//Remove last character//
String newText = textInBox.substring(0, textInBox.length()-1);
// Update edit text
display.setText(newText);
答案 0 :(得分:3)
试试这个:
// Get edit text characters
String textInBox = display.getText().toString();
if(textInBox.length() > 0)
{
//Remove last character//
String newText = textInBox.substring(0, textInBox.length()-1);
// Update edit text
display.setText(newText);
}
答案 1 :(得分:0)
BtonBackSpace.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String bs = null;
if(Tfield.getText().length()>0){
StringBuilder Strb = new StringBuilder(Tfield.getText());
Strb.deleteCharAt(Tfield.getText().length() - 1);
bs = Strb.toString();
Tfield1.setText(bs );
}