2013-04-16 33 views
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是:

Toast.makeText(this, "text", Toast.LENGTH_LONG).show(); 
+0

你可以显示你的按钮点击方法和吐司使用? –

+0

Gunjan Verma,我编辑了我的文章,现在在那里。 – Marc

+0

我没有看到任何演员在那里..你能解释并显示你的意思是“我得到的错误是,我需要OnClickListener onclicklistener” – quinestor

回答

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

       } 
0

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