标签:UI public from toast class array eof ide ...
Lambda表达式是Java 8的一个新特性,可以替代只有一个函数的接口实现,告别匿名内部类,代码看起来更简洁易懂。Lambda表达式同时还提升了对集合的迭代、遍历、过滤数据的操作。那么在Android Studio中如何使用lambda表达式呢?
项目使用的jdk要为jdk 8或更高版本
修改build.gradle文件,enable jack和设置sourceCompatibility,targetCompatibility为jdk 8
defaultConfig {
...
jackOptions {
enabled=true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
然后在项目中就可以愉快使用lambda表达式了,大家感受下:
public class TestActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); findViewById(R.id.button1).setOnClickListener(view -> onButton1()); findViewById(R.id.button2).setOnClickListener(view -> onButton2()); } public void onButton1() { Runnable runnable = () -> Toast.makeText(this, "abcd", Toast.LENGTH_SHORT).show(); runnable.run(); } public void onButton2() { Observable.from(Arrays.asList(1, 2, 3, 4, 5)) .filter(integer -> integer % 2 == 0) .map(integer -> integer * integer) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(integer -> Toast.makeText(this, String.valueOf(integer), Toast.LENGTH_SHORT).show()); } }
标签:UI public from toast class array eof ide ...
原文地址:http://www.cnblogs.com/rainboy2010/p/6476076.html