关于Android自定义控件回收机制在5.0以上和以下的区别

在一个APK装在手机5.0以下可以正常运行,然而到5.0以上就报recycle()回收机制错误原因:Android5.0自带自定义控件绘制完成后进行recycle()处理,无需自己再去写一次recycle()方法进行回收。即:

在控件初始化中,获得属性值,赋给控件即可,赋值的代码:

//将属性值设置到控件中
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditTextWithClearBitton);
CharSequence hint = a.getText(R.styleable.EditTextWithClearBitton_hint);
CharSequence text = a.getText(R.styleable.EditTextWithClearBitton_text);
if   (text!= null &&!StringUtil.isEmpty(text.toString().trim())) {
     editText.setText(text);
     //设置光标位置
     editText.setSelection(text.length());
     this .clearImageButton.setVisibility(View.VISIBLE);
} else   {
     if   (hint!= null &&!StringUtil.isEmpty(hint.toString().trim())) {
         editText.setHint(hint);
     }

}

//判断手机系统是否小于Android5.0,小于就进行运行自定义回收处理方法

if(android.os.Build.VERSION.SDK_INT <= 20){

    a.recycle();

}




你可能感兴趣的:(关于Android自定义控件回收机制在5.0以上和以下的区别)