0

我的申请有问题。它是为在屏幕上拖动球而设计的。我的问题是(我认为)我的布局设计(我不知道我该怎么做)。我的 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; 

}
}

我进行了更多研究,吐司消息确实在 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.makeText(this, "text", Toast.LENGTH_LONG).show();
4

2 回答 2

0

布局设置使用 onclickListeners 使应用程序崩溃,现在我的 XML 文件中有 View 类,这解决了我的所有问题。

于 2013-04-16T17:08:29.677 回答
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;
                 }

                 }
于 2013-04-16T10:34:13.967 回答