按钮显示Android Eclipse

按钮显示Android Eclipse,android,eclipse,togglebutton,Android,Eclipse,Togglebutton,我添加了一个toggleButton,但它没有显示,我也不知道为什么。我已经完成了Android教程,并回顾了代码以及许多其他来源。目的是使程序在按钮打开时暂停。目前,该按钮甚至不会显示在用户界面上。有什么建议吗 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:la

我添加了一个toggleButton,但它没有显示,我也不知道为什么。我已经完成了Android教程,并回顾了代码以及许多其他来源。目的是使程序在按钮打开时暂停。目前,该按钮甚至不会显示在用户界面上。有什么建议吗

<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.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);
    // }

}
package com.evorlo.counter;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Gravity;
导入android.view.MotionEvent;
导入android.widget.TextView;
导入android.widget.ToggleButton;
公共类计数器扩展活动{
私有整数计数=0;
私有int hiCount=0;
私有布尔capCount=false;
创建时的公共void(Bundle instCounter){
super.onCreate(instCounter);
TextView tv=新的TextView(此);
电视机尺寸(250);
如果(计数<10000&&capCount==false){
tv.setText(Integer.toString(count));
}否则{
capCount=true;
如果(计数>=10000){
hiCount+=10;
计数-=10000;
}
如果(hiCount<100){
tv.setText(hiCount+“k+”+计数);
}否则{
tv.setText(“超过100k”);
}
}
电视。设置重力(重力。重心);
设置内容视图(电视);
ToggleButton butPause=新的ToggleButton(此);
if(butPause==null){
意图暂停=新意图(此为pause.class);
星触觉(暂停);
}
}
//公共无效暂停计数器(视图){
//意图暂停=新意图(此为pause.class);
//星触觉(暂停);
// }
}
您正在立即开始一项新活动。这不是个好主意。

您将看到计数器活动中的内容,而不是主要活动中的内容,这是您看不到切换按钮的原因。将startActivity()注释掉以供检查。

谢谢。我把它注释掉了,现在我的切换按钮在那里了……我是否可以永久地在那里使用切换按钮?是的。您的切换按钮将一直存在,直到您更改活动。不要在onCreate()方法中启动新活动。使用按钮事件来执行此操作。我正在查看按钮事件信息,不知道我做错了什么。我想在运行Counter类时显示并保留该按钮。我已经试着把这个方法移到那个课程的末尾,但后来我又回到了起点。我试着将它移动到类中的main方法中,但也没有成功…您对Counter类使用相同的布局吗?添加您的计数器类代码。嗯!好吧,我明白你想做什么了。但是使用上面的代码,您将只看到textview,因为setContentView(tv)行;我建议使用textview和切换按钮创建一个xml布局,并在创建视图时如下所示;setcontentview(R.layout.counter_布局)就像您的主要活动一样。您仍然可以像您试图做的那样以编程方式来做,但这是一个更复杂的过程。
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);

    }
}
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);
    // }

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

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

    startActivity(counter);

}