Android:独特识别可点击动画

时间:2015-04-08 18:24:26

标签: android imageview onclicklistener

我正在创建一个小型Android游戏,其中涉及单一项目的单词Phonics / soundings。

我昨天发布了这个问题关于在an​​droid中添加随机移动的按钮:Making a randomly moving clickable button on Android(对于参考)

我现在有这个工作,但我想知道如何分离/识别每个按钮,因为代码只为6个不同的' wordBubbles'创建了一个onClickListener。在我的游戏中:

AnimatedWordsView类:

 BitmapDrawable[] wordBubble = {(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_blue), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_green),
            (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_red), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_yellow),
            (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_green), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_blue)};
for (int count = 0; count <= 5; count++) { ... } // Positioning of bubbles 
AnimatedWordsView word1 = (AnimatedWordsView) findViewById(R.id.anim_view_word1);
        word1.setOnClickListener(this);

animated_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000">
<com.example.ltss.dyslexia.app.AnimatedWordsView
    android:id="@+id/anim_view_word1"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"/>
</LinearLayout>

目前,所有气泡都是可点击的,但是我需要一种方法来分离/识别每个气泡,以便为正确答案应用点 - 我认为我可以为每个气泡应用单独的onClickListener,但是在布局中添加任何内容会导致应用程序崩溃

*编辑:我不确定如何更详细地描述上述内容 - 但是,请澄清 - 我的意思是我试图在XML中创建更多视图,但如果有更多内容,程序会崩溃比在XML中声明的1个视图,例如以下内容返回错误:

animated_layout.xml (编辑)

<com.example.ltss.dyslexia.app.AnimatedWordsView
    android:id="@+id/anim_view_word1"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"/
<com.example.ltss.dyslexia.app.AnimatedWordsView
    android:id="@+id/anim_view_word2"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"/>

错误如下

java.lang.NullPointerException
        at com.example.ltss.dyslexia.app.AnimatedWordsView.onDraw(AnimatedWordsView.java:73)

第73行

AnimatedWordsView word2 = (AnimatedWordsView) findViewById(R.id.anim_view_word2);
        word2.setOnClickListener(this);

任何帮助都会非常感激!

谢谢:)

1 个答案:

答案 0 :(得分:0)

解决问题的最佳方法 - 您有多个Button - esque View,并且您希望为其分配不同的功能 - 是检查视图的id 。密切关注onClick时提供的参数(您必须实施的方法,以便Object implement View.OnClickListener()

public void onClick(View viewThatWasClicked) { }

当这个事件发生时,Android会通过将其作为参数传递告诉听众哪个视图被点击了。所以现在你只需要一些方法来识别哪个视图。

int idOfViewThatWasClicked = viewThatWasClicked.getId();

switch (idOfViewThatWasClicked) {
    // Or whatever you're calling it in the layout XML.
    case R.id.view_representing_right_answer:
        awardAllThePoints();
        break;

    case R.id.view_representing_wrong_answer:
        makeFunOfUser();
        break;
}

这样,您不需要为每个可点击的Listener创建一个新的View对象 - 给它们完全相同的一个,并找出在{{1}中调用它的按钮}。

另外,要明确 - 您不要将监听器添加到“布局”;所以我不确定你做了什么导致了崩溃。

使用此方法您将遇到的挑战是,它要求您为每个按钮指定唯一的onClick()。如果您没有在XML中定义视图(您确实应该这样做!),而是在Java代码中运行时,您将无法执行此操作;但您可以使用id来存储视图是否代表正确的答案,然后使用AnimatedWordsView.setTag()onClick()中进行检查。