Android Open Source - Android-RxJava Double Binding Text View Fragment






From Project

Back to project page Android-RxJava.

License

The source code is released under:

Apache License

If you think the Android project Android-RxJava listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.morihacky.android.rxjava;
/*  w  w  w .j  a v  a  2 s.  co  m*/
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnTextChanged;
import com.google.common.base.Strings;
import com.morihacky.android.rxjava.app.R;
import rx.Subscription;
import rx.functions.Action1;
import rx.subjects.PublishSubject;

public class DoubleBindingTextViewFragment
    extends Fragment {

  @InjectView(R.id.double_binding_num1) EditText _number1;
  @InjectView(R.id.double_binding_num2) EditText _number2;
  @InjectView(R.id.double_binding_result) TextView _result;

  Subscription _subscription;
  PublishSubject<Float> _resultEmitterSubject;

  @Override
  public View onCreateView(LayoutInflater inflater,
                           @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.fragment_double_binding_textview, container, false);
    ButterKnife.inject(this, layout);

    _resultEmitterSubject = PublishSubject.create();
    _subscription = _resultEmitterSubject.asObservable().subscribe(new Action1<Float>() {
      @Override
      public void call(Float aFloat) {
        _result.setText(String.valueOf(aFloat));
      }
    });

    onNumberChanged();
    _number2.requestFocus();

    return layout;
  }

  @OnTextChanged({ R.id.double_binding_num1, R.id.double_binding_num2 })
  public void onNumberChanged() {
    float num1 = 0;
    float num2 = 0;

    if (!Strings.isNullOrEmpty(_number1.getText().toString())) {
      num1 = Float.parseFloat(_number1.getText().toString());
    }

    if (!Strings.isNullOrEmpty(_number2.getText().toString())) {
      num2 = Float.parseFloat(_number2.getText().toString());
    }

    _resultEmitterSubject.onNext(num1 + num2);
  }

  @Override
  public void onDestroyView() {
    super.onDestroyView();
    if (_subscription != null) {
      _subscription.unsubscribe();
    }
  }
}




Java Source Code List

com.morihacky.android.rxjava.BufferDemoFragment.java
com.morihacky.android.rxjava.ConcurrencyWithSchedulersDemoFragment.java
com.morihacky.android.rxjava.DemoTimeoutFragment.java
com.morihacky.android.rxjava.DoubleBindingTextViewFragment.java
com.morihacky.android.rxjava.MainActivity.java
com.morihacky.android.rxjava.MainFragment.java
com.morihacky.android.rxjava.PollingFragment.java
com.morihacky.android.rxjava.RetrofitFragment.java
com.morihacky.android.rxjava.SubjectDebounceSearchEmitterFragment.java
com.morihacky.android.rxjava.app.ApplicationTest.java
com.morihacky.android.rxjava.retrofit.Contributor.java
com.morihacky.android.rxjava.retrofit.GithubApi.java
com.morihacky.android.rxjava.retrofit.User.java
com.morihacky.android.rxjava.rxbus.RxBusDemoFragment.java
com.morihacky.android.rxjava.rxbus.RxBusDemo_Bottom1Fragment.java
com.morihacky.android.rxjava.rxbus.RxBusDemo_Bottom2Fragment.java
com.morihacky.android.rxjava.rxbus.RxBusDemo_Bottom3Fragment.java
com.morihacky.android.rxjava.rxbus.RxBusDemo_TopFragment.java
com.morihacky.android.rxjava.rxbus.RxBus.java