GeorgeYang'Blog

my technology blog

安卓实用开发小知识 1704

阅读:668 创建时间:17-04-12 09:49:18 tags:android

安卓实用开发小知识 1704

  • Edittext设置android:focusable=“false”禁止输入之后,再次用setFocusable(true)无法输入,可通过以下方法令edittext重新获得焦点可输入文字:

    edittext.setFocusable(true);//--设置edittext是否可以获得焦点 edittext.setFocusableInTouchMode(true);//--设置edittext在touch模式下是否可以获得焦点 edittext.requestFocus();//--调用该方法给指定的view或者它的子view焦点。如果这个view在isFocusable()方法下返回false,或者isFocusableInTouchMode()方法下返回false,这个view不会真正获得焦点 edittext.requestFocusFromTouch();//--调用这个给指定的view或者它的子view焦点。这个特别的变异于(?)requestFocus()的方法会让在这个view被touch时,给view焦点。

参考:http://blog.csdn.net/nonosama0125/article/details/43160349

  • RecyclerView嵌套RecyclerView离开界面再回来时,位置变动博客出处,里面的RecyclerView抢先获取到焦点,导致界面上一到item的顶部,解决办法是在外部的RecyclerView加上:

    android:descendantFocusability="blocksDescendants"

    关于descendantFocusability beforeDescendants:viewgroup会优先其子类控件而获取到焦点

     afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
    
     blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
    

参考:http://blog.csdn.net/yingpaixiaochuan/article/details/53190420

  • 安卓USB开发 1.发现设备

    UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); Map usbList = usbManager.getDeviceList();

2.授权

 if (!usbManager.hasPermission(usbDevice)) {
        usbPermissionReceiver = new UsbPermissionReceiver();
        //申请权限
        Intent intent = new Intent(ACTION_DEVICE_PERMISSION);
        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        IntentFilter permissionFilter = new IntentFilter(ACTION_DEVICE_PERMISSION);
        context.registerReceiver(usbPermissionReceiver, permissionFilter);
        usbManager.requestPermission(usbDevice, mPermissionIntent);
         }

  private class UsbPermissionReceiver extends BroadcastReceiver {
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
             if (ACTION_DEVICE_PERMISSION.equals(action)) {
                 synchronized (this) {
                     UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                     if (device.getDeviceName().equals(usbDevice.getDeviceName()) {
                         if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                           //授权成功,在这里进行打开设备操作
                         } else {
                           //授权失败
                         }
                     }
                 }
             }
         }
     }

3.获取输入输出端口

 usbInterface=usbDevice.getInterface(0);

 for (int index = 0; index < usbInterface.getEndpointCount(); index++) {
                 UsbEndpoint point = usbInterface.getEndpoint(index);
                 if (point.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                     if (point.getDirection() == UsbConstants.USB_DIR_IN) {
                         usbEndpointIn = point;
                     } else if (point.getDirection() == UsbConstants.USB_DIR_OUT) {
                         usbEndpointOut = point;
                     }
                 }
             }

4 数据传输

 usbDeviceConnection = usbManager.openDevice(usbDevice);
 //发送数据
 int ret = usbDeviceConnection.bulkTransfer(usbEndpointOut, data, data.length, DEFAULT_TIMEOUT);
 //接收数据
 int inMax = inEndpoint.getMaxPacketSize(); 
 ByteBuffer byteBuffer = ByteBuffer.allocate(inMax); 
 UsbRequest usbRequest = new UsbRequest(); 
 usbRequest.initialize(connection, inEndpoint); 
 usbRequest.queue(byteBuffer, inMax); 
 if(connection.requestWait() == usbRequest){ 
     byte[] retData = byteBuffer.array(); 
     for(Byte byte1 : retData){ 
         System.err.println(byte1); 
     } 
 }

参考:https://juejin.im/post/58eaff05ac502e006c43e114