Android-删除按键上的字符

Android-删除按键上的字符,android,button,arraylist,Android,Button,Arraylist,下面是我编写的为计算器输入数字的方法。代码功能齐全,无错误 我想知道如何编写一个名为backspace的单独方法,一次从用户输入中删除一个字符 为了举例说明onClick1是如何工作的(只是为了清楚地说明我想做什么),如果我输入2+4*6(使用按钮;它们的onClick操作链接到onClick1),那么textViewCalcHistExp1将显示文本2+4*6,而arrayList将保存以下值:[2,+,4,*,6] 我希望backspace起作用,这样,如果我单击按钮(与backspace方

下面是我编写的为计算器输入数字的方法。代码功能齐全,无错误

我想知道如何编写一个名为
backspace
的单独方法,一次从用户输入中删除一个字符

为了举例说明
onClick1
是如何工作的(只是为了清楚地说明我想做什么),如果我输入
2+4*6
(使用按钮;它们的onClick操作链接到
onClick1
),那么
textViewCalcHistExp1
将显示文本
2+4*6
,而
arrayList
将保存以下值:
[2,+,4,*,6]

我希望
backspace
起作用,这样,如果我单击按钮(与
backspace
方法链接),
textViewCalcHistExp1
的显示现在将是
2+4*
arrayList
将保存以下值:
[2,+,4,*]

代码如下:

ArrayList<String> arrayList = new ArrayList<String>();
String stringInput = "";
String stringInputWithOp = "";
public String prevCalc = "";

public void onClick1 (View view) {
    TextView textViewCalcHistExp1 = (TextView) findViewById(R.id.textViewCalcHistExp1);
    Button button = (Button) view;
    stringInput = (String) button.getText().toString();


    if (!stringInput.contains("+") && !stringInput.contains("-") && !stringInput.contains("×") && !stringInput.contains("÷")) {
        stringInputWithOp = stringInputWithOp+stringInput;
        if (arrayList.size()>0) {
            arrayList.remove((arrayList.size()-1));
        }
        arrayList.add(stringInputWithOp);
    }
    else {
        arrayList.add(stringInput);
        arrayList.add(stringInput);
        stringInputWithOp="";
    }
    //This version truncates array formatting i.e. entering "2+4*6" would display "2+4*6"
    textViewCalcHistExp1.setText(textViewCalcHistExp1.getText().toString()+stringInput);

    //This version leaves array formatting i.e. entering "2+4*6" would display [2,+,4,*,6] ;good for debugging
    //textViewCalcHistExp1.setText(arrayList.toString());
}
ArrayList ArrayList=new ArrayList();
字符串stringInput=“”;
字符串stringInputWithOp=“”;
公共字符串prevCalc=“”;
公共void onClick1(视图){
TextView textViewCalcHistExp1=(TextView)findViewById(R.id.textViewCalcHistExp1);
按钮=(按钮)视图;
stringInput=(字符串)按钮。getText().toString();
如果(!stringInput.contains(“+”&&!stringInput.contains(“-”&&!stringInput.contains(“×”&!stringInput.contains(“÷”)){
stringInputWithOp=stringInputWithOp+stringInput;
如果(arrayList.size()>0){
移除((arrayList.size()-1));
}
添加(stringInputWithOp);
}
否则{
添加(stringInput);
添加(stringInput);
stringInputWithOp=“”;
}
//此版本截断数组格式,即输入“2+4*6”将显示“2+4*6”
textViewCalcHistExp1.setText(textViewCalcHistExp1.getText().toString()+stringInput);
//此版本保留数组格式,即输入“2+4*6”将显示[2,+,4,*,6];有利于调试
//textViewCalcHistExp1.setText(arrayList.toString());
}

您可以这样做:

private void backspace() {
    if (!arrayList.isEmpty()) {
        // removing the last item from the ArrayList
        arrayList.remove(arrayList.size() - 1);
    }

    String string = textViewCalcHistExp1.getText().toString();
    if (string.length() > 0) {
        // removing the last character from the TextView
        textViewCalcHistExp1.setText(string.substring(0, string.length() - 1));
    }
}
您还需要将
textViewCalcHistExp1
作为一个实例变量(
arrayList
已经是),方法是在
onClick1()之外声明它