Created
February 22, 2014 16:50
-
-
Save mdaisuke/9157913 to your computer and use it in GitHub Desktop.
rxjava usage in android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mdaisuke.sample; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import butterknife.ButterKnife; | |
import butterknife.InjectView; | |
import rx.Observable; | |
import rx.functions.Action1; | |
import rx.functions.Func2; | |
import rx.subjects.BehaviorSubject; | |
import rx.subjects.PublishSubject; | |
public class MainActivity extends Activity { | |
@InjectView(R.id.email) | |
EditText emailEdit; | |
@InjectView(R.id.password) | |
EditText passwordEdit; | |
@InjectView(R.id.push_me_button) | |
Button pushMe; | |
@InjectView(R.id.push_me_text) | |
TextView pushMeText; | |
private int counter = 0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ButterKnife.inject(this); | |
//edit text filling sample | |
final BehaviorSubject<String> email = BehaviorSubject.create(emailEdit.getText().toString()); | |
emailEdit.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {} | |
@Override | |
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {} | |
@Override | |
public void afterTextChanged(Editable editable) { | |
email.onNext(editable.toString()); | |
} | |
}); | |
final BehaviorSubject<String> password = BehaviorSubject.create(passwordEdit.getText().toString()); | |
passwordEdit.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {} | |
@Override | |
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {} | |
@Override | |
public void afterTextChanged(Editable editable) { | |
password.onNext(editable.toString()); | |
} | |
}); | |
Observable.combineLatest(email, password, new Func2<String, String, Boolean>() { | |
@Override | |
public Boolean call(String s, String s2) { | |
return (s != null && !s.equals("") && s2 != null && !s2.equals("")); | |
} | |
}).subscribe(new Action1<Boolean>() { | |
@Override | |
public void call(Boolean aBoolean) { | |
pushMe.setEnabled(aBoolean); | |
} | |
}); | |
//click event sample | |
final PublishSubject<Object> push = PublishSubject.create(); | |
pushMe.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
push.onNext(new Object()); | |
} | |
}); | |
push.subscribe(new Action1<Object>() { | |
@Override | |
public void call(Object o) { | |
counter += 1; | |
pushMeText.setText(Integer.toString(counter)+" times pushed"); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment