我正在为Android创建一个游戏应用程序,当点击一个绿色圆圈或(按钮)然后你获得奖励,如果是红色圆圈或(按钮),那么你将失去游戏。我的问题是为圆圈创建这些按钮。我已经在这里的其他人的帮助下为圈子制作了发生器,我试图创建按钮。这是我到目前为止的所有代码:
public class Main extends Activity
{
DrawingView v;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
v = new DrawingView(this);
setContentView(v);
Button redCircle = (Button) findViewById(Color.RED);
redCircle.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent s = new Intent(Main.this, ifRed());
startActivity(s);
}
private Class<?> ifRed()
{
// TODO Auto-generated method stub
String str = "You clicked on a red circle. You Loose!";
System.out.print(str);
return null;
}
});
}
}
和下面的圆圈生成器:
public class DrawingView extends View
{
public DrawingView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
private int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
private final Runnable updateCircle = new Runnable()
{
@Override
public void run()
{
lastColor = random.nextInt(2) == 1 ? Color.RED : Color.GREEN;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 380);
}
};
@Override
protected void onAttachedToWindow()
{
super.onAttachedToWindow();
handler.post(updateCircle);
}
@Override
protected void onDetachedFromWindow()
{
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
// your other stuff here
canvas.drawCircle(random.nextInt(canvas.getWidth()-radius/2) + radius/2f, random.nextInt(canvas.getHeight()-radius/2) + radius/2f, radius, paint);
}
}