2013-03-15 2 views
0

OnClick 이벤트에서 UI를 업데이트하는 데 약간의 문제가 있습니다.런타임 중에 뷰에서 이미지를 업데이트하거나 변경하십시오.

내가 할 때 :

case R.id.radStripMiningCoal : 
    {  
     LinearLayout testLayout = (LinearLayout)findViewById(R.id.layouttest); 
     LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout2); 
     LinearLayout layout4 = (LinearLayout)findViewById(R.id.layout3); 
     LinearLayout layout = (LinearLayout)findViewById(R.id.recomended); 
     LinearLayout layout2 = (LinearLayout)findViewById(R.id.basic); 


     int origWidth = getWindowManager().getDefaultDisplay().getWidth(); 


     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(origWidth,LayoutParams.WRAP_CONTENT); 
     layout2.setLayoutParams(params); 
     layout.setLayoutParams(params); 
     testLayout.setLayoutParams(params); 
     layout3.setLayoutParams(params); 
     layout4.setLayoutParams(params); 

     stripminingcoal.setVisibility(View.VISIBLE); 
     VSLists.setVisibility(View.GONE); 
     Next.setVisibility(View.GONE); 

      AnimationDrawable animation = new AnimationDrawable(); 

      animation.addFrame(getResources().getDrawable(R.drawable.multispd1), 5000); 
      animation.addFrame(getResources().getDrawable(R.drawable.img1), 5000); 
      animation.addFrame(getResources().getDrawable(R.drawable.shocktubecluster1), 5000); 
      animation.setOneShot(false); 

      ImageView imageAnim = (ImageView) findViewById(R.id.imgBasicIS);   
      imageAnim.setBackgroundDrawable(animation);    
      animation.start(); 

      AnimationDrawable animation2 = new AnimationDrawable(); 
      animation2.addFrame(getResources().getDrawable(R.drawable.unidelaysp), 5000); 
      animation2.addFrame(getResources().getDrawable(R.drawable.trunkline), 5000);     
      animation2.setOneShot(false); 

      ImageView imageAnim2 = (ImageView) findViewById(R.id.imgRecomendedIS);   
      imageAnim2.setBackgroundDrawable(animation2);    
      animation2.start(); 

      AnimationDrawable animation3 = new AnimationDrawable(); 

      animation3.addFrame(getResources().getDrawable(R.drawable.leadin2), 5000); 
      animation3.addFrame(getResources().getDrawable(R.drawable.tubestarter),5000); 
      animation3.setOneShot(false); 
      ImageView imageAnim3 = (ImageView) findViewById(R.id.imgRecomendedBSS);    
      imageAnim3.setBackgroundDrawable(animation3);   
      animation3.start(); 

      txtBasicIS.setText("Shock Tube Multi SPD/Detonating Cord/Cluster"); 
      txtRecomendedIS.setText("Shock Tube Uni-Delay SP/Trunkline"); 
      txtRecomendedBSS.setText("Lead-In Line/Electric Shock Tube Starter"); 

모든 업데이트와 잘 표시됩니다. 그러나의 내가 "사건"의 다른에서 동일한 코드를하는 것처럼 곧 새로운 이미지로 이미지를 업데이트하지 않습니다 : I가 없거나 잘못을하고있는 중이 야 무엇

