2013-12-23 21 views
2

我有一個應用程序,讓你點擊按鈕加減+5或-5和+1或-1從20起始值我有它設置,以便當按鈕被點擊,它會將該值放入一個字符串中並顯示,以便用戶可以看到他們按下的內容的歷史記錄。我有一個名爲reset()的方法;一個重置初始值回至20,我想知道如何也很清楚該字符串值明確從字符串的Android值

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

} 
+0

存儲在您的值20在'counter'變量中?你可以顯示你的'reset()'方法的代碼嗎? – GrIsHu

+0

看到我的回答........ –

+0

在時間reset.set的字符串值作爲'null'。等於0 – Yugesh

回答

1

試試這個..

使用您StringtmTextGlobal像下面和喜歡你的整數counter

String tmText; 
int counter; 

reset方法

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

計數器值我說你有什麼在這裏,它仍然無法重設串當復位函數被調用。 – user3104719

+0

@ user3104719那裏有你所謂的'復位()'方法 – Hariharan

+0

我打電話通過菜單選項復位功能,它重置INT計數器回到20的初始值,以及重置文本黑色的顏色(它根據數字更改爲紅色或綠色)。那些會像他們應該重置,字符串不會。 – user3104719

0

確保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 
} 
: 
: 
: 
} 
0

您可以通過

通過指定雙引號將其重置UR字符串str =「」;

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顯示的值:

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