android 页面内处理多个点击事件方案

在同一个页面有多个点击事件时,我们通常都是直接这样case分别处理

 但是像我这个case特别多

附上对应xml文件

<LinearLayout
    android:id="@+id/sjjk_activity_bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black"
    android:orientation="horizontal">

    <Button
        android:id="@+id/history_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/purple_700"
        android:text="查看历史数据"
        android:textColor="@color/white"></Button>

    <Button
        android:id="@+id/value_pattern"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/Copper"
        android:text="数值模式"
        android:textColor="@color/white"></Button>

    <Button
        android:id="@+id/bight_pattern"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/teal_200"
        android:text="曲线模式"
        android:textColor="@color/white"></Button>
</LinearLayout>

而且前面有一个buttoncolor方法,这是对某几种点击事件做处理的  而有的点击事件不需要这个方法,甚至有了影响。这个当然可以把buttoncolor方法放在需要的case下面。

又或者是一些大厂要求圈复杂度不超过15  复杂页面有很多点击事件,统一这样处理就不太好。圈复杂度问题可以让方法拆分。

有什么什么方式让不同点击事件分别对应处理呢,那就是可以用控件onclick属性。

<Button
    android:id="@+id/bight_pattern"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/teal_200"
    android:text="曲线模式"
    android:onClick="buttonClick"
    android:textColor="@color/white"></Button>

这个buttonClick对应的是方法名, 在代码中添加对应的函数buttonClick,注意要添加到当前Activity对应的java类中

public void buttonClick(View view) {
    //处理逻辑
  }
 

这样通用的点击事件我们就用开头形式case分别处理,需要单独处理的就用控件onclick属性

猜你喜欢

转载自blog.csdn.net/weixin_54723630/article/details/126199388