Liseł Liseł - 6 months ago 11
Android Question

Dynamic value passing from numeric EditText to String in same Activity

This is a 2 part question. Firstable, I have this

EditText
which is strictly set that user can put numbers only by
android:inputType="number"
. Like:

<EditText
android:id="@+id/edit_A1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:imeOptions="actionDone"
android:inputType="number"
android:hint="@string/zeroes" />

<TextView
android:id="@+id/act1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/act_string"
android:layout_centerVertical="true"
android:hint="000," />


What kind of output do I get from it? Is it normal
String
or
Integer
?

Secondly, I would like to set the
TextView
(code above) this way that when I input the number to the
EditText
and hit
OK
button from the appeared numpad, the number will immediately appear in the
TextView
. Both
EditText
and
TextView
are in the same activity. I have the following code:

public static class MainSectionFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);

EditText mAcu1ET = (EditText) rootView.findViewById(R.id.edit_A1);
TextView mAcu1TV = (TextView) rootView.findViewById(R.id.act1);

//????

return rootView;
}
}


to create the view. What kind of
onClickListener
should I implement so my
TextView
would change its value on
OK
button click from the
EditText
numpad? Still learning, go easy on me.

Title of the topic might be confusing, sorry!

Answer

Unfortunately, none of the above solutions worked. I found out the solution here: catching the "OK" button vs pressing "round-arrow" on EditText in Android

To sum up, my code would look like this:

public static class MainSectionFragment extends Fragment {

    private EditText mAcu1ET;
    private TextView mAcu1TV;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.main, container, false);

            mAcu1ET = (EditText) rootView.findViewById(R.id.edit_A1);
            mAcu1TV = (TextView) rootView.findViewById(R.id.act1);

            mAcu1ET.setOnEditorActionListener(new OnEditorActionListener() {
                @Override
                public boolean onEditorAction( TextView v, int actionId, KeyEvent event ) {   
                        mAcu1TV.setText(mAcu1ET.getText().toString());
                     return false;
                }
            });
            return rootView;
        }
    }

Thanks for contributing! Raghandunan somehow led me to it, thanks :)

Comments