如何扩展两个类,以便我可以在视图中设置一个新的活动

时间:2015-04-02 04:25:47

标签: java android android-layout android-view

嗨,所以我试图制作一个游戏,当点击一个红色圆圈时,另一个屏幕弹出并说他们已经失败了游戏。现在我理解他们有多种方法可以做到这一点,我试图在我的主类中创建一个公共静态方法,将setContentView设置到另一个屏幕,但是你不能把它变成静态引用。所以我很难坚持做什么。

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);


        //LinearLayout layout1 = new LinearLayout (this);
        //FrameLayout game = new FrameLayout(this);
        DrawingView v = new DrawingView (this);

        //TextView myText = new TextView(this);

        //int w = getResources().getInteger(DrawingView.redColor);
        //Button redCircle = (Button) findViewById(w);



         //redCircle.setWidth(300);
         //redCircle.setText("Start Game");


        //layout1.addView(myText);
       // layout1.addView(redCircle); 
        //redCircle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        //game.addView(myText);
        //game.addView(v);
        //game.addView(layout1);
        setContentView(v);
        //redCircle.setOnClickListener((OnClickListener) this);
    }
    public void onClick(View v) {
        Intent intent = new Intent(this, Main.class);
            startActivity(intent);

        // re-starts this activity from game-view. add this.finish(); to remove from stack
   }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public static void onFailure(){
        Intent w = new Intent(this, YouFailed.class);
        startActivity(w);
    }


}

这是扩展视图的类

public class DrawingView extends View{



    public DrawingView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }
    RectF rectf = new RectF(0, 0, 200, 0);

    private static final int w = 100;
    public static 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();
    public static int redColor = Color.RED;
    public static int greenColor = Color.GREEN;
    int randomWidth = 0;
    int randomHeight = 0;
    public static int addPoints = 0;



    private final Runnable updateCircle = new Runnable() {
        @Override 
        public void run() {
            lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
            paint.setColor(lastColor);
            invalidate();
            handler.postDelayed(this, 1000);

        }
    };

    private Object startActivity;



    @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
        if(random == null){
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }else {
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }

        canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
    }

    public boolean onTouch(View v, MotionEvent event) {
   int x = (int) event.getX();
   int y = (int) event.getY();
   if(isInsideCircle(x, y) ==  true){
      //Do your things here
       if(redColor == lastColor){
           Main.onFailure();
       } else {
           addPoints++;
       }
   }else {

   }
   return true;
}

private boolean isInsideCircle(int x, int y){
  if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius)))
    return true;
  return false;    
}

    public interface FailureCallback {
      public void onFailure();
    }

    public void setOnFailure(Object startActivity){
    this.startActivity = startActivity;
}







}

1 个答案:

答案 0 :(得分:0)

您需要创建Activity的新实例,然后从其引用中调用setContentView