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