【问题标题】:Android ImageButton OnClick calls another function in the same LinearLayoutAndroid ImageButton OnClick 在同一个 LinearLayout 中调用另一个函数
【发布时间】: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


【解决方案1】:

我尝试重现您的问题。我认为这与您设置的比例有关。我用不同的比例和不同的背景尝试了你的代码,它有效。尝试使用不同的图像比例。

【讨论】:

  • 是的!体重秤是个问题!但是,我真的不知道为什么会这样。你对这种行为有什么解释吗?
  • @FedericoBacci 因为图像重叠,并且android在彼此之上绘制视图。因此,当您认为您正在单击第一个按钮时,您实际上是在单击第二个按钮,因为它位于第一个按钮的顶部
【解决方案2】:

清理并重建您的项目。

在处理资源时很可能是 ID 的 android 构建/缓存问题。

【讨论】:

  • 有趣。代码在活动中运行?布局被 setContent() 膨胀了?
  • @skbrhmn 发现问题
猜你喜欢
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 2012-11-23
  • 2021-09-09
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多