如何在 Eclipse 中使用基于 Java 的程序插入按钮
Posted
技术标签:
【中文标题】如何在 Eclipse 中使用基于 Java 的程序插入按钮【英文标题】:How to insert a button using Java Based Program in Eclipse 【发布时间】:2015-03-23 06:30:54 【问题描述】:我只是想问一下如何使用基于 java 的程序创建一个按钮。我想在我的官方游戏上有一个按钮,比如 Next 按钮,如果用户单击它,它将允许他们进入游戏的下一个级别。这就像游戏中的暂停/继续/停止/重启按钮。
这是我使用的游戏代码。我在这里看不到按钮的声明,但是当我玩这个时,它上面有一个退出按钮(?),允许用户返回到以前的表单。
public class NewGame extends Activity
static MediaPlayer mp1;
MediaPlayer jump;
MediaPlayer takecoin;
GameLoopThread gameLoopThread;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
// for no title
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GameView(this));
public class GameView extends SurfaceView
Bitmap bmp;
Bitmap background, taya, note1;
Bitmap run1;
Bitmap run6;
Bitmap jump2;
Bitmap coin;
Bitmap exit;
// MediaPlayer mp1,jump,takecoin;
private SurfaceHolder holder;
// private gameloop gameLoopThread;
private int x = 0, y = 0, z = 0, delay = 0, getx, gety, sound = 1;
int show = 0, sx, sy = 0;
int cspeed = 0, kspeed = 0, gameover = 0;
int score = 0, health = 120, reset = 0;
private int min = 1, sec = 0;
private Handler mHandler = new Handler();
private Runnable mRunnable;
private boolean isStop = false;
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public GameView(Context context)
super(context);
mHandler.postDelayed(mRunnable = new Runnable()
@Override
public void run()
// TODO Auto-generated method stub
if(sec <= 0)
if(min > 0)
sec = 59;
min--;
isStop = false;
else
//stop timer here
Log.e("TIMER", "timer stop!");
mHandler.removeCallbacks(this);
isStop = true;
else
sec--;
isStop = false;
Log.i("TIMER", "min: " + min + " sec: " + sec);
if(!isStop)
mHandler.postDelayed(this, 1000);
, 1000);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback()
@SuppressWarnings("deprecation")
@Override
public void surfaceDestroyed(SurfaceHolder holder)
// for stoping the game
gameLoopThread.setRunning(false);
gameLoopThread.getThreadGroup().interrupt();
@SuppressLint("WrongCall")
@Override
public void surfaceCreated(SurfaceHolder holder)
gameLoopThread.setRunning(true);
gameLoopThread.start();
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height)
);
// getting the screen size
Display display = getWindowManager().getDefaultDisplay();
sx = display.getWidth();
sy = display.getHeight();
;
cspeed = x / 4;
kspeed = x / 4;
background = BitmapFactory.decodeResource(getResources(),
R.drawable.imgluneta);
run1 = BitmapFactory
.decodeResource(getResources(), R.drawable.run1);
run6 = BitmapFactory
.decodeResource(getResources(), R.drawable.run6);
jump2 = BitmapFactory.decodeResource(getResources(),
R.drawable.run11);
coin = BitmapFactory
.decodeResource(getResources(), R.drawable.coin);
exit = BitmapFactory
.decodeResource(getResources(), R.drawable.exit);
taya = BitmapFactory
.decodeResource(getResources(), R.drawable.taya);
note1 = BitmapFactory.decodeResource(getResources(),
R.drawable.note1);
exit = Bitmap.createScaledBitmap(exit, 25, 25, true);
background = Bitmap
.createScaledBitmap(background, 2 * sx, sy, true);
// health dec
note1 = Bitmap.createScaledBitmap(note1, sx, sy, true);
mp1 = MediaPlayer.create(NewGame.this, R.raw.game);
jump = MediaPlayer.create(NewGame.this, R.raw.jump);
takecoin = MediaPlayer.create(NewGame.this, R.raw.cointake);
// on touch method
@Override
public boolean onTouchEvent(MotionEvent event)
if (event.getAction() == MotionEvent.ACTION_UP)
show = 1;
getx = (int) event.getX();
gety = (int) event.getY();
// exit
if (getx < 25 && gety < 25)
System.exit(0);
// sound off
if (getx > 25 && getx < 60)
if (gety < 25)
sound = 0;
mp1.stop();
// sound on
if (getx > 61 && getx < 90)
if (gety < 25)
sound = 1;
// restart game
if (getx > 91 && gety < 25)
if (health <= 0)
gameLoopThread.setPause(0);
health = 100;
score = 0;
return true;
@SuppressLint("WrongCall")
@Override
public void onDraw(Canvas canvas)
canvas.drawColor(Color.BLACK);
// background moving
z = z - 10;
if (z == -sx)
z = 0;
canvas.drawBitmap(background, z, 0, null);
else
canvas.drawBitmap(background, z, 0, null);
// running player
x += 5;
if (x == 20)
x = 5;
if (show == 0)
if (x % 2 == 0)
int height = run1.getHeight();
canvas.drawBitmap(run1, sx / 16, (15 * sy / 18) - height / 2, null);
// kinfe hit
if (kspeed == 20)
kspeed = sx;
health -= 25;
canvas.drawBitmap(note1, 0, 0, null);
else
int height = run6.getHeight();
canvas.drawBitmap(run6, sx / 16, (15 * sy / 18) - height / 2, null);
// kinfe hit
if (kspeed == 20)
kspeed = sx / 2;
health -= 25;
canvas.drawBitmap(note1, 0, 0, null);
// for jump
if (show == 1)
if (sound == 1)
jump.start();
int height=jump2.getHeight();
canvas.drawBitmap(jump2, sx / 16, (3 * sy / 4)-height, null);
// score
if (cspeed <= (sx / 8)-height/2 && cspeed >= (sx / 16)-height/2)
if (sound == 1)
takecoin.start();
cspeed = sx / 2;
score += 10;
// jump-hold
delay += 1;
if (delay == 3)
show = 0;
delay = 0;
// for coins
cspeed = cspeed - 5;
if (cspeed == -sx / 2)
cspeed = sx / 2;
canvas.drawBitmap(coin, cspeed, 3 * sy / 4, null);
else
canvas.drawBitmap(coin, cspeed, 3 * sy / 4, null);
// kinfe
kspeed = kspeed - 20;
int height = taya.getHeight();
canvas.drawBitmap(taya, kspeed, (15 * sy / 18) - height / 2, null);
if (kspeed < 0)
kspeed = sx;
health -= 25;
// score
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setTextSize(15);
paint.setTextAlign(Align.LEFT);
canvas.drawText("Score :" + score, 3 * sx / 4, 20, paint);
// exit
canvas.drawBitmap(exit, 0, 0, null);
if (sound == 1)
mp1.start();
mp1.setLooping(true);
// health
Paint myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStrokeWidth(10);
canvas.drawText("Health :" + health, 0, (sy / 8) - 5, myPaint);
canvas.drawRect(0, sy / 8, health, sy / 8 + 10, myPaint);
// game over
if (health <= 0)
gameover = 1;
mp1.stop();
canvas.drawText("GAMEOVER OVER", sx / 2, sy / 2, myPaint);
canvas.drawText("YOUR SCORE : " + score, sx / 2, sy / 4,
myPaint);
canvas.drawText("Restart", 91, 25, myPaint);
gameLoopThread.setPause(1);
// restart
if (reset == 1)
gameLoopThread.setPause(0);
health = 100;
score = 0;
// timer
Paint mypaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStrokeWidth(10);
mypaint.setTextSize(50);
canvas.drawText("Timer :" + min + sec,min, sec, mypaint);
@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;
@Override
public void onResume()
super.onResume();
NewGame.mp1.isPlaying();
@Override
public void onPause()
super.onPause();
NewGame.mp1.pause();
【问题讨论】:
你有没有尝试过任何代码,你有什么努力? @Ann Lee,你的问题太笼统了。基本上,将一个 Button 小部件添加到您的布局并为其分配一个 onClick 侦听器。 没有。我已经使用 findviewbyid 添加了按钮,但是一旦我玩了游戏,它就不会显示按钮。 你能发布你的xml布局文件吗? 很遗憾,游戏的 xml 是空白的。我用过基于java的。纯java。 【参考方案1】:您可以在 Java 代码中创建一个按钮-
Button button = new Button(this);
“this”是上下文。
【讨论】:
我已经添加了游戏的代码。请看一下。以上是关于如何在 Eclipse 中使用基于 Java 的程序插入按钮的主要内容,如果未能解决你的问题,请参考以下文章
如何在eclipse创建基于axis2的webservice服务端