case R.id.radNarrowReefGold: 
    { 
     LinearLayout testLayout = (LinearLayout)findViewById(R.id.layouttest); 
     LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout2); 
     LinearLayout layout4 = (LinearLayout)findViewById(R.id.layout3); 
     LinearLayout layout = (LinearLayout)findViewById(R.id.recomended); 
     LinearLayout layout2 = (LinearLayout)findViewById(R.id.basic); 


     int origWidth = getWindowManager().getDefaultDisplay().getWidth(); 


     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(origWidth,LayoutParams.WRAP_CONTENT); 
     layout2.setLayoutParams(params); 
     layout.setLayoutParams(params); 
     testLayout.setLayoutParams(params); 
     layout3.setLayoutParams(params); 
     layout4.setLayoutParams(params); 

     stripminingcoal.setVisibility(View.VISIBLE); 
     VSLists.setVisibility(View.GONE); 
     Next.setVisibility(View.GONE); 

      AnimationDrawable animation = new AnimationDrawable(); 

      animation.addFrame(getResources().getDrawable(R.drawable.udlp3), 5000); 
      animation.addFrame(getResources().getDrawable(R.drawable.udlp6), 5000); 
      animation.addFrame(getResources().getDrawable(R.drawable.trunkline), 5000); 
      animation.setOneShot(false); 

      ImageView imageAnim = (ImageView) findViewById(R.id.imgBasicIS);   
      imageAnim.setBackgroundDrawable(animation);    
      animation.start(); 

      AnimationDrawable animation2 = new AnimationDrawable(); 
      animation2.addFrame(getResources().getDrawable(R.drawable.udlp3vivid), 5000); 
      animation2.addFrame(getResources().getDrawable(R.drawable.udlp6vivid), 5000); 
      animation2.addFrame(getResources().getDrawable(R.drawable.trunkline), 5000);     
      animation2.setOneShot(false); 

      ImageView imageAnim2 = (ImageView) findViewById(R.id.imgRecomendedIS);   
      imageAnim2.setBackgroundDrawable(animation2);    
      animation2.start(); 

      AnimationDrawable animation3 = new AnimationDrawable(); 

      animation3.addFrame(getResources().getDrawable(R.drawable.leadin2), 5000); 
      animation3.addFrame(getResources().getDrawable(R.drawable.tubestarter),5000); 
      animation3.setOneShot(false); 
      ImageView imageAnim3 = (ImageView) findViewById(R.id.imgRecomendedBSS);    
      imageAnim3.setBackgroundDrawable(animation3);   
      animation3.start(); 

      txtBasicIS.setText("Uni-Delay LP (3)/Uni-Delay LP (6)/Trunkline"); 
      txtRecomendedIS.setText("Shock Tube Uni-Delay SP/Trunkline"); 
      txtRecomendedBSS.setText("Lead-In Line/Electric Shock Tube Starter"); 
    } 

를?

textViews도 변경되지 않습니다.

미리 감사드립니다. 패트릭

+1

'case' 블록의 끝에서'break;'명령문을 놓치지 않았습니까? –

+0

감사합니다. 그게 ... 어리석은 루키 실수! –

+0

가능한 경우 [case 문 뒤에 중단해야하는 이유는 무엇입니까?] (http://stackoverflow.com/questions/2710300/why-do-we-need-break-after-case-statements) – Raedwald

답변

1

누락 된 "휴식;" 귀하의 스위치 케이스 구조에있는 진술.

내가 직접 작성한 다음 작은 스 니펫을 사용하여이를 테스트 할 수 있습니다. 휴식 시간을 제거한다면; 성명서를 통해 MESSI에 글을 올릴 수는 없지만 RONALDO 만 보여줍니다. 즉, 논리가 깨졌습니다.

"휴식;" MESSI와 RONALDO 사이를 전환 할 수 있습니다.

MainActivity.java

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

public class MainActivity extends Activity implements OnClickListener{ 

TextView myText; 
private Button b1; 
private Button b2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     myText = (TextView)this.findViewById(R.id.textView1); 

     b1 = (Button)this.findViewById(R.id.buttonMessi); 
     b1.setOnClickListener(this); 

     b2 = (Button)this.findViewById(R.id.buttonRonaldo); 
     b2.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     switch(v.getId()) { 
     case R.id.buttonMessi: 
      myText.setText("MESSI"); 
      break; 
     case R.id.buttonRonaldo: 
      myText.setText("RONALDO"); 
      break; 
     } 

    } 

} 

activity_main.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" 
tools:context=".MainActivity" > 

<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" /> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:orientation="horizontal"> 
<Button 
    android:id="@+id/buttonMessi" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/textView1" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_marginBottom="40dp" 
    android:text="Messi" /> 
<Button 
    android:id="@+id/buttonRonaldo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/textView1" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_marginBottom="40dp" 
    android:text="Ronaldo" /> 
</LinearLayout> 

</RelativeLayout> 
+0

* faceDesk * 이러한 어리석은 루키 실수! 포인터에 대 한 밀을 주셔서 감사합니다! –

+1

그것은 항상 일어납니다. 그래서 재미있는 코딩을하게됩니다 :) –

+0

그냥 질문입니다. 애니메이션 설정없이 Imageview Source를 변경하면 어떻게 갈 수 있습니까? 예 : img.SetImageResource (R.drawable.img); –

0

시도가 img.SetImageResource을 추가하기 (0); 이전 줄 앞에. 그것은 당신에게 명성을 쌓았습니다

관련 문제