这是两个类 - SecondActivity和AnimationView。 SecondActivity扩展了Activity,AnimationView扩展了View.I想要传递" text"的值。从一个SecondActivity到AnimationView ..
我想使用字符串" text"在AnimationView类中。所以我需要将它的值从SecondActivity类传递给AnimationView类。我试图通过intent来实现它。但我认为它不可能通过intent,因为AnimationView没有扩展Activity
SecondActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class SecondActivity extends Activity {
AnimationView animationview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
String text = i.getStringExtra("TextBox");
Intent in = new Intent(getApplicationContext(), AnimationView.class);
in.putExtra("abc", text);
setContentView(R.layout.activity_two);
animationview=(AnimationView)findViewById(R.id.animationView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AnimationView.java
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.app.Activity;
import android.view.View;
public class AnimationView extends View{
private final int FRAME_RATE=15;
private Paint paint;
private Handler h;
Ball myball;
Ball greenball;
Ball redball;
public AnimationView(Context context,AttributeSet attrs) {
super(context,attrs);
// TODO Auto-generated constructor stub
h=new Handler();
paint=new Paint();
paint.setColor(Color.BLUE);
myball=new Ball(100,100,Color.BLUE,50);
greenball=new Ball(200,200,Color.GREEN,50);
redball=new Ball(50,400,Color.RED,50);
myball.setDX(10);
myball.setDY(10);
greenball.setDX(10);
greenball.setDY(10);
redball.setDX(10);
redball.setDY(10);
}
protected void onDraw(Canvas c)
{
myball.bounce(c);
greenball.bounce(c);
redball.bounce(c);
myball.bounceoff(myball);
greenball.bounceoff(greenball);
redball.bounceoff(redball);
c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
h.postDelayed(r, FRAME_RATE);
}
private Runnable r=new Runnable()
{ public void run()
{ invalidate();
}
};
}
答案 0 :(得分:1)
你有什么
Intent in = new Intent(getApplicationContext(), AnimationView.class);
in.putExtra("abc", text);
不起作用。它不是一个Activity类
你可以有一个setter方法
String value;
public void setString(String value)
{
this.value = value;
}
然后
animationview.setString(text);