我的申请表有问题。这是为了在屏幕上拖动球。 我的问题是(我认为)我的布局设计(我不知道如何做到这一点)。我在MainActivity中的布局如下:
//Creates 3 layouts
LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.main, null);
view.setClickable(true);
RelativeLayout layout = new RelativeLayout(this);
final DrawView custom = new DrawView(this);
//Creates the background and sets the background.
RelativeLayout background = new RelativeLayout(this);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.achtergrond);
background.setBackground(drawable);
layout.addView(background, new LinearLayout.LayoutParams(widthOfBackground, LayoutParams.WRAP_CONTENT));
//Rules of the background, forced to the right
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)background.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
background.setLayoutParams(params1);
//Adding of the layouts to the main layout.
layout.addView(custom);
layout.addView(view);
//Shows the view to the user
setContentView(layout);
我做了3个布局: - 一个主要的xml布局(视图) - 一个RelativeLayout,其中放入其他布局。 - 例如:在其中制作球的drawView。
我的问题是当我在主布局中创建一个按钮时。 按钮确实显示,但我无法使用它。如果我在MainActivity中创建了一个onClick方法,它就不会做任何事情。此外,如果我尝试在MainActivity中创建一个Toast消息,它就不会显示出来。
我非常感谢您对我的布局的任何帮助或提示!
drawView(自定义)的代码是:
package HVA.getConnected.toptopo;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
public class DrawView extends View {
protected static final int REFRESH = 0;
public ColorBall[] colorballs = new ColorBall[1]; // array that holds the balls
private int balID = 0; // variable to know what ball is being dragged
public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events
// setting the start point for the balls
Point point1 = new Point(200,200);
Point point1a = new Point(755,502);
// declare each ball with the ColorBall class
colorballs[0] = new ColorBall(context,R.drawable.vlag_frankrijk, point1, R.drawable.vlag_frankrijk_vink, point1a);
}
// the method that draws the balls
@Override
protected void onDraw(Canvas canvas) {
//draw the balls on the canvas
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}
}
// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
balID = 0;
System.out.println("debug 1");
for (ColorBall ball : colorballs) {
// check if inside the bounds of the ball (circle)
// get the center for the ball
int centerX = ball.getX() + 25;
int centerY = ball.getY() + 25;
// calculate the radius from the touch to the center of the ball
double radCircle = Math.sqrt( (double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));
// if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball
if (radCircle > 0 && radCircle < 23){
balID = ball.getID();
break;
}
// check all the bounds of the ball (square)
if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
balID = ball.getID();
break;
}
}
System.out.println("debug 2");
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > 0) {
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
}
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
if (balID > 0){
ColorBall a = colorballs[balID-1];
if(a.getX() > a.getDestX()-100 && a.getX() < a.getDestX()+100 &&
a.getY() > a.getDestY()-100 && a.getY() < a.getDestY()){
System.out.println("___Drag X & Y is good");
colorballs[balID-1].setCanMove(false);
}
}
break;
}
// redraw the canvas
invalidate();
return true;
}
}
我已经研究了一下,Toast消息在MainActivity类中有效,但按钮没有。该程序现在不能再运行了。 我补充说:
b1 = (Button) findViewById(R.id.btn1);
b1.setOnClickListener(this);
我得到的错误是我需要使用OnClickListener强制转换onclicklistener。但是,当我这样做时,它不能再运行了。它与错误ClassCastException崩溃,我不知道如何解决这个问题。
onClick方法:
@Override
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.btn1:
System.out.println(".....");
Toast.makeText(this, "hoi?", Toast.LENGTH_LONG).show();
break;
}
}}
Toast是:
Toast.makeText(this, "text", Toast.LENGTH_LONG).show();
答案 0 :(得分:0)
像这样使用
Button btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showToastMessage(R.id.btn1);
}
});}
//showToastMessage(int id) method definition here
public void showToastMessage(int id)
{
switch(id) {
case id:
System.out.println(".....");
Toast.makeText(this, "hoi?", Toast.LENGTH_LONG).show();
break;
}
}
答案 1 :(得分:0)
布局设置使用onclickListeners破坏了应用程序,我现在在我的XML文件中有了View类,它解决了我所有的问题。