1

I use a radio button to display an editText which has a numeric input. I wish to display the numeric keypad and have tried a number of ideas from the web without success.

EditText e = (EditText) findViewById(R.id.input1);

case R.id.radio_down:
        if (checked) {
            e.setVisibility(View.VISIBLE);
            e.selectAll();
            /*  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
            */
            e.setInputType(InputType.TYPE_CLASS_NUMBER);
            e.setFocusableInTouchMode(true);
            e.requestFocus();
        }
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
GeoffC
  • 61
  • 1
  • 7

3 Answers3

1

Use below code in your xml file.

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Type number"
    android:inputType="number" />
Elesh Baraiya
  • 388
  • 7
  • 12
  • Works OK when first displayed but not after selected by radio button – GeoffC Apr 18 '15 at 05:20
  • @GeoffC ok. i got your solution. put this line on your radio button condition. if you use text use this line mEditText.setInputType(InputType.TYPE_CLASS_TEXT) else mEditText.setInputType(InputType.TYPE_CLASS_NUMBER) – Elesh Baraiya Apr 18 '15 at 07:29
1
    editText1 = (EditText) findViewById(R.id.editText1);

    radioGender = (RadioGroup) findViewById(R.id.radioGender);

    radioGender
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton rb = (RadioButton) group
                            .findViewById(checkedId);
                    if (checkedId == R.id.radioNumber) {
                        Toast.makeText(MainActivity.this, rb.getText(),
                                Toast.LENGTH_SHORT).show();
                        editText1.setKeyListener(DigitsKeyListener
                                .getInstance("0123456789."));
                    }

                }
            });
}

MAINACTIVITY.XML

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="39dp"
    android:ems="10"
    android:inputType="text"
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" >

    <requestFocus />
</EditText>
Jain Nidhi
  • 255
  • 2
  • 15
0

No need to write any java code just define EditText like this

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="39dp"
        android:ems="10"
        android:inputType="number">
      <requestFocus />
   </EditText>
Assad
  • 291
  • 1
  • 4
  • 16