清除字符串android中的值

时间:2013-12-23 04:10:22

标签: android string reset

我有一个应用程序,让你点击按钮来添加和减去+5或-5和+1或-1从起始值20.我已经设置,以便当点击一个按钮时,它将把它将值写入字符串并显示它,以便用户可以看到他们按下的内容的历史记录。我有一个名为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 = ""; 

}

4 个答案:

答案 0 :(得分:1)

试试这个..

使用String中的Global tmText ,如下所示整数 counter

String tmText;
int counter;

reset 方法

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

答案 1 :(得分:0)

确保String应该是类变量而不是局部变量 要清除调用变量String值,请尝试此

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

答案 2 :(得分:0)

您可以通过

为其指定双引号来重置字符串

str =“”;

答案 3 :(得分:0)

我建议在您的课程中使用OnClickListener。这里解释的差异是The different OnClickListener implementation ways

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

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("");