问题
I have an app that lets you click buttons to add and subtract +5 or -5 and +1 or -1 from a starting value of 20. I have it set up so that when a button is clicked it will put that value into a string and display it so that the user can see a history of what they have pressed. I have a method called reset(); that resets the starting value back to 20, I would like to know how to also clear the string values
add5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter += 5;
updateDisplay();
// add the click history for p1
String tmText = (String) btnPressedTV.getText();
btnPressedTV.setText(tmText + "\n" + String.valueOf(counter));
}
});
sub5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter -= 5;
updateDisplay();
// add the click history for p1
String tmText = (String) btnPressedTV.getText();
btnPressedTV.setText(tmText + "\n" + String.valueOf(counter));
}
});
void reset() {
// TODO Auto-generated method stub
counter = 20;
display.setText(String.valueOf(20));
display.setTextColor(Color.BLACK);
tmText = "";
}
回答1:
Try this..
Use your String
tmText in Global
like below and as like your integer counter
String tmText;
int counter;
and reset
method
public void reset()
{
tmText = "";
counter = 20;
}
回答2:
Make sure that the String
should be class variable and not local variable.
To clear a call variable String value try this
class Sample
{
String str; // class variable (string)
int counter=20; // class variable (integer)
:
:
:
reset.setOnClickListener(new View.OnClickListener() { // button click
@Override
public void onClick(View v) {
str = ""; // you can clear the string value like this
counter = 20; // you can reset the integer value like this
}
});
:
:
:
public void resetfunction() // method
{
str = ""; // you can clear the string value like this
counter = 20; // you can reset the integer value like this
}
:
:
:
}
回答3:
You can reset ur String by assigning double quotes to it by
str = "";
回答4:
I suggest using implementing OnClickListener in your class. The difference explained here The different OnClickListener implementation ways
Here is some code to explain what I mean:
public class MainActivity extends Activity implements OnClickListener {
Button add5;
Button sub5;
Button reset;
int counter;
String tmText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//init the counter var
counter = 20;
//get handles to the buttons in your xml declaration
add5 = (Button) findViewById(R.id.add5);
sub5 = (Button) findViewById(R.id.sub5);
reset = (Button) findViewById(R.id.reset);
//attach the buttons the onclickListener
add5.setOnClickListener(this);
sub5.setOnClickListener(this);
reset.setOnClickListener(this);
@Override
public void onClick(View arg0) {
switch(arg0.getId()) {
case R.id.add5:
// +5 code
break;
case R.id.sub5:
// minus 5 code
break;
case R.id.reset;
counter = 20;
tmText = "";
}
}
}
If you are looking to clear the values displayed in a textview:
TextView myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText("");
来源:https://stackoverflow.com/questions/20736863/clear-a-value-from-a-string-android