onKeyDown onKeyUp dispatchKeyEvent OnEditorActionListener EditText
onKeyDown onKeyUp dispatchKeyEvent OnEditorActionListener EditText
异常 SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
1 <com.google.android.material.textfield.TextInputLayout 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" 4 5 6 android:background="@color/ad_setting_normal" 7 > 8 9 <!-- 10 <EditText 11 android:id="@+id/et_in_put_id" 12 android:layout_width="1dp" 13 android:layout_weight="3" 14 android:layout_height="wrap_content" 15 android:background="@color/black_transparent" 16 android:padding="5dip" 17 android:textIsSelectable="false" 18 /> 19 20 android:hint="@string/in_put_ic_or_id" 21 android:textIsSelectable="false" 22 --> 23 24 <com.google.android.material.textfield.TextInputEditText 25 android:id="@+id/et_in_put_id" 26 android:layout_width="@dimen/dp_150" 27 android:layout_height="wrap_content" 28 android:background="@color/black_transparent" 29 android:maxLines="1" 30 android:lines="1" 31 android:singleLine="false" 32 android:inputType="textNoSuggestions" 33 /> 34 <!-- 35 36 --> 37 38 39 </com.google.android.material.textfield.TextInputLayout>
解决办法,取消布局中的代码,使用 onKeyDown onKeyUp dispatchKeyEvent 其中任意一项事件替代。
1 @Override 2 public boolean onKeyDown(int keyCode, KeyEvent event){ 3 4 System.out.println("onKeyDown keyCode:"+keyCode +" onKeyDown:"+event.getKeyCode()); 5 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){ 6 7 //这里写自己的操作,此方法兼容Android 1.0到Android 2.1 8 9 } 10 return false; 11 } 12 13 14 @Override 15 public boolean onKeyUp(int keyCode, KeyEvent event){ 16 17 System.out.println("onKeyUp keyCode:"+keyCode +" onKeyDown:"+event.getKeyCode()); 18 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){ 19 20 //这里写自己的操作,此方法兼容Android 1.0到Android 2.1 21 22 } 23 return false; 24 }
1 public static String keyvule=""; 2 public static Long time; 3 @Override 4 public boolean dispatchKeyEvent(KeyEvent event) { 5 if(time==null){ 6 time=System.currentTimeMillis(); 7 } 8 Long now=System.currentTimeMillis(); 9 if(now < time+500){ 10 if(event.getAction() == KeyEvent.ACTION_DOWN){ 11 12 } 13 if(event.getAction() == KeyEvent.ACTION_UP){ 14 time=System.currentTimeMillis(); 15 String s = String.valueOf(new char[]{event.getNumber()}); 16 keyvule += ""+s; 17 } 18 19 if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN ){ 20 21 if(!keyvule.trim().equals("")){ 22 //得到正确结果 2659380460 23 JSONObject obj = new JSONObject(); 24 obj.put("key", LoginType.A1_id.code); 25 obj.put("value",keyvule); 26 27 Bus.sendObject(SystemConfig.cmd_login_all,obj, ob2->{ 28 if(ob2==null){ 29 System.out.println("Usb串口数据发送失败:"+keyvule + " str :"+keyvule); 30 }else { 31 System.out.println("Usb串口数据发送成功1:"+keyvule + " str :"+keyvule); 32 } 33 }); 34 } 35 keyvule=""; 36 time=null; 37 } 38 39 if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){ 40 41 /*隐藏软键盘*/ 42 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 43 if(inputMethodManager.isActive()){ 44 inputMethodManager.hideSoftInputFromWindow(Main1Activity.this.getCurrentFocus().getWindowToken(), 0); 45 } 46 47 return true; 48 } 49 } 50 51 return super.dispatchKeyEvent(event); 52 }
1 public void ininIDView(View view) { 2 et_in_put_id = view.findViewById(R.id.et_in_put_id); 3 et_in_put_id.setOnEditorActionListener(editorActionListener); 4 et_in_put_id.setFocusable(true);//允许此控件有获得焦点的能力 5 et_in_put_id.setFocusableInTouchMode(true);//(false);//false失去焦点 6 et_in_put_id.requestFocus();//让控件得到焦点 7 et_in_put_id.setEnabled(true); 8 et_in_put_id.setClickable(false);//无效,就不能点击 9 10 // SupportHelper.hideSoftInput(et_in_put_id); 11 // AbstractCmdActivity.hideSoftKeyboard(getApplicationContext(),getCurrentFocus()); 12 // imm.showSoftInput(view,InputMethodManager.SHOW_FORCED); 13 14 15 InputMethodManager imm =(InputMethodManager) _mActivity.getSystemService(Context.INPUT_METHOD_SERVICE); 16 imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 17 18 } 19 20 // 接收到Usb数据 输入结束 21 private TextView.OnEditorActionListener editorActionListener = new TextView.OnEditorActionListener() { 22 @Override 23 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 24 String et_info = et_in_put_id.getText().toString(); 25 System.out.println("编辑动作7:"+et_info); 26 System.out.println("编辑动作8:"+v.getText().toString()); 27 28 et_in_put_id.setText(""); 29 30 LoginType type = LoginType.A1_id; 31 SupportHelper.hideSoftInput(et_in_put_id); 32 // LoginAll.reglogin_all(et_info,type); 33 34 JSONObject obj = new JSONObject(); 35 obj.put("key", LoginType.A1_id.code); 36 obj.put("value",et_info); 37 // stack size 1038KB 38 System.out.println("接收到Usb数据:"+et_info + " str :"+et_info); 39 40 Bus.sendObject(SystemConfig.cmd_login_all,obj, ob2->{ 41 if(ob2==null){ 42 System.out.println("Usb串口数据发送失败:"+et_info + " str :"+et_info); 43 }else { 44 System.out.println("Usb串口数据发送成功1:"+et_info + " str :"+et_info); 45 } 46 47 }); 48 49 return false; 50 } 51 };