android - How to block keyboard when focusing on EditText -


in android application, have use custom number-pad placed in view next edittexts input numbers, need:

  1. edittext should have focus when user tap them have cursor position insert number when user hits key.
  2. soft keyboard should not appear when edit text focused (but cannot hide keyboard setting android:windowsoftinputmode="statehidden" because there search box in screen still need use soft keyboard)

i have searched , found many articles hiding keyboard programmingly when being shown, or set input type/focusable....to not show keyboard....but not meet need.

anybody have solution case please me.

this 1 took inspiration csipsimple project , implemented own

here code same create custom numberpad here mine layout dialpad buttons 1 9 , * #

package com.xyz.custom;  import java.util.hashmap; import java.util.map;  import com.xyz.payphone.r;  import android.annotation.suppresslint; import android.content.context; import android.media.tonegenerator; import android.util.attributeset; import android.util.sparsearray; import android.widget.imagebutton; import android.widget.tablelayout; import android.view.keyevent; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener;  public class dialpadnovanet extends tablelayout implements onclicklistener {      private ondialkeylistener ondialkeylistener;     @suppresslint("usesparsearrays")     private static final map<integer, int[]> digits_btns = new hashmap<integer, int[]>();     private static final string tag = "dialpad";      static {         digits_btns.put(r.id.button0, new int[] { tonegenerator.tone_dtmf_0,                 keyevent.keycode_0 });         digits_btns.put(r.id.button1, new int[] { tonegenerator.tone_dtmf_1,                 keyevent.keycode_1 });         digits_btns.put(r.id.button2, new int[] { tonegenerator.tone_dtmf_2,                 keyevent.keycode_2 });         digits_btns.put(r.id.button3, new int[] { tonegenerator.tone_dtmf_3,                 keyevent.keycode_3 });         digits_btns.put(r.id.button4, new int[] { tonegenerator.tone_dtmf_4,                 keyevent.keycode_4 });         digits_btns.put(r.id.button5, new int[] { tonegenerator.tone_dtmf_5,                 keyevent.keycode_5 });         digits_btns.put(r.id.button6, new int[] { tonegenerator.tone_dtmf_6,                 keyevent.keycode_6 });         digits_btns.put(r.id.button7, new int[] { tonegenerator.tone_dtmf_7,                 keyevent.keycode_7 });         digits_btns.put(r.id.button8, new int[] { tonegenerator.tone_dtmf_8,                 keyevent.keycode_8 });         digits_btns.put(r.id.button9, new int[] { tonegenerator.tone_dtmf_9,                 keyevent.keycode_9 });         digits_btns.put(r.id.buttonpound, new int[] {                 tonegenerator.tone_dtmf_p, keyevent.keycode_pound });         digits_btns.put(r.id.buttonstar, new int[] { tonegenerator.tone_dtmf_s,                 keyevent.keycode_star });     };      private static final sparsearray<string> digits_names = new sparsearray<string>();      static {         digits_names.put(r.id.button0, "0");         digits_names.put(r.id.button1, "1");         digits_names.put(r.id.button2, "2");         digits_names.put(r.id.button3, "3");         digits_names.put(r.id.button4, "4");         digits_names.put(r.id.button5, "5");         digits_names.put(r.id.button6, "6");         digits_names.put(r.id.button7, "7");         digits_names.put(r.id.button8, "8");         digits_names.put(r.id.button9, "9");         digits_names.put(r.id.buttonpound, "pound");         digits_names.put(r.id.buttonstar, "star");     };      public dialpadnovanet(context context) {         super(context);         layoutinflater inflater = layoutinflater.from(context);         inflater.inflate(r.layout.dialpad_novanet, this, true);      }      public dialpadnovanet(context context, attributeset attrs) {         super(context, attrs);         layoutinflater inflater = layoutinflater.from(context);         inflater.inflate(r.layout.dialpad_novanet, this, true);     }      private void dispatchdialkeyevent(int buttonid) {         if (ondialkeylistener != null && digits_btns.containskey(buttonid)) {             int[] datas = digits_btns.get(buttonid);             ondialkeylistener.ontrigger(datas[1], datas[0]);         }     }      @override     public void onclick(view v) {         dispatchdialkeyevent(v.getid());      }      public void setondialkeylistener(ondialkeylistener listener) {         ondialkeylistener = listener;     }      @override     protected void onfinishinflate() {         super.onfinishinflate();          (int buttonid : digits_btns.keyset()) {             imagebutton button = (imagebutton) findviewbyid(buttonid);             if (button != null) {                 button.setonclicklistener(this);             }         }      }      public interface ondialkeylistener {          /**          * called when user make action          *           * @param keycode          *            keycode pressed          * @param dialtone          *            corresponding dialtone          */         void ontrigger(int keycode, int dialtone);     }      boolean mforcewidth = false;      public void setforcewidth(boolean forcewidth) {         mforcewidth = forcewidth;     }      protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {         super.onmeasure(widthmeasurespec, heightmeasurespec);         if (mforcewidth) {             setmeasureddimension(measurespec.getsize(widthmeasurespec),                     getmeasuredheight());         }     };     /*      * public void applytheme(theme t) {      *       * log.d(tag, "theming in progress"); for(int buttonid :      * digits_btns.keyset()) {      *       * imagebutton b = (imagebutton) findviewbyid(buttonid); // need use      * state list reused t.applybackgroundstatelistdrawable(b, "btn_dial");      *       * // src of button drawable src =      * t.getdrawableresource("dial_num_"+digits_names.get(buttonid)); if(src !=      * null) { b.setimagedrawable(src); }      *       * // padding of button t.applylayoutmargin(b, "dialpad_btn_margin"); }      *       * }      */  } 

