【发布时间】:2017-02-09 16:32:52
【问题描述】:
我在一个 LinearLayout 中有 3 个 ImageButton,问题是,当点击时,“@id/yellow”调用第二个按钮函数,“@id/green”调用第三个按钮函数,而“@id/brown”而是调用它自己的函数(它应该怎么做)。
这是 XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageButton
android:id="@+id/yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="4"
android:scaleY="2.5"
android:layout_marginLeft="13dp"
android:layout_weight="1"
android:onClick="yellowPotted"
android:background="@drawable/ball_yellow"/>
<ImageButton
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="4"
android:scaleY="2.5"
android:layout_marginLeft="13dp"
android:layout_weight="1"
android:onClick="greenPotted"
android:background="@drawable/ball_green"/>
<ImageButton
android:id="@+id/brown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="4"
android:scaleY="2.5"
android:layout_marginLeft="13dp"
android:layout_weight="1"
android:onClick="brownPotted"
android:background="@drawable/ball_brown"/>
</LinearLayout>
这是我活动中的 3 个功能(我不知道它们是否有用):
public void yellowPotted(View view) {
Log.d("BALLS", "YELLOW");
}
public void greenPotted(View view) {
Log.d("BALLS", "GREEN");
}
public void brownPotted(View view) {
Log.d("BALLS", "BROWN");
}
我已经尝试在我的活动代码中直接使用setOnClickListener() 并在新的View.OnClickerListener() 中覆盖onClick() 方法,但它也不起作用(完全相同的问题)。
有人知道可能是什么问题吗?
【问题讨论】:
-
您的问题是什么?错误?崩溃?
-
正如我所写,前两个 ImageButton 调用了错误的函数。真不知道怎么回事。
-
你的 ImageButtons 重叠了吗?
标签: android xml onclick onclicklistener imagebutton