java的Lambda的引用

看书的时候发现有不懂的地方查了一下发现是java的Lambda

(参数) ->{ 方法; }

Lambda表达式由三部分组成:

参数:类似方法中的形参列表,这里的参数是函数式接口里的参数。这里的参数类型可以明确的声明也可不声明而由JVM隐含的推断。另外当只有一个推断类型时可以省略掉圆括号。

->:可理解为“被用于”的意思

方法体:可以是表达式也可以代码块,是函数式接口里方法的实现。代码块可返回一个值或者什么都不反回,这里的代码块块等同于方法的方法体。如果是表达式,也可以返回一个值或者什么都不反回。

针对下面的CompoundButton.OnCheckedChangeListener方法
在这里插入图片描述
就用到了Lambda ,封装成了一个匿名函数。
然后当可选按钮被调用时(setOnCheckedChangeListener)就可以改变,去执行lambda定义的函数方法。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/toggle"
        android:textOn="横向排列"
        android:textOff="纵向排列"
        android:checked="true"/>
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/switcher"

        android:thumb="@drawable/ic_launcher_background"
        android:checked="true"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/test"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="aaaa"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="aaaa"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="aaaa"/>
    </LinearLayout>

</LinearLayout >  
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ToggleButton toggle = findViewById(R.id.toggle);
        Switch switcher = findViewById(R.id.switcher);
       LinearLayout test = findViewById(R.id.test);

       CompoundButton.OnCheckedChangeListener listener=(button, isChecked) -> {
         if (isChecked){
             test.setOrientation(LinearLayout.VERTICAL);
             toggle.setChecked(true);
            switcher.setChecked(true);
         }else {
             test.setOrientation(LinearLayout.HORIZONTAL);
             toggle.setChecked(false);
             switcher.setChecked(false);
         }
        };
        toggle.setOnCheckedChangeListener(listener);
        switcher.setOnCheckedChangeListener(listener);


    }```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值