如何扩展两个类,这样我可以设置视图的新活动 [英] how to extend two classes so that i may set a new activity in view
问题描述
嗨所以我尝试做一个游戏,点击红圈当另一个屏幕上弹出,并说他们失败了比赛。现在我明白自己是这样做我试图让我在主类中的公共静态方法,将的setContentView到其他屏幕的多种方法,但你不能作出这样的到一个静态的参考。因此,IM样的坚持,以什么做的。
公共类主要活动扩展{
DrawingView伏;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//的LinearLayout布局1 =新的LinearLayout(本);
//的FrameLayout游戏=新的FrameLayout(本);
DrawingView V =新DrawingView(本); //会将myText的TextView =新的TextView(本); // INT W = getResources()getInteger(DrawingView.redColor);
//按钮redCircle =(按钮)findViewById(W); //redCircle.setWidth(300);
//redCircle.setText(\"Start游戏);
//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(五);
//redCircle.setOnClickListener((OnClickListener)本);
}
公共无效的onClick(视图v){
意向意图=新意图(这一点,Main.class);
startActivity(意向); //重新开始从游戏视角这一活动。添加this.finish();从堆栈中删除
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
} 公共静态无效onFailure处(){
意向W =新意图(这一点,YouFailed.class);
startActivity(重量);
}
}
和这里是一个扩展视图类
公共类DrawingView扩展视图{ 公共DrawingView(上下文的背景下){
超级(上下文);
// TODO自动生成构造函数存根 }
RectF rectf =新RectF(0,0,200,0); 私有静态最终诠释W = 100;
公共静态INT lastColor = Color.BLACK;
私人最终随机随机=新的随机();
私人最终涂料粉刷=新的油漆();
私人最终诠释半径= 230;
私人最终处理程序处理程序=新的处理程序();
公共静态INT redColor = Color.RED;
公共静态INT绿彩= Color.GREEN;
INT randomWidth = 0;
INT randomHeight = 0;
公共静态INT addPoints = 0; 私人最终可运行updateCircle =新的Runnable(){
@覆盖
公共无效的run(){
lastColor = random.nextInt(2)== 1? redColor:绿彩;
paint.setColor(lastColor);
无效();
handler.postDelayed(这一点,1000); }
}; 私有对象startActivity; @覆盖
保护无效onAttachedToWindow(){
super.onAttachedToWindow();
handler.post(updateCircle);
} @覆盖
保护无效onDetachedFromWindow(){
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
} @覆盖
保护无效的onDraw(帆布油画){
super.onDraw(画布);
//你的其他的东西在这里
如果(随机== NULL){
randomWidth =(INT)(random.nextInt(Math.abs(的getWidth() - 半径/ 2))+半径/ 2F);
randomHeight =(random.nextInt((int)的Math.abs((的getHeight() - 半径/ 2 +半径/ 2F))));
}其他{
randomWidth =(INT)(random.nextInt(Math.abs(的getWidth() - 半径/ 2))+半径/ 2F);
randomHeight =(random.nextInt((int)的Math.abs((的getHeight() - 半径/ 2 +半径/ 2F))));
} canvas.drawCircle(randomWidth,randomHeight +半径/ 2F,半径,油漆);
} 公共布尔onTouch(视图V,MotionEvent事件){
INT X =(int)的event.getX();
INT Y =(int)的event.getY();
如果(isInsideCircle(X,Y)== TRUE){
//做你的东西在这里
如果(redColor == lastColor){
Main.onFailure();
}其他{
addPoints ++;
}
}其他{ }
返回true;
}私人布尔isInsideCircle(INT X,int y)对{
如果((((X - randomWidth)*(X - randomWidth)+((y)的 - randomHeight)*(Y - randomHeight)))≤((半径)*(半径)))
返回true;
返回false;
} 公共接口FailureCallback {
公共无效onFailure处();
} 公共无效setOnFailure(对象startActivity){
this.startActivity = startActivity;
}}
您需要创建活动的新实例
键,然后调用的setContentView
从它的参考。
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;
}
}
You need to create a new instance of the Activity
and and then call setContentView
from it's reference.
这篇关于如何扩展两个类,这样我可以设置视图的新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!