Java 在运行时更新或更改视图中的图像

Java 在运行时更新或更改视图中的图像,java,android,Java,Android,我在OnClick事件中更新UI时有点问题 当我这样做时: case R.id.radStripMiningCoal : { LinearLayout testLayout = (LinearLayout)findViewById(R.id.layouttest); LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout2); LinearLayout layout

我在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"); 
一切更新和显示良好。 但只要我在不同的“案例”中使用相同的代码,它就不会将图像更新为新图像:

    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"); 
    }
我错过了什么或做错了什么

文本视图也不会改变

提前谢谢。 Patrick

这是由于您的switch case结构中缺少“break;”语句造成的

您可以使用我动态编写的以下小片段来测试这一点。如果你移除了断点;声明,你无法将文本切换到梅西,它只显示罗纳尔多。换句话说,逻辑被打破了

如果有“突破”,你可以在梅西和罗纳尔多之间切换

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>

尝试添加img.SetImageResource(0);在前一行之前。
它为你赢得了荣誉

难道你就错过了
休息时间吗案例末尾的语句
块谢谢。就这样。。。愚蠢的新手错误!faceDesk的复制品可能是如此愚蠢的新手错误!谢谢你的指南针!它总是发生,这就是为什么编码如此有趣:)只是一个问题。如何在没有动画设置的情况下更改Imageview源。例如img.SetImageResource(R.drawable.img);?是,使用img.SetImageResource(R.drawable.img);如果这不起作用。尝试添加img.SetImageResource(0);在前一行之前。这有点像是重置了可拉伸的。