• cathome 猫家 开发日记-自定义控件


    概述

    根本就是一句话“:  自定义一个类(group view的派生类),在构造函数中,生成view,并加入他们。   语句: this.addView(view); 所有的步骤,都是这句话的扩展和补充罢了。

    一般的流程。

    整个流程都好理解,就是属性是如何加载上去的。没有去认真研究。属性问题操作上就是感觉

    one: 建立属性文件

    two.在布局文件多加了一行 xmlns:custom="http://schemas.android.com/apk/res-auto"

    three:好像这个时候。就可以在控件中填入自定义属性了。

    four:后台 特性语句。就可以获得属性了。TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.arr_postComment)

    感觉android 应该可以把这个做的不要那么抽象,直接在控件中加入属性集合的名字多好。让人好理解。

    1.想下组合控件需要哪些子控件。

    2.建立一个class ,继承于 FrameLayout,LinearLayout或者RelativeLayout 。 定义构造函数, 在函数中,加载xml布局或手写代码生成view。 加入view到这个类中(要注意的是我们的类也是一个view)。

    3.一般有事件需要处理。那么采用回调来处理 。

    4.一般需要给控件加入属性。那么建立一个attrs.xml属性集文件,即你要自定义控件的属性,

    在使用空间的文件中。额外加入xmlns:custom="http://schemas.android.com/apk/res-auto"

    在控件上。写custom:hint="测试属性" (虽然不会自动补全。但是是可以用的)

    在代码中获得

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.arr_postComment);

    String hint= typedArray.getString(R.styleable.arr_postComment_hint);

    举例

    自定义控件,布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
            <EditText android:id="@+id/tv_comment" android:layout_width="40dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="0dp" android:layout_marginRight="10dp" android:layout_toLeftOf="@id/tv_submit" android:background="@drawable/shape_rectangle_transparency1_corner_grey" android:hint="我觉得这种猫科动物..." android:lines="2" android:maxLength="30" android:paddingLeft="4dp" android:singleLine="false" android:textSize="14dp" />
            <TextView android:id="@+id/tv_submit" android:layout_width="40dp" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:background="@drawable/shape_rectangle_transparency1_corner_green" android:gravity="center" android:text="递交" />
    </RelativeLayout>

    属性文件

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="arr_postComment">
            <attr name="hint" format="string" />
            <attr name="btn_text" format="string"/>
        </declare-styleable>
        <declare-styleable name="arr_postComment2">
            <attr name="hint2" format="string" />
            <attr name="btn_text2" format="string"/>
        </declare-styleable>
    </resources>

    控件类

    package com.android.linson.catshome.Control.adapter;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.EditText;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    import com.android.linson.catshome.R;
    
    import java.util.logging.Handler;
    
    //<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
    //        <EditText android:id="@+id/tv_comment" android:layout_width="40dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="0dp" android:layout_marginRight="10dp" android:layout_toLeftOf="@id/tv_submit" android:background="@drawable/shape_rectangle_transparency1_corner_grey" android:hint="我觉得这种猫科动物..." android:lines="2" android:maxLength="30" android:paddingLeft="4dp" android:singleLine="false" android:textSize="14dp" />
    //              <TextView android:id="@+id/tv_submit" android:layout_width="40dp" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:background="@drawable/shape_rectangle_transparency1_corner_green" android:gravity="center" android:text="递交" />
    //        </RelativeLayout>
    
    public class SubmitCommentCustom extends RelativeLayout implements View.OnClickListener
    {
        private EditText mComment;
        private TextView mSubmit;
        private OnSubmit mHandle;
    
        public SubmitCommentCustom(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            //load layout file 2.findcontrols 3.setup sub control 4. add each view
    
            View view=View.inflate(this.getContext(), R.layout.lsuipostcomment, null);
            mComment=view.findViewById(R.id.tv_comment);
            mSubmit=view.findViewById(R.id.tv_submit);
    
            mSubmit.setOnClickListener(this);
            this.addView(view);
    
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.arr_postComment);
    
            String hint= typedArray.getString(R.styleable.arr_postComment_hint);
            String btntext=typedArray.getString(R.styleable.arr_postComment_btn_text);
            mComment.setHint(hint);
            mSubmit.setText(btntext);
        }
    
    
        @Override
        public void onClick(View v)
        {
            if(mHandle!=null)
            {
                String str=mComment.getText().toString();
                mHandle.OnSubmited(str);
            }
        }
    
        public void SetOnSubmit(OnSubmit onsubmit)
        {
            mHandle=onsubmit;
        }
    
        public interface OnSubmit
        {
            void OnSubmited(String someThing);
        }
    
    }

    使用范例-布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent">
    
            <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent">
                <com.android.linson.catshome.Control.adapter.SubmitCommentCustom custom:hint="测试属性"  custom:btn_text="提交" android:layout_width="match_parent" android:layout_height="40dp" android:id="@+id/postcomment" android:layout_margin="4dp" ></com.android.linson.catshome.Control.adapter.SubmitCommentCustom>
            </RelativeLayout>
       
    </RelativeLayout>

    使用范例-activity

    mpost=findViewById(R.id.postcomment);
            mpost.SetOnSubmit(new SubmitCommentCustom.OnSubmit()
            {
                @Override
                public void OnSubmited(String someThing)
                {
                    Log.i("DEBUG", "OnSubmited article: "+someThing);
                }
            });
  • 相关阅读:
    postgresql 53300错误
    linux su失败:无法设置用户ID:资源暂时不可用
    shell中使用带密码的方式直接pg_dump和psql
    pg数据库查询表大小
    linux安装postgresql简洁版
    检查linux版本命令
    博客园后台搜索自己的博客
    欧式洗车
    做生意
    无线AP隔离
  • 原文地址:https://www.cnblogs.com/lsfv/p/9938031.html
Copyright © 2020-2023  润新知