0

我在 3 个 imagebuttons(circle) 的帮助下制作了一个非常简单的 android 程序,现在我想要的是在单击该特定按钮后,它的背景图像应该被更改..我尝试了如下代码,但它不起作用: main。爪哇

   package com.esp.therisemethod.ui;

import com.esp.therisemethod.R;
import com.esp.therisemethod.uc.Header;
import com.esp.therisemethod.uc.Menu;

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

public class ProgressActivity extends Activity implements OnClickListener {
    private ImageView resetall, undo, b1, b2, b3;
    int cnt1,cnt2,cnt3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        b1 = (ImageView) findViewById(R.id.b1);
        b2 = (ImageView) findViewById(R.id.b2);
        b3 = (ImageView) findViewById(R.id.b3);
        resetall = (ImageView) findViewById(R.id.reset_all);
        undo = (ImageView) findViewById(R.id.undo);

        setContentView(R.layout.activity_progress);
        Header header =(Header)findViewById(R.id.header);
        header = (Header) findViewById(R.id.header);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);

        header.btn_bak.setVisibility(View.VISIBLE);

        header.btn_bak.setVisibility(View.VISIBLE);
        Menu menu =(Menu)findViewById(R.id.prog_footer);
        menu.setSelectedTab(1);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.b1:
              cnt1++;

                if(cnt1>1 && cnt1<=5)
            {
                b1.setBackgroundResource(R.drawable.circle_red);
            }
            else if (cnt1>=6 && cnt1<10) {
                b1.setBackgroundResource(R.drawable.circle_orange);
            }
            else{
                b1.setBackgroundResource(R.drawable.circle_green);
            }
            break;

        case R.id.b2:
             cnt2++;

                if(cnt2<=5)
            {
                b2.setImageResource(R.drawable.circle_red);
            }
            else if (cnt2>=6 && cnt2<10) {
                b2.setImageResource(R.drawable.circle_orange);
            }
            else{
                b2.setImageResource(R.drawable.circle_green);
            }

            break;

        case R.id.b3:
             cnt3++;

                if(cnt3<=5)
            {
                b2.setImageResource(R.drawable.circle_red);
            }
            else if (cnt3>=6 && cnt3<10) {
                b2.setImageResource(R.drawable.circle_orange);
            }
            else{
                b2.setImageResource(R.drawable.circle_green);
            }


            break;

        case R.id.reset_all:

            break;
        case R.id.undo:

            break;
        }

    }
}

主要的.xml

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

    <com.esp.therisemethod.uc.Header
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
         android:gravity="center"
         android:layout_below="@+id/header"
         android:layout_above="@+id/prog_footer">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
       android:orientation="vertical"
           >

            <LinearLayout
                android:id="@+id/linearp1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/header"
                android:orientation="vertical" >

                <com.esp.therisemethod.uc.EspTextView
                    android:id="@+id/client_name"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:tag="800"
                    android:text="Welcome Back SAM!"
                    android:textSize="25dp"
                    android:textStyle="bold" />

                <com.esp.therisemethod.uc.EspTextView
                    android:id="@+id/make_choice"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:gravity="center"
                    android:tag="800"
                    android:text="Please make your choice:"
                    android:textSize="20dp" />
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/ralativep1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/linearp1"
                android:layout_centerVertical="true"
                android:layout_marginTop="10dp" >

                <ImageView
                    android:id="@+id/b1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginLeft="20dp"
                    android:src="@drawable/cir_grey1" />

                <ImageView
                    android:id="@+id/b2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="20dp"
                    android:src="@drawable/cir_grey1" />
            </RelativeLayout>

            <ImageView
                android:id="@+id/b3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ralativep1"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:src="@drawable/cir_grey1" />

            <LinearLayout
                android:id="@+id/linear2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/b3"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="30dp"
                android:weightSum="2" >

                <Button
                    android:id="@+id/reset_all"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_med"
                    android:text="Reset all"
                    android:textColor="#ffffff" />

                <Button
                    android:id="@+id/undo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_med"
                    android:text="Undo"
                    android:textColor="#ffffff" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <com.esp.therisemethod.uc.Menu
        android:id="@+id/prog_footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>
4

2 回答 2

1

如果您的 onClickListener 工作正常,在您的情况下,您必须更改源而不是背景,例如:

     b2.setImageResource(R.drawable.yourImage);
于 2013-06-28T11:51:36.110 回答
1

如果您src在 xml 中设置了属性,则更新 UI 使用setImageResource()

如果您background在 xml 中设置了属性,则更新 UI 使用setImageDrawable()

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //First set content view..
    setContentView(R.layout.activity_progress);
    b1 = (ImageView) findViewById(R.id.b1);
    b2 = (ImageView) findViewById(R.id.b2);
    b3 = (ImageView) findViewById(R.id.b3);
    resetall = (ImageView) findViewById(R.id.reset_all);
    undo = (ImageView) findViewById(R.id.undo);
    Header header =(Header)findViewById(R.id.header);
于 2013-06-28T11:55:34.110 回答