What Is The Best Way To Delete The Last Digit In A Calculator App
package com.gamesup.braingame; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; publ
Solution 1:
You can remove the last character in a string by using the substring
method.
In the method where the delete press is being captured:
Strings= display.getText().toString();
s = s.substring(0, s.length() - 1);
display.setText(s);
This will replace the string s with the last character removed
The full code to do this would be:
package com.gamesup.braingame;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
publicclassEasyextendsActivity {
EditText display;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easy);
display = (EditText)findViewById(R.id.displayText);
display.setText("?");
}
staticbooleanisEmpty=true;
publicvoidnum_Clicked(View v){
Buttonbtn= (Button) v;
if (v.getId()== R.id.del_button){
Strings= display.getText().toString();
s = s.substring(0, s.length() - 1);
display.setText(s);
return;
}
if(isEmpty){
display.setText(btn.getText());
isEmpty = false;
}
else{
display.append(btn.getText().toString());
}
}
}
Post a Comment for "What Is The Best Way To Delete The Last Digit In A Calculator App"