2013-06-28 3 views
0

3 개의 이미지 버튼 (원)을 사용하여 매우 간단한 안드로이드 프로그램을 만들었습니다. 이제는 그 버튼의 일부를 클릭 한 후 배경 이미지를 변경해야합니다. 아래 코드 있지만 작동하지 않는 시도 : main.java을버튼 배경이 안드로이드의 카운터에서 변경되지 않았습니다

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

    } 
} 

main.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> 
+0

xml 레이아웃 파일에 배경을 어떻게 설정 했습니까? – Opiatefuchs

+0

기다리십시오. 내 질문을 업데이트하는 중입니다. – jigar

+0

b1과 b2에 대한 xml 선언을 공유하고 onCreate에서이 단추를 초기화하는 중입니다. [참조하고 있다고 가정하고] 버튼 참조를 위해 클릭 수신기를 설정하는지 확인하십시오. – Chandan

답변

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); 
+0

내가 말한대로 코드를 편집했는데 작동하지 않습니다 ... 내 업데이트 된 질문을 확인하십시오 ... 형 – jigar

+1

버튼 참조를 받기 전에 setContentView()를 내주십시오. 내 편집 된 답변을 확인하십시오. – Chandan

+0

@ Chandan-oh ... 그 작업 ... 고맙습니다. .. :) – jigar

1

귀하의 OnClickListener를 작동하는 경우 올바른, 귀하의 경우에 당신은 예를 들어, 소스없는 배경을 변경해야합니다 :

 b2.setImageResource(R.drawable.yourImage); 
+0

도와 주셔서 감사합니다. 내 freind를 도와주세요. :) – jigar

+0

기꺼이 도와 드리겠습니다. – Opiatefuchs

관련 문제