2012-03-03 56 views
1

我想从存在于当前的布局,并希望以不同方式处理这两个对象动态创建一个按钮/布局对象..Android的创建对象动态

这里是我的代码,我实现了..

全局声明

Button btnStart, btnButton1, btnButton2, btnButton3; 

和OnCreate中()

btnStart = (Button) findViewById(R.id.btnStart); 
btnButton1 = (Button) findViewById(R.id.Button1); 
btnButton2 = (Button) findViewById(R.id.Button2); 

btnButton3 = btnButton1; // Problem comes here. 

我想创建与btnButton1动态相同的btnButton3。在点击 btnStart我正在动画的btnButton1,btnButton2和btnButton3

现在的问题是btnButton2运行动画罚款..但btnButton1没有动画 ,而是btnButton3的动画。

这里是我完整的代码..

public class TweenAnimationActivity extends Activity { 
    /** Called when the activity is first created. */ 

    Button btnStart, btnButton1, btnButton2, btnButton3;  
    FrameLayout mainLayout; 

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

     mainLayout = (FrameLayout) findViewById(R.id.flMain); 

     WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
     Display display = wm.getDefaultDisplay(); 
     int width = display.getWidth(); 
     int height = display.getHeight(); 

     Log.e("Dimension", "Width : " + width + " Height : " + height); 

     btnStart = (Button) findViewById(R.id.btnStart); 
     btnButton1 = (Button) findViewById(R.id.Button1); 
     btnButton2 = (Button) findViewById(R.id.Button2); 

     btnButton3 = btnButton1; 
//  btnButton3.setLayoutParams(relativeParams); 

     final Animation animation = new TranslateAnimation(0, 0, 0, 
       height - 220); 
     animation.setDuration(5000); 
     animation.setFillAfter(true); 
     animation.setAnimationListener(new AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 
       // TODO Auto-generated method stub 
       btnButton1.setText("Moving"); 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       // TODO Auto-generated method stub 
       btnButton1.setText("Moved"); 
      } 
     }); 

     final Animation animation1 = new TranslateAnimation(0, 0, 0, 
       -(height - 220)); 
     // animation1.setStartOffset(1000); 
     animation1.setDuration(5000); 
     animation1.setFillAfter(true); 
     animation1.setAnimationListener(new AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 
       // TODO Auto-generated method stub 
       btnButton2.setText("Moving"); 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       // TODO Auto-generated method stub 
       btnButton2.setText("Moved"); 
      } 
     }); 

//  final Animation animation2 = new TranslateAnimation(0, 0, -(width - 220), 
//    height - 220); 
     final Animation animation2 = new TranslateAnimation(0, 0, (width - 220), 
       height - 220); 
     animation2.setDuration(5000); 
     animation2.setFillAfter(true); 
     animation2.setAnimationListener(new AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) { 
       // TODO Auto-generated method stub 
       btnButton3.setText("MovingCopied"); 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
       // TODO Auto-generated method stub 
       btnButton3.setText("MovedCopied"); 
      } 
     }); 


     btnStart.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       btnButton1.startAnimation(animation); 
       btnButton2.startAnimation(animation1); 
       btnButton3.startAnimation(animation2); 
      } 
     }); 

    } 
} 

任何这里是我的布局..

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/flMain" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 


    <Button 
     android:id="@+id/btnStart" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Start" /> 


    <Button 
     android:id="@+id/Button1" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_gravity="top|right" 
     android:text="Button1" /> 


    <Button 
     android:id="@+id/Button2" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_gravity="bottom|right" 
     android:text="Button2" /> 

</FrameLayout> 

回答

0

在你的代码,btnButton3 = btnButton1,您尚未创建BUTTON3对象刚刚创建的引用的button1。 尝试

btnButton3 = new Button(this) 
btnButton3.setLayoutParams(btnButton1.getLayoutParams()); 
mainLayout.addView(btnButton3); 

和更好的教程,你可以看看here