Android 从字符串中清除值

Android 从字符串中清除值,android,string,reset,Android,String,Reset,我有一个应用程序,可以让你点击按钮,在起始值20的基础上加减+5或-5和+1或-1。我将其设置为,当单击按钮时,它会将该值放入字符串中并显示,以便用户可以查看他们按下的按钮的历史记录。我有一个叫做reset()的方法;这会将起始值重置回20,我想知道如何同时清除字符串值 add5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // T

我有一个应用程序,可以让你点击按钮,在起始值20的基础上加减+5或-5和+1或-1。我将其设置为,当单击按钮时,它会将该值放入字符串中并显示,以便用户可以查看他们按下的按钮的历史记录。我有一个叫做reset()的方法;这会将起始值重置回20,我想知道如何同时清除字符串值

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 = ""; 

}

确保
字符串
应该是类变量而不是局部变量。
要清除调用变量字符串值,请尝试以下操作

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
 }
 :
 :
 :
}

您可以通过为字符串指定双引号来重置字符串

str=”“试试这个

全局
中使用
字符串
tmText,如下所示,并使用整数计数器

String tmText;
int counter;
和重置方法

public void reset()  
{
    tmText = ""; 
    counter = 20; 
}

我建议在您的类中使用实现OnClickListener。这里解释了区别

下面是一些代码来解释我的意思:

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 = "";
        }
    }
}
如果要清除文本视图中显示的值:

TextView myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText("");

值20是否存储在
计数器
变量中?您可以显示
reset()
方法的代码吗?请参阅我的答案……。在重置时。将字符串值设置为
null
。计数器值等于0。我在这里添加了您的值,但在调用重置函数时它仍然不会重置字符串。@user3104719您在哪里调用了
reset()
method我通过菜单选项调用重置函数,它会将int计数器重置回起始值20,并将文本颜色重置为黑色(根据数字,它会变为红色或绿色)。它们会像它们应该的那样重置,字符串不会。@user3104719在add
btnPressedTV.setText(“”;
我输入错误,它会像你说的那样工作。谢谢!