
我想创建一个自定义视图,有一个TextVIEw和一个EditText,并在另一个布局中使用它:
<TextVIEw androID:text="Holder" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/tvTitle" androID:layout_weight="1" /><EditText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:inputType="none" androID:text="name" androID:ems="10" androID:ID="@+ID/etAnwser" androID:layout_weight="1" />
我有它的自定义attr
<resources><declare-styleable name="form_item"> <attr name="tvTitle" format="string" /> <attr name="anwserHint" format="string" /> <attr name="anwserText" format="string" /> <attr name="androID:enabled" /></declare-styleable>
在片段中,我执行以下 *** 作:
<rhcloud.com.financialcontrol.tabutil.FormItem androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:enabled="@{state.get()}" form_item:anwserText='@={expense.description}' form_item:tvtitle="Description:" /> 它的单向数据绑定很好用,但无论我改变文本,他都不会在课堂上给我发回调
@InverseBindingMethods(value = { @InverseBindingMethod(type = FormItem.class,attribute = "anwserText"),})public class FormItem extends linearLayout { private TextVIEw tvTitle; private EditText etAnwser; public FormItem(@NonNull Context context) { super(context); LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.form_item,this); tvTitle = (TextVIEw) findVIEwByID(R.ID.tvTitle); etAnwser = (EditText) findVIEwByID(R.ID.etAnwser); } public FormItem(@NonNull Context context,@NonNull String Title) { this(context); setTvTitle(Title); } public FormItem(@NonNull Context context,@NonNull String Title,@NonNull String hint) { this(context,Title); setAnwserHint(hint); } public FormItem(@NonNull Context context,@NonNull String hint,@NonNull String anwserText) { this(context,Title,hint); setAnwserHint(anwserText); } public FormItem(@NonNull Context context,@NonNull AttributeSet attrs) { super(context,attrs); LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.form_item,this); tvTitle = (TextVIEw) findVIEwByID(R.ID.tvTitle); etAnwser = (EditText) findVIEwByID(R.ID.etAnwser); TypedArray a = context.gettheme().obtainStyledAttributes( attrs,R.styleable.form_item,0); try { setTvTitle(a.getString(R.styleable.form_item_tvTitle)); setAnwserHint(a.getString(R.styleable.form_item_anwserHint)); setAnwserText(a.getString(R.styleable.form_item_anwserText)); String isEnabled = a.getString(R.styleable.form_item_androID_enabled); if (isEnabled != null) { setEnable(Boolean.parseBoolean(isEnabled)); } } finally { a.recycle(); } } public voID setTvTitle(String Title) { tvTitle.setText(Title); } public String getTvTitle() { return tvTitle.getText().toString(); } public voID setAnwserHint(String hint) { etAnwser.setHint(hint); } public String getAnwserHint() { return etAnwser.getHint().toString(); } public voID setEnable(boolean isEnable) { tvTitle.setEnabled(isEnable); etAnwser.setEnabled(isEnable); } public voID setAnwserText(String anwserText) { etAnwser.setText(anwserText); } public String getAnwserText() { return etAnwser.getText().toString(); } @InverseBindingAdapter(attribute = "form_item:anwserText") public static String setonAnwserTextAttrChanged(final String value){ Log.d("Test","Calling InverseBindingAdapter: " + value); return value; } @BindingAdapter(value = {"anwserTextAttrChanged"},requireAll = false) public static voID setonAnwserTextAttrChanged(final FormItem vIEw,final InverseBindingListener anwserTextAttrChanged){ Log.d("Test","Calling BindingAdapter: " + vIEw.getAnwserText()); if(anwserTextAttrChanged == null){ }else{ Log.d("Test","Calling here"); anwserTextAttrChanged.onChange(); } } @BindingAdapter(value = {"androID:enabled"}) public static voID customEnable(FormItem formItem,boolean isEnable) { formItem.setEnable(isEnable); }} 有谁知道如何让它正常工作?
完全代码可以在here找到
解决方法 这对我有用:@InverseBindingMethods(value = { @InverseBindingMethod(type = FilterpositionVIEw.class,attribute = "bind:filterStringValue",method = "getFilterValue",event = "androID:filterStringValuetAttrChanged")})public class FilterpositionVIEw extends linearLayout { private FilterpositionBinding mBinding; public FilterpositionVIEw(Context context) { super(context); init(context); } public FilterpositionVIEw(Context context,AttributeSet attrs) { super(context,attrs); init(context); } public FilterpositionVIEw(Context context,AttributeSet attrs,int defStyleAttr) { super(context,attrs,defStyleAttr); init(context); } public FilterpositionVIEw(Context context,int defStyleAttr,int defStyleRes) { super(context,defStyleAttr,defStyleRes); init(context); } private voID init(Context context) { mBinding = DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.filter_position,this,true); setorIEntation(HORIZONTAL); mBinding.filterpositionCheck.setonCheckedchangelistener((buttonVIEw,isChecked) -> { mBinding.filterpositionValue.setEnabled(isChecked); if (!isChecked) mBinding.filterpositionValue.setText(""); }); } /** * Zwraca wpisywany text * * @return wpisane litery tekstu */ public String getFilterValue() { return mBinding.filterpositionValue.getText().toString(); } @BindingAdapter(value = {"bind:filterTitle","bind:filterStringValue","bind:filterDateValue"},requireAll = false) public static voID setFilterBinding(FilterpositionVIEw positionVIEw,String filterTitle,String filterStringValue,Long filterDateValue) { positionVIEw.mBinding.filterpositionTitle.setText(filterTitle); if (filterStringValue != null) positionVIEw.mBinding.filterpositionValue.setText(filterStringValue); if (filterDateValue != null) positionVIEw.mBinding.filterpositionValue.setText(DateTimeFormatUtil.format(filterDateValue)); } @BindingAdapter(value = {"androID:afterTextChanged","androID:filterStringValuetAttrChanged"},requireAll = false) public static voID setTextWatcher(FilterpositionVIEw filterpositionVIEw,final TextVIEwBindingAdapter.AfterTextChanged after,final InverseBindingListener textAttrChanged) { TextWatcher newValue = new TextWatcher() { @OverrIDe public voID beforeTextChanged(CharSequence s,int start,int count,int after) { } @OverrIDe public voID onTextChanged(CharSequence s,int before,int count) { } @OverrIDe public voID afterTextChanged(Editable s) { if (after != null) { after.afterTextChanged(s); } if (textAttrChanged != null) { textAttrChanged.onChange(); } } }; TextWatcher oldValue = ListenerUtil.trackListener(filterpositionVIEw.mBinding.filterpositionValue,newValue,R.ID.textWatcher); if (oldValue != null) { filterpositionVIEw.mBinding.filterpositionValue.removeTextChangedListener(oldValue); } filterpositionVIEw.mBinding.filterpositionValue.addTextChangedListener(newValue); }} 当然,您必须在XML布局中添加@ = {},如下所示:
<com.example.customvIEws.FilterpositionVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" bind:filtertitle="@{@string/filter_product}" bind:filterStringValue="@={sfmodel.product}"/> 总结 以上是内存溢出为你收集整理的Android自定义视图和自定义Attr的双向数据绑定全部内容,希望文章能够帮你解决Android自定义视图和自定义Attr的双向数据绑定所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)