Android Fragment with Textwatcher - does not work

Asked

Viewed 135 times

1

Good morning! With the help of the staff, I made a calculation scheme that when typing the value in an 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 halfway I found this link:

https://github.com/JulienGenoud/android-percent-support-lib-sample That while focusing on 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 within this app that I already downloaded ready from this link, the fields are not updated automatically while I type the value. For the textWatcher to work, I have to do something beyond 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 the view1.xml (that I just changed the content to my two fields of the other app that I made, taking the Percent layout from this.

<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 I believed that it is neither necessary, if you have any other ref to indicate me please.

Thank you very much! Cockroach

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.