【问题标题】:Android - Display a button and text simultaneouslyAndroid - 同时显示按钮和文本
【发布时间】:2012-12-11 03:42:06
【问题描述】:

我在这方面花了几个小时,但我几乎没有取得任何进展。我问了一个类似的问题,但超过2小时,仍然没有成功。所以我会问这个并尽可能具体。我已经花了大约三个小时,所以请不要把我写下来。

我想同时显示文本和一个 ToggleButton。我可以显示其中一个,但不能同时显示。请帮忙!这是我的代码:

public class Counter extends Activity {

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



    @Override
    protected void onCreate(Bundle instCounter) {

        super.onCreate(instCounter);

        setContentView(R.layout.activity_counter);

        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 btnPause = new ToggleButton(this);

//       if (btnPause == buttonOn) {
//       Intent pause = new Intent(this, Pause.class);
//       startActivity(pause);
//       }

    }

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

    // // Show the Up button in the action bar.
    // getActionBar().setDisplayHomeAsUpEnabled(true);
}

<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=".Counter" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="ToggleButton" />

</RelativeLayout>

【问题讨论】:

    标签: android eclipse button text


    【解决方案1】:

    您将 Activity 设置为使用 TextView 作为整体视图。您的按钮永远不会像这样显示(实际上,您的布局中的其他任何东西也不会显示)!在你的R.layout.activity_counter 中创建一个 TextView 和 Button,然后使用

    TextView tv = (TextView) findViewById (R.id.textView1);
    

    这将允许您拥有 both UI 元素,因为您可以从布局中找到 TextView,然后使用它,同时不理会其他所有内容。

    【讨论】:

    • 谢谢。我应该把它放在 R.layout.activity_counter 的什么位置?
    • 根据您的 xml,它看起来不错。如果 Eclipse 的图形布局查看器没有报错,并且它显示了您想要的布局,那么运行通常没问题。
    • 虽然不好。我可以显示按钮或文本字段。但不能同时两者兼而有之,这正是我所需要的。
    • 什么意思?如果你打电话给 findViewById 并取出 setContentView(tv); 你应该没问题。也许您需要弄乱布局方向,但您的问题是将内容视图设置为新 TextView 的内容。
    • 如果我去掉 setContentView(tv),那么它就不再显示我想要的文本了。
    【解决方案2】:

    只需更改onCreate 喜欢

    @Override
    protected void onCreate(Bundle instCounter) {
    
        super.onCreate(instCounter);
    
        setContentView(R.layout.activity_counter);
    
        TextView tv = (TextView)(findViewById(R.id.textView1));
    
        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 btnPause = (ToggleButton)(findViewById(R.id.toggleButton1));
    
         if (btnPause == buttonOn) { //Do not know what you want here
         Intent pause = new Intent(this, Pause.class);
         startActivity(pause);
         }
    
    }
    

    【讨论】:

    • 这两个变化都只是红色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2011-10-12
    • 2020-10-08
    • 1970-01-01
    • 2021-04-07
    • 2015-02-11
    相关资源
    最近更新 更多