2011-12-16 95 views
0

我的一些編碼如下。第一個按鈕(button2)用於退出,在我的模擬器和手機上工作正常,但應該從TextView獲取輸入並添加777然後顯示爲Toast消息的(按鈕1)在其中任何一箇中都不起作用。需要關於這種情況的更多信息

public class TimerCodeActivity extends Activity { 

protected TextView timebox; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.timerlayout); 

    timebox = (TextView) findViewById(R.id.timebox); 

    Button button2 = (Button) findViewById(R.id.button2); 
    button2.setOnClickListener(new OnClickListener(){ 

     @Override  
     public void onClick(View v) { 
      finish(); 
      System.exit(0);} 
    });  

    Button button1 = (Button) findViewById(R.id.button1); 
    button1.setOnClickListener(new OnClickListener(){  

     @Override 
     public void onClick(View v) { 
      String timeboxRepresentation = (String) timebox.getText(); 

      int Digit = Integer.parseInt(timeboxRepresentation.substring(0,2)); 
      int total = Digit + 777; 

      Toast.makeText(TimerCodeActivity.this, String.valueOf(total), Toast.LENGTH_LONG).show(); 
     } 
    }); 
}} 

非常感謝您的幫助。

+0

不工作在什麼方式,它顯示什麼?請考慮更好的標題 – codeling 2011-12-16 13:53:46

+0

這裏有什麼問題和錯誤?timeboxRepresentation包含哪些內容? – Pratik 2011-12-16 13:54:24

回答

1

試試這個:

public class TimerCodeActivity extends Activity { 

protected TextView timebox; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.timerlayout); 

    timebox = (TextView) findViewById(R.id.timebox); 

    Button button2 = (Button) findViewById(R.id.button2); 
    button2.setOnClickListener(new OnClickListener(){ 

     @Override  
     public void onClick(View v) { 
      finish(); 
     } 
    });  

    Button button1 = (Button) findViewById(R.id.button1); 
    button1.setOnClickListener(new OnClickListener(){  

     @Override 
     public void onClick(View v) { 
      // Use toString to get the TextView content 
      String timeboxRepresentation = timebox.getText().toString(); 

      int Digit = Integer.parseInt(timeboxRepresentation.substring(0,2)); 
      int total = Digit + 777; 

      Toast.makeText(TimerCodeActivity.this, String.valueOf(total), Toast.LENGTH_LONG).show(); 
     } 
    }); 
}} 

,只是刪除

System.exit(0); 

了也沒用。

相關問題