1

我想一一删除我的edittext中的字符。我研究了很多,但有一些问题,请指教。这是我的示例代码。

我创建了一个删除按钮"ImageButton buttonDelete;"// XML imageButton1 ,我的编辑文本是"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);
4

2 回答 2

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); 
}
于 2012-07-07T11:04:59.933 回答
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 );

            }
于 2017-08-20T13:45:44.167 回答