自定义控件----继承framLayout --textview与checkBox

//在布局文件中

<com.bwei.administrator.view.zuheView
android:layout_width="match_parent"
android:layout_height="wrap_content"
  android:id="@+id/zh"
></com.bwei.administrator.view.zuheView>
//自定义view中

private TextView tv;
private CheckBox ck;



public zuheView(@NonNull Context context) {
    super(context);
    init();
}

public zuheView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    
    init();
}


public zuheView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}
//进行初始化数据
private void init() {
    //把布局挂载在combineView    View view=View.inflate(getContext(),R.layout.combine_view,this);
    //获取里面的控件
    tv = (TextView) view.findViewById(R.id.tvtext);
    ck = (CheckBox) view.findViewById(R.id.ck);

   

    //设置点击事件......点击的时候多选框的状态改变
      this.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              ck.setChecked(!ck.isChecked());
          }
      });

}
     ///对外的两个方法   包括文本的显示   以及checkbox的是否选中
    public void setText(String text){
        tv.setText(text);
    }
    //对外提供设置是否选中
    public void setChecked(boolean flag){
    ck.setChecked(flag);
    }

      //获取是否选中
   public boolean getChecked(){
    return ck.isChecked();
   }
//在MainActivity中调用设置属性值的方法

    

zuheView = (com.bwei.administrator.view.zuheView) findViewById(R.id.zh);
    zuheView.setChecked(true);
    zuheView.setText("哩哩啦啦咕咕咕咕");
------------------------------自定义属性值-------------------------------------------
  

* values下面创建一个attrs.xml文件...attribute

* 添加标签declare_styable自己声明的样式,name自动提示为类名

* 自定义属性attr,,,name为属性名称

* format为数据类型,,,string boolean

* xml声明的时候引用属性

命名空间:xmlns:dash="http://schemas.android.com/apk/res-auto"

* 获取设置的属性

text = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "text");//第一个参数为命名空间,第二个参数为属性名

         checked = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "checked", false);

=======在主布局中============ 跟布局
xmlns:yukaihua="http://schemas.android.com/apk/res-auto"
控件中
yukaihua:text="咯咯咯咯咯个"
yukaihua:checked="true"           最终显示的值

自定义view中
  定义变量
private String text;
private  boolean checked;
public zuheView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    //获取xml里面给的属性值
     //第一个参数是命名空间  第二个是属性名
    text=attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","text");
     checked=attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto","checked",true);
    init();
}
在init方法中
//设置初始的值
   tv.setText(text);
ck.setChecked(checked);

======values 下的attrs文件========
<declare-styleable name="zuheView">
    <attr name="text" format="string"></attr><!--属性名和属性值-->
    <attr name="checked" format="boolean"/>
</declare-styleable>









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值