2016-09-24 56 views
1

我在android eclipse中製作倒數計時器,我想用另一個類的Textview。如何用另一個課程的textview開始倒計時?

這是我的第一類

public class Main extends Activity { 
Spinner timerValueSpinner; 
Button startButton; 
TextView statusTextView; 
Countdown timer; 
String[] timeValues; 
Resources resourcePointer; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    timerValueSpinner = (Spinner)this.findViewById(R.id.secondsSpinner); 

    statusTextView = (TextView)this.findViewById(R.id.timerView); 

    resourcePointer = getResources(); 
    timeValues = resourcePointer.getStringArray(R.array.seconds_list); 



    startButton = (Button)this.findViewById(R.id.startButton); 
    startButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(Main.this,Timer.class); 

      startActivity(intent); 

      if(timerValueSpinner.getSelectedItemPosition() > -1){ 
       int parsedSpinnerValue = 0; 
       parsedSpinnerValue = Integer.parseInt(timeValues[timerValueSpinner.getSelectedItemPosition()]); 

       if(parsedSpinnerValue > 0){ 
        if(timer != null){ 
         timer.cancel(); 
        } 
        timer = new Countdown(parsedSpinnerValue 
          * Countdown.oneSecond, Countdown.oneSecond,statusTextView); 

        timer.start(); 

       } 

      } 

     } 


    }); 
} 

這是我的第二課堂,我想它的statusTextVie和第1(主)使用類,而不是它自己的TextView所以當我點擊按鈕第二課將顯示,倒計時將從其textview開始。

public class Timer extends Activity { 


TextView statusTextVie; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_timer); 

    statusTextVie = (TextView)this.findViewById(R.id.timerVie); 
} 

這是我第一次在這裏問一個問題,所以我很抱歉,如果我犯了一些錯誤。

回答

0

爲什麼不通過意圖將時間值從第一個活動傳遞到第二個活動並在第二個活動中啓動線程?

第一項活動:

public class Main extends Activity { 
    Spinner timerValueSpinner; 
    Button startButton; 


    String[] timeValues; 
    Resources resourcePointer; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     timerValueSpinner = (Spinner)this.findViewById(R.id.secondsSpinner); 
     resourcePointer = getResources(); 
     timeValues = resourcePointer.getStringArray(R.array.seconds_list); 



     startButton = (Button)this.findViewById(R.id.startButton); 
     startButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(Main.this,Timer.class); 



       if(timerValueSpinner.getSelectedItemPosition() > -1){ 
        int parsedSpinnerValue = 0; 
        parsedSpinnerValue = Integer.parseInt(timeValues[timerValueSpinner.getSelectedItemPosition()]); 

        if(parsedSpinnerValue > 0){ 
         intent.putExtra("TimeValue", parsedSpinnerValue); 
         startActivity(intent); 
        } 

       } 

      } 


     }); 
    } 

次活動:

public class Timer extends Activity { 

    Countdown timer; 
    TextView statusTextVie; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_timer); 
     Bundle extras = getIntent().getExtras(); 
     int timeVal = extras.getInt("TimeValue", 0); 
     statusTextVie = (TextView)this.findViewById(R.id.timerVie); 
     if (timeVal > 0){ 
      if(timer != null){ 
       timer.cancel(); 
      } 
      timer = new Countdown(timeVal * Countdown.oneSecond, Countdown.oneSecond,statusTextVie); 

      timer.start(); 
     } 
    } 
} 
+1

哇,謝謝!!我剛開始學習java編程和android eclipse,所以我不知道那麼多(我只是看教程)。我從昨天開始試圖找到一個解決方案,然後你解決它。再次謝謝你。 –

+0

很高興它工作:) –

相關問題