Android Fragment with TextWatcher - does not work

1

Good morning! With the help of the staff, I made a calculation scheme that when typing the value in one editText1, another editText2 is updated automatically, and vice versa .. it's just a test converting units. Initially I was trying to integrate this with SlidingTabs, but in the middle of the path I found this link:

link That despite focusing on the percent, it puts a slide menu on the side with the activities;

Well, separately the codes work perfectly, but when trying to integrate my code below into this app I've already downloaded ready from that link, the fields are not updated automatically as I type the value. For textWatcher to work, do I have to do anything other than what I had done?

view_1.java

public class view_1 extends Activity {

    double CVtokWfactor_2 = 0.7354988;
    boolean doNotEnterEd1 = false;
    boolean doNotEnterEd2 = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view1);

        final EditText ed1_2 = (EditText) findViewById(R.id.editText02_1);
        final EditText ed2_2 = (EditText) findViewById(R.id.editText02_2);


        ed1_2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (doNotEnterEd1 == true) {
                    return;
                }
                doNotEnterEd1 = true;

                try {
                    if (ed1_2.getText().toString().equals("")) {
                        ed2_2.setText("");
                    } else {
                        ed2_2.setText(String.format("%.3f", (Double.parseDouble(ed1_2.getText().toString()) * CVtokWfactor_2)));
                    }

                } catch (NumberFormatException e) {
                }
                doNotEnterEd2 = false;

            }
        });

        ed2_2.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {

                if (doNotEnterEd2 == true) {
                    return;
                }

                doNotEnterEd1 = true;
                try {
                    if (ed2_2.getText().toString().equals("")) {
                        ed1_2.setText("");
                    } else {
                        ed1_2.setText(String.format("%.3f", (Double.parseDouble(ed2_2.getText().toString()) * CVtokWfactor_2)));    //Here do the conversion as you like, replace CVtokWfactor.
                    }

                } catch (NumberFormatException e) {
                }

                doNotEnterEd1 = false;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
        });


    }


}

and view1.xml (I just changed the content to my two fields from the other app I made,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.juliengenoud.percentsamples.view_1">


    <EditText
        android:id="@+id/editText02_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50sp"
        android:gravity="center"
        android:hint="Potência CV"
        android:inputType="numberDecimal"
        android:textSize="30sp" />

    <EditText
        android:id="@+id/editText02_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText02_1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50sp"
        android:gravity="center"
        android:hint="Potência kW"
        android:inputType="numberDecimal"
        android:textSize="30sp" />


</RelativeLayout>

What do you think I could do to make it work? That part of the percent for me believed what is neither necessary, if they have any other ref to indicate me please.

Thank you very much! Cheap

    
asked by anonymous 22.07.2015 / 16:50

0 answers