2012-12-11 64 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