now create custom edittext

public class dialeredittext extends edittext {      private static final string tag="dialeredittext";     private boolean isdigit=null;     private method showsoftinputonfocus=null;      public dialeredittext(context context, attributeset attrs) {         super(context, attrs);          setisdigit(true, false);     }      public synchronized void setisdigit(boolean isdigit, boolean autofocus) {         if(this.isdigit == null || this.isdigit != isdigit) {             this.isdigit = isdigit;             reflexsetshowsoftinputonfocus(!isdigit);             if (isdigit) {                 setrawinputtype(inputtype.type_class_text | inputtype.type_text_flag_no_suggestions);                 settextsize(typedvalue.complex_unit_px, getcontext().getresources().getdimension(r.dimen.dialpad_digits_text_size));             } else {                 setinputtype(inputtype.type_class_text | inputtype.type_text_variation_email_address                         | inputtype.type_text_flag_no_suggestions);                 settextsize(typedvalue.complex_unit_sp, 14);             }         }         applykeyboardshowhide(autofocus);     }        @override     protected void onfocuschanged(boolean focused, int direction, rect previouslyfocusedrect) {         super.onfocuschanged(focused, direction, previouslyfocusedrect);         if(focused) {             applykeyboardshowhide(false);         }else {             final inputmethodmanager imm = ((inputmethodmanager) getcontext()                     .getsystemservice(context.input_method_service));             if(imm != null && imm.isactive(this)) {                 imm.hidesoftinputfromwindow(getapplicationwindowtoken(), 0);             }         }     }      @override     public boolean ontouchevent(motionevent event) {         final boolean ret = super.ontouchevent(event);         // must done after super.ontouchevent()         applykeyboardshowhide(false);         return ret;     }  /*     @override     public boolean requestfocus(int direction, rect previouslyfocusedrect) {         boolean ret = false;         if(!isdigit) {             ret = super.requestfocus(direction, previouslyfocusedrect);         }         applykeyboardshowhide(false);         return ret;     } */      private void applykeyboardshowhide(boolean autofocus) {         final inputmethodmanager imm = ((inputmethodmanager) getcontext()                 .getsystemservice(context.input_method_service));         if (imm != null) {             if(isdigit) {                 if(imm.isactive(this)) {                     imm.hidesoftinputfromwindow(getapplicationwindowtoken(), 0);                 }             }else if(autofocus) {                 imm.showsoftinput(this, 0);             }         }     }      @override     public void sendaccessibilityeventunchecked(accessibilityevent event) {         if (event.geteventtype() == accessibilityevent.type_view_text_changed) {             // since we're replacing text every time add or remove             // character, read difference. (issue 5337550)             final int added = event.getaddedcount();             final int removed = event.getremovedcount();             final int length = event.getbeforetext().length();             if (added > removed) {                 event.setremovedcount(0);                 event.setaddedcount(1);                 event.setfromindex(length);             } else if (removed > added) {                 event.setremovedcount(1);                 event.setaddedcount(0);                 event.setfromindex(length - 1);             } else {                 return;             }         } else if (event.geteventtype() == accessibilityevent.type_view_focused) {             // parent edittext class lets tts read "edit box" when view             // has focus,             // confuses users on app launch (issue 5275935).             return;         }         super.sendaccessibilityeventunchecked(event);     }      @override     protected void onlayout(boolean changed, int left, int top, int right, int bottom) {         super.onlayout(changed, left, top, right, bottom);         // here ensure hide keyboard         // since fired when virtual keyboard         // blink no better way found hide keyboard sure         applykeyboardshowhide(false);     }      private void reflexsetshowsoftinputonfocus(boolean show) {         if(showsoftinputonfocus != null) {             utilitywrapper.safelyinvokemethod(showsoftinputonfocus, this, show);         }     }  } 

now can use xml avoid padding , stuff cause thats app specific

<linearlayout 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:orientation="vertical" >           <com.novanet.custom.dialeredittext             android:id="@+id/edtdialer"             android:layout_width="0dp"             android:layout_height="match_parent"             android:layout_weight="70" />        <com.novanet.custom.dialpadnovanet         android:id="@+id/dial_pad"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_gravity="center_vertical"         android:layout_weight="65"         android:paddingbottom="10dip"         android:paddingleft="5dip"         android:paddingright="5dip" />    </linearlayout> 

now in activity u can this

edttext=(dialeredittext)findviewbyid(r.id.edtdialer);         dialpad=(dialpadnovanet)findviewbyid(r.id.dial_pad);         dialpad.setondialkeylistener(new ondialkeylistener() {              @override             public void ontrigger(int keycode, int dialtone) {                 log.v(tag,"key "+keycode);                 keyevent event=new keyevent(keyevent.action_down, keycode);          edttext.onkeydown(keycode, event);              }         }); 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -