Android中的自定义控件

本文分享了在Android开发过程中自定义控件的经验,包括自定义对话框与组合控件的具体实现步骤。从布局文件的设计到代码逻辑的实现,帮助开发者理解如何按需定制界面元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近有不少人问我Android中的自定义控件的问题,今天就抽时间把我在工作中用的东西,写一下,希望可以帮助到更多的人.大家在查看的过程中如果发现不对的地方,及时指正,大家共同进步
1.自定义对话框.
在安卓中给我们开发人员准备了各自各样的对话框,但有时候还是达不到我们的需求,这时候就需要我们自定义一个对话框,接下来,就以一个密码初始化的对话框作为案例
1)先写布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#44000000"
        android:drawableLeft="@drawable/dialog_title_default_icon"
        android:padding="5dp"
        android:text="密码验证"
        android:textSize="18sp" />

    <EditText
       android:background="@drawable/input_pwd_shape"
        android:id="@+id/et_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:hint="请输入密码"
        android:password="true" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:background="@drawable/setting_pwd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" 
            android:text="确定"
            android:id="@+id/btu_ok"
            />
        <Button
            android:layout_width="0dp"
            android:background="@drawable/setting_pwd"
            android:layout_height="wrap_content"
            android:layout_weight="1" 
            android:text="取消"
            android:id="@+id/btu_cancel"
            />


    </LinearLayout>

</LinearLayout>

好了,布局文件搞定,接下来就是该怎么用了

AlertDialog.Builder builder = new Builder(需要加载对话框的activity);
//将builder转换为dialog,方便cancel
AlertDialog dialog = builder.create();
//加载之前写好的布局文件
View view = View.inflate(需要加载对话框的activity.this, R.layout.resetpwd_item,null);

以上就是关键的代码,接下来就是为按钮设置点击事件,做为有经验的开发人员接下来的都是so easy了,我就不献丑了
看效果这里写图片描述
还是很丑,你可以自定义输入框的形状,我这里就不赘述了,有时间,专门写一下shape
自定义对话框就搞定了,接下来就是自定义组合控件了
2.自定义组合控件及使用
1)
记住是在values目录下

自定义属性;
        <?xml version="1.0" encoding="utf-8"?>
        //里面的代码如果不会写,可以参考安卓的源代码
        <resources>
            //枚举属性:如果希望SettingItemView的背景取值只能是某些值, 可以使用自定义枚举属性. 
            //写法参考TextView的ellipsize.
                <attr name="sivBackground">
                    <enum name="first" value="0" />
                    <enum name="middle" value="1" />
                    <enum name="last" value="2" />
                </attr>
                <attr name="sivIsShowToggle" format="boolean" />
                <attr name="sivTitle" format="string" />
        </resources>

2) 在控件中使用自定义属性时,需要在布局文件中声明命名空间:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

            <com.itheima.mobilesafe.view.SettingItemView
                android:clickable="true"
                android:layout_marginTop="10dp"
                android:id="@+id/siv_autoupdate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                itheima:sivBackground="first"
                itheima:sivIsShowToggle="true"

                />
            <!-- ... -->
    </LinearLayout>

3)在代码中读取属性,设置背景的选择器,设置是否显示开关,显示标题;

 public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                //
                // itheima:sivBackground="first"
                // itheima:sivIsShowToggle="true"
                // itheima:sivTitle="设置自动更新
                backgroundRes = attrs.getAttributeIntValue(NAMESPACE, "sivBackground",
                        0);
                isShowToggle = attrs.getAttributeBooleanValue(NAMESPACE,
                        "sivIsShowToggle", false);
                title = attrs.getAttributeValue(NAMESPACE, "sivTitle");

                4、在构造方法中调用initView(context)方法;
                // 初始化界面
                initView(context);
            }

        public void initView(Context context) {
                View view = View.inflate(context, R.layout.item_setting_view, null);
                ivToggle = (ImageView) view.findViewById(R.id.iv_toggle);
                tvTitle = (TextView) view.findViewById(R.id.tv_title);
                if (isShowToggle) { // 显示开关
                    ivToggle.setVisibility(View.VISIBLE);
                } else {
                    // 不显示开关
                    ivToggle.setVisibility(View.GONE);
                }
                // 显示条目的标题
                tvTitle.setText(title);
                switch (backgroundRes) {
                case BACKGROUND_FIRST:
                    setBackgroundResource(R.drawable.setting_item_first_seletor);
                    break;
                case BACKGROUND_MIDDLE:
                    setBackgroundResource(R.drawable.setting_item_middle_seletor);
                    break;
                case BACKGROUND_LAST:
                    setBackgroundResource(R.drawable.setting_item_last_seletor);
                    break;
                }
                // 把界面显示在自己身上
                addView(view);
            }

好了,自定义控件及其使用就介绍完了,这就是我在项目中使用过的自定义控件,代码基本没有什么改动,相信你看起来也不会很费力.
大家在看的过程中如果发现有什么不妥的地方,还希望给我留言

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值