how to extend two classes so that i may set a new activity in view

▼魔方 西西 提交于 2019-12-12 03:07:54

问题


Hi so im trying to make a game that when a red circle is clicked another screen pops up and says they have failed the game. Now as i understand their is multiple ways of doing this i have attempted to make a public static method in my main class that will setContentView to the other screen, but you cannot make that into a static reference. So im kind of stuck as to what to do.

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


}

and here is the class that extends view

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:


You need to create a new instance of the Activity and and then call setContentView from it's reference.



来源:https://stackoverflow.com/questions/29404973/how-to-extend-two-classes-so-that-i-may-set-a-new-activity-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!