Android ImageButton OnClick 调用同一 LinearLayout 中的另一个函数
Android ImageButton OnClick calls another function in the same LinearLayout
我在一个 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>
下面是我的activity中的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");
}
我已经尝试在 activity 的代码中直接在新的 View.OnClickerListener()
中使用 setOnClickListener()
和覆盖 onClick()
方法,但它也不起作用(完全相同的问题)。
有人知道可能是什么问题吗?
清理并重建您的项目。
很可能是处理资源时 ID 的 android build/cache 问题。
我试过重现你的问题。我认为这与您设置的比例有关。我用不同的比例和不同的背景尝试了你的代码,它有效。尝试使用不同比例的图像。
我在一个 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>
下面是我的activity中的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");
}
我已经尝试在 activity 的代码中直接在新的 View.OnClickerListener()
中使用 setOnClickListener()
和覆盖 onClick()
方法,但它也不起作用(完全相同的问题)。
有人知道可能是什么问题吗?
清理并重建您的项目。
很可能是处理资源时 ID 的 android build/cache 问题。
我试过重现你的问题。我认为这与您设置的比例有关。我用不同的比例和不同的背景尝试了你的代码,它有效。尝试使用不同比例的图像。