//rxbinding
compile "com.jakewharton.rxbinding:rxbinding:${libs.rxbinding}"
compile "com.jakewharton.rxbinding:rxbinding-support-v4:${libs.rxbinding}"
compile "com.jakewharton.rxbinding:rxbinding-appcompat-v7:${libs.rxbinding}"
compile "com.jakewharton.rxbinding:rxbinding-design:${libs.rxbinding}"
compile "com.jakewharton.rxbinding:rxbinding-recyclerview-v7:${libs.rxbinding}"
添加了rxbinding 会自动下载rxjava、rxandroid
经常我们在项目中需要对某个button点击之前做很多的判断,比如 登录按钮,判断用户名是否空,密码是否空,密码长度是否正确,记住密码是否选择了…登录之前的验证有很对,通常都是 if else多个嵌套,现在改成rx实现。
1、点击按钮btBuyProduct
2、map的 func1 2个参数 参数类型;返回值类型,这里就是etBuyAmount edittext 的内容。
3、filter 参数类型,上面传过来的 ;返回值类型Boolean
false 订阅者就不执行subscribe
true 继续往下执行
4、多个filter嵌套,可以用作对数据进行过滤和篡改
public class MainActivity extends AppCompatActivity {
@BindView(R.id.textView1)
EditText editText1;
@BindView(R.id.textView2)
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
RxTextView.textChanges(editText1).filter(new Func1<CharSequence, Boolean>() {
@Override
public Boolean call(CharSequence charSequence) {
if (charSequence.length() > 0) {
return true;
} else {
Toast.makeText(MainActivity.this, "不能为空...", Toast.LENGTH_SHORT).show();
return false;
}
}
}).subscribe(new Action1<CharSequence>() {
@Override
public void call(CharSequence charSequence) {
editText2.setText("来自editText1:" + charSequence);
}
});
}
}
subscribe订阅
Action1<String> 参数就是map传过来的string,经过多次filter过滤后的值!!!
RxView.clicks(btBuyProduct)
.map(new Func1<Void, String>() {
@Override
public String call(Void aVoid) {
return etBuyAmount.getText().toString();
}
})
.filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
if (!cbHaveRead.isChecked()) {
ToastUtil.showMiddleScreenToast(BuyProductActivity.this.getContext(), "请阅读并同意《资金托管协议》《快捷投资协议》");
}
return cbHaveRead.isChecked();
}
})
.filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
if (TextUtils.isEmpty(s) || !(Double.valueOf(s) > 0)) {
ToastUtil.showMiddleScreenToast(BuyProductActivity.this.getContext(), "请输入有效金额");
}
return !TextUtils.isEmpty(s) && Double.valueOf(s) > 0;
}
})
.filter(s -> {
if (!(Double.valueOf(s) >= Double.valueOf(productBuyAddResp.getStartAmount()))) {
ToastUtil.showMiddleScreenToast(getContext(), "不能低于起投金额");
}
return Double.valueOf(s) >= Double.valueOf(productBuyAddResp.getStartAmount());
})
.filter(s -> {
if (!(Double.valueOf(s) % Integer.valueOf(productBuyAddResp.getIncreAmount()) == 0)) {
ToastUtil.showMiddleScreenToast(getContext(), "每次增加不得少于" + productBuyAddResp.getIncreAmount());
}
return Double.valueOf(s) % Integer.valueOf(productBuyAddResp.getIncreAmount()) == 0;
})
.filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
if (!(Double.valueOf(s) <= Double.valueOf(productBuyAddResp.getChargeAmount()))) {
ToastUtil.showMiddleScreenToast(BuyProductActivity.this.getContext(), "超过剩余可投金额");
}
return Double.valueOf(s) <= Double.valueOf(productBuyAddResp.getChargeAmount());
}
})
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
buyPayPwdFragmentDialog = new BuyPayPwdFragmentDialog(s1 -> {
buyPayPwdFragmentDialog.onShowLoading();
mPresenter.getBuyProduct(Base64Utils.getPayToken(s, s1, mPrdId));
});
buyPayPwdFragmentDialog.show(BuyProductActivity.this.getSupportFragmentManager(), "payDetailFragment");
}
}, throwable -> {
LogUtils.loge(throwable.getMessage());
});
其中ButterKnife使用需要导入2个包:
1、你的studio切换到Android模式列表:
选择 build.gradle(Module:app)添加
dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
apt 'com.jakewharton:butterknife-compiler:8.4.0'这个是注解编辑依赖jar包
同时添加在第一行添加
apply plugin: 'android-apt'
详细:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
android {
compileSdkVersion 25
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.lihui.myapplication"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile "com.jakewharton.rxbinding:rxbinding:1.0.1"
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
2、在 build.gradle(Project:你的项目名)
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
详细:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
如果不加
apt 'com.jakewharton:butterknife-compiler:8.4.0'
会爆出空指针异常!!!!!!!!!