【问题标题】:Android: onClick event listener doesn't work on linearlayoutAndroid:onClick 事件侦听器在线性布局上不起作用
【发布时间】:2015-10-21 04:25:39
【问题描述】:

我正在尝试为整个布局实现 onclick,但它不起作用。

这是我的 RelativeLayout 函数:

public void updateRange(View v)
{
    System.out.println("Hello!");
}

这是我的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity"
    android:background="#ffffffff"
    android:id="@+id/xasdf"
    android:onClick="updateRange">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toptab"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Settings"
            android:fontFamily="monospace"
            android:textAllCaps="false"
            android:layout_weight="0.333333"
            android:id="@+id/settingsButton"
            android:background="#fffdb64b" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Solutions"
            android:fontFamily="monospace"
            android:textAllCaps="false"
            android:layout_weight="0.333333"
            android:id="@+id/solutionsButton"
            android:background="#fffdb64b" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mode"
            android:fontFamily="monospace"
            android:textAllCaps="false"
            android:layout_weight="0.333333"
            android:id="@+id/modeButton"
            android:background="#fffdb64b" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/chartlayout"
        android:layout_below="@+id/toptab"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:weightSum="1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textView"
            android:layout_weight="0.5"
            android:background="#ffffffff"
            android:fontFamily="monospace"
            android:hint="y = ..." />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Graph"
            android:fontFamily="monospace"
            android:textAllCaps="false"
            android:layout_weight="0.25"
            android:id="@+id/button"
            android:background="#ffd0d0d0"
            android:onClick="drawGraph" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Table"
            android:fontFamily="monospace"
            android:layout_weight="0.25"
            android:textAllCaps="false"
            android:id="@+id/button2"
            android:background="#ffd0d0d0" />
    </LinearLayout>

</RelativeLayout>

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    尝试在您的Relative Layout 描述中添加下面的额外行:

    ...
    android:onClick="updateRange"
    android:clickable="true">
    

    【讨论】:

    • @Henry 是的。我刚刚在我相当复杂的相对布局之一上对其进行了测试。那你肯定有其他问题。
    猜你喜欢
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多