2012-12-11 65 views
0

我已經添加了toggleButton,但它沒有顯示,我不明白爲什麼。我已經完成了Android教程,並回顧了代碼以及其他許多來源。目標是當按鈕打開時讓程序暫停。目前,該按鈕甚至不會顯示在用戶界面上。有什麼建議麼?按鈕顯示Android的Eclipse

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" /> 

    <ToggleButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:onClick="pauseCounter" /> 

</RelativeLayout> 

package com.evorlor.counter; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends Activity { 

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

     Intent counter = new Intent(MainActivity.this, Counter.class); 

     startActivity(counter); 

    } 

    public void pauseCounter(View view) { 
     Intent pause = new Intent(this, Pause.class); 
     startActivity(pause); 
    } 

    // @Override 
    // public boolean onCreateOptionsMenu(Menu menu) { 
    // // Inflate the menu; this adds items to the action bar if it is present. 
    // getMenuInflater().inflate(R.menu.activity_main, menu); 
    // return true; 
    // } 

} 

package com.evorlor.counter; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class Pause extends Activity{ 

    public void onCreate(Bundle instPause) { 
     super.onCreate(instPause); 

     TextView tv = new TextView(this); 

     tv.setTextSize(250); 
     tv.setText("PAUSE"); 
     setContentView(tv); 

    } 
} 

這是我的Counter類。這是混亂。抱歉。這跟我到目前爲止一樣接近。幫助將非常感激!我花了很多時間在這個麻煩的按鈕上!謝謝!

package com.evorlor.counter; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.widget.TextView; 
import android.widget.ToggleButton; 

public class Counter extends Activity { 

    private int count = 0; 
    private int hiCount = 0; 
    private boolean capCount = false; 

    public void onCreate(Bundle instCounter) { 
     super.onCreate(instCounter); 

     TextView tv = new TextView(this); 

     tv.setTextSize(250); 
     if (count < 10000 && capCount == false) { 

      tv.setText(Integer.toString(count)); 
     } else { 
      capCount = true; 
      if (count >= 10000) { 
       hiCount += 10; 
       count -= 10000; 
      } 
      if (hiCount < 100) { 

       tv.setText(hiCount + "k+" + count); 
      } else { 
       tv.setText("Over\n100k"); 
      } 
     } 
     tv.setGravity(Gravity.CENTER); 

     setContentView(tv); 

     ToggleButton butPause = new ToggleButton(this); 

     if (butPause == null) { 
      Intent pause = new Intent(this, Pause.class); 
      startActivity(pause); 
     } 

    } 
    // public void pauseCounter(View view) { 
    // Intent pause = new Intent(this, Pause.class); 
    // startActivity(pause); 
    // } 

} 

回答

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

    Intent counter = new Intent(MainActivity.this, Counter.class); 

    startActivity(counter); 

} 

您正在開始一個新的活動的時候了。這不是一個好主意。
您將會看到您的Counter活動中有什麼,而不是您主要活動中的內容,原因是您沒有看到您的切換按鈕。註釋掉startActivity()只是爲了檢查。

+0

謝謝。我評論說,現在我的toggleButton在那裏...是否有可能讓我永遠有切換按鈕? – Evorlor

+0

是的。直到您更改活動,您的切換按鈕纔會在那裏。只是不要在onCreate()方法中開始一個新的活動。使用按鈕事件來做到這一點。 –

+0

我正在看按鈕事件信息,並且我看不到我做錯了什麼。我想讓按鈕出現並保持在運行Counter類的時候。我已經嘗試將該方法移動到該類的最後,但是隨後我又回到了原點。我試着將它移動到Class中的主要方法中,但沒有成功...... – Evorlor