Java 如何在Android中删除TextView中的最后一个字符?
我正在为一个类创建一个计算器应用程序,除了“BackSpace”按钮之外,我所有的东西都在工作。关于操作TextView,我能找到的唯一信息是使用SetText方法将TextView重置为null或仅为空字符串。但我需要做的是删除最后输入计算器的数字例如:如果输入数字12并按下退格按钮,它将删除2,但保留1。我决定只包括我的“onClick”方法,因为它是唯一与这个问题相关的方法。所有的计算都是用另一种方法完成的。谢谢Java 如何在Android中删除TextView中的最后一个字符?,java,android,textview,Java,Android,Textview,我正在为一个类创建一个计算器应用程序,除了“BackSpace”按钮之外,我所有的东西都在工作。关于操作TextView,我能找到的唯一信息是使用SetText方法将TextView重置为null或仅为空字符串。但我需要做的是删除最后输入计算器的数字例如:如果输入数字12并按下退格按钮,它将删除2,但保留1。我决定只包括我的“onClick”方法,因为它是唯一与这个问题相关的方法。所有的计算都是用另一种方法完成的。谢谢 public void onClick(View v) {
public void onClick(View v) {
// display is assumed to be the TextView used for the Calculator display
String currDisplayValue = display.getText().toString();
Button b = (Button)v; // We assume only buttons have onClickListeners for this App
String label = b.getText().toString(); // read the label on the button clicked
switch (v.getId())
{
case R.id.clear:
calc.clear();
display.setText("");
//v.clear();
break;
case R.id.plus:
case R.id.minus:
case R.id.mult:
case R.id.div:
String operator = label;
display.setText("");
calc.update(operator, currDisplayValue);
break;
case R.id.equals:
display.setText(calc.equalsCalculation(currDisplayValue));
break;
case R.id.backSpace:
// Do whatever you need to do when the back space button is pressed
//Removes the right most character ex: if you had the number 12 and pressed this button
//it would remove the 2. Must take the existing string, remove the last character and
//pass the new string into the display.
display.setText(currDisplayValue);
break;
default:
// If the button isn't one of the above, it must be a digit
String digit = label;// This is the digit pressed
display.append(digit);
break;
}
}
使用
它将允许您按索引替换/删除字符(在您的情况下,它将是字符串的最后一个索引)
如果您有号码,请输入1829384
长度为7,索引将从0开始
当使用子字符串时,它将是从0到(7-1),因此新字符串将是182938我考虑使用子字符串方法,但我认为您需要知道要使其工作,您需要查找的数字。我试试看谢谢!是的,谢谢,现在似乎是个愚蠢的问题。一点也不。很高兴知道它起作用了:)
NumberEntered = NumberEntered.substring(0, NumberEntered.length() - 1);