我已经开始研发应用了.我昨天建立了菜单但是onClick方法不起作用!我创建了一个扩展View并调用她的MainMenuObject的类 - 该类用于主菜单中的任何对象(按钮,徽标等).我为他们创建了一个特殊的类,因为我在菜单启动时正在做动画.在我构建了MainMenuObject类后,我构建了另一个扩展View的类(OpeningTimesView),它将包含主菜单的所有按钮,并将作为主要活动的布局.
一切都很好,动画非常好,我想把听众放在我的按钮上,所以我在onClickListener上添加了一个实现onClickListener,并覆盖了onClick方法.然后我用setOnClickListener(this)和setClickable(true)将监听器添加到按钮,但它不起作用!我已经尝试了一切!请帮我弄清楚我做错了什么.我已经为onClick方法添加了一个toast,它不依赖于任何"if",但它不会显示.
(BTW有没有办法将屏幕宽度和高度定义为所有类都可以访问的变量?它不能是静态的,因为你从显示对象获得高度和宽度但必须有另一种方式)
这是代码:
public class OpeningTimesView extends View implements OnClickListener{
private MainMenuObjectView searchButton;
private MainMenuObjectView supportButton;
private MainMenuObjectView aboutButton;
private int screenWidth;
private int screenHeight;
public OpeningTimesView(Context context, Display dis) {
super(context);
this.screenWidth = dis.getWidth();
this.screenHeight = dis.getHeight();
searchButton = new MainMenuObjectView(context, 200, MovingMode.RIGHT, R.drawable.search, dis);
supportButton = new MainMenuObjectView(context, 400, MovingMode.LEFT, R.drawable.support, dis);
aboutButton = new MainMenuObjectView(context, 600, MovingMode.RIGHT, R.drawable.about, dis);
searchButton.setClickable(true);
supportButton.setClickable(true);
aboutButton.setClickable(true);
searchButton.setOnClickListener(this);
supportButton.setOnClickListener(this);
aboutButton.setOnClickListener(this);
}
@Override
public void onClick(View view){
Toast.makeText(getContext(), "Search button …Run Code Online (Sandbox Code Playgroud) 我有两个图像在屏幕上移动,一个是球,一个是男人.我想要发生的是当用户触摸到男人的形象时,球会掉落.
我的问题是我似乎无法添加onclick/ontouch事件并让它工作.
我没有正确实施,有人可以帮忙吗?
我在下面列出了3个课程.Greg是男人,球被命名为球:)
TestAnimationActivity.java
package com.test.firstAnimation;
import android.app.Activity;
import android.os.Bundle;
public class TestAnimationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyAnimationView(this));
}
}
Run Code Online (Sandbox Code Playgroud)
Sprite.java
package com.test.firstAnimation;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class Sprite extends View implements OnClickListener{
private static int gregCoordX = 410; // the x coordinate at the canvas for greg
private Bitmap img; // the image …Run Code Online (Sandbox Code Playgroud)