Friday, April 13, 2012

How to let the input of EditText only be available for hex numbers.


Recently I was working on a project, which requests the user to input the MAC address in hex manually. It will be really annoying if I need to check the validation of the address each time the user typed in. So I decided to see if I can make some constraints for the inputs of the EditText.

UPDATE

By going through a lot of searches, I found a easy way to implement this.


Just add the following attributes to the EditText in the XML file.

      android:digits="0123456789ABCDEF"
This could make the inputs could only be the numbers above. If the user try to type in other characters, the system will just automatically ignore it.


      android:inputType="textCapCharacters"
Also, this could your input keyboard be cap characters automatically, thus you the user don't need to cap it manually.

Update: For clarity, I will post partial code of the sample project below, hope it will help.


package com.antonio081014.sample.edittextsample;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends Activity {

 private EditText tv;

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

  tv = (EditText) findViewById(R.id.edittext_text);
  tv.setOnEditorActionListener(new OnEditorActionListener() {

   @Override
   public boolean onEditorAction(TextView v, int actionId,
     KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE
      || event.getAction() == KeyEvent.ACTION_DOWN
      && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
     Log.d("", tv.getText().toString());
     return true;
    }
    return false;
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}


<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/edittext_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:digits="0123456789ABCDEF"
        android:hint="@string/hello_world"
        android:imeOptions="actionDone"
        android:inputType="textCapCharacters" />

</RelativeLayout>

No comments :

Post a Comment