-1

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!

Lisek
  • 599
  • 1
  • 8
  • 25

5 Answers5

0

Let me see if I got it, you want to write a number, press OK and see that number in both fields?

Agustín
  • 1,467
  • 7
  • 22
  • 40
  • public void setNumber(View view){ mAcu1TV.setText(mAcu1ET.getText().toString); } I'm guessing it would be something like that EDIT: Sorry I couldn't set the code type text – Agustín Jan 08 '14 at 17:36
0

Edit text will always give you string but you can type cast it into number.

mAcu1ET.getText().toString();

Code:

public static class MainSectionFragment extends Fragment {

    EditText mAcu1ET;
    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.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
            // TODO Auto-generated method stub
            if(arg1==KeyEvent.KEYCODE_NUMPAD_ENTER){

                                 mAcu1ET.setText(mAcu1TV.getText().toString());
                             }
            return false;
        }
    });

        return rootView;
    }
}
vipul mittal
  • 16,683
  • 3
  • 38
  • 43
0

On button click:

String set = youreditText.getText().toString();
yourtextView.setText(set);

EditText, even if the inputType is in number, will only give you the keyboard configuration for number. You will always need to getText().toString() to get the input.

Rakeeb Rajbhandari
  • 4,853
  • 6
  • 41
  • 74
0

You can use OnKeyListener

http://developer.android.com/reference/android/view/View.OnKeyListener.html

mAcu1ETt.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ENTER) { 
            /* do something */ 
        }
    }
 });
Raghunandan
  • 129,147
  • 24
  • 216
  • 249
0

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 :)

Community
  • 1
  • 1
Lisek
  • 599
  • 1
  • 8
  • 25