我无法通过单击按钮更改背景的颜色

时间:2013-11-26 16:19:14

标签: android

我正在尝试通过单击按钮来更改背景的颜色。例如:单击“Y”应使其为黄色,“G”为绿色,依此类推,但单击按钮不会更改任何内容。我只实现了两个按钮,但它们没有用。谁能告诉我哪里出错?

这是我的活动:

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

    // Declare UI elements
    private Button firstButton;
    private Button secondButton;
    private ImageView changeBackground;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // Our only layout for this app is main.xml

        // Initialize the UI components
        changeBackground = (ImageView) findViewById(R.id.backGround);
        firstButton = (Button) findViewById(R.id.button1);
        // When we creating a button and if we expect that to use for event handling we have to set the listener
        firstButton.setOnClickListener(this);
        secondButton = (Button) findViewById(R.id.button2);
        secondButton.setOnClickListener(this);
    }

    // Have to implement with the OnClickListner
    // onClick is called when a view has been clicked.
    @Override
    public void onClick(View v) { // Parameter v stands for the view that was clicked.  

        if(v.getId() == R.id.button1){
            // setText() sets the string value of the TextView
            changeBackground.setBackgroundColor(Color.RED);

        }else if(v.getId() == R.id.button2){
            changeBackground.setBackgroundColor(Color.BLACK);
        }


    }
}

和我的XML:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#CCEEFF"
    tools:context=".MainActivity" >

     <ImageView

        android:id="@+id/backGround"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

       />

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

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/backGround"
        android:layout_alignParentBottom="true"

        android:text="@string/white" />


    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="@string/black" />


    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:text="@string/red" />


    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:text="@string/yellow" />


    <Button
        android:id="@+id/button5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button4"
        android:layout_alignBottom="@+id/button4"
        android:layout_toRightOf="@+id/button4"
        android:text="@string/green"/>


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我可能不对,但如果您没有放入一些内容,您的ImageView根本不会出现:

<ImageView

    android:id="@+id/backGround"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

   />

宽度和高度都设置为包装其内容,而不包含。

尝试使用:

<ImageView

    android:id="@+id/backGround"
    android:layout_width="200px"
    android:layout_height="200px"

   />

编辑:

这是你如何做到的。使用开关而不是ifs和elses,这将变得太乱。

package com.example.imageview;
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;

    public class MainActivity extends Activity implements OnClickListener {

        // Declare UI elements
        private Button firstButton;
        private Button secondButton, thirdButton, fourthButton, fifthButton;
        private ImageView changeBackground;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); // Our only layout for this app is main.xml

            // Initialize the UI components
            changeBackground = (ImageView) findViewById(R.id.backGround);
            firstButton = (Button) findViewById(R.id.button1);
            // When we creating a button and if we expect that to use for event handling we have to set the listener
            firstButton.setOnClickListener(this);
            secondButton = (Button) findViewById(R.id.button2);
            secondButton.setOnClickListener(this);
            thirdButton = (Button) findViewById(R.id.button3);
            thirdButton.setOnClickListener(this);
            fourthButton = (Button) findViewById(R.id.button4);
            fourthButton.setOnClickListener(this);
            fifthButton = (Button) findViewById(R.id.button5);
            fifthButton.setOnClickListener(this);
        }

        // Have to implement with the OnClickListner
        // onClick is called when a view has been clicked.
        @Override
        public void onClick(View v) { // Parameter v stands for the view that was clicked.  

            switch(v.getId()) {

            case R.id.button1:
                changeBackground.setBackgroundColor(Color.WHITE);
                break;
            case R.id.button2:
                changeBackground.setBackgroundColor(Color.BLACK);
                break;
            case R.id.button3:
                changeBackground.setBackgroundColor(Color.RED);
                break;
            case R.id.button4:
                changeBackground.setBackgroundColor(Color.YELLOW);
                break;
            case R.id.button5:
                changeBackground.setBackgroundColor(Color.GREEN);
                break;
            }

        }
    }

不要忘记修复你的xml布局,否则你什么也看不见。

1)在你的xml文件中为你的布局添加一个id。

android:id="@+id/myLayout"

2)在Activity类中声明你的布局对象:

RelativeLayout myLayout;

3)在您的Activity的onCreate()方法中连接您的布局。

myLayout = (RelativeLayout) findViewById(R.id.myLayout);

4)在onClick方法中,将changeBackGround更改为myLayout(您希望为此对象设置背景颜色,显然是所有实例。)例如:

myLayout.setBackGroundColor(Color.RED);