自定义组合控件属性,自定义对话框,md5加密,自定义按钮背景,

来源:互联网 发布:网校程序源码 编辑:程序博客网 时间:2024/06/09 17:10

12.自定义组合控件的属性

通过自定义组合控件类的全路径名定义组合控件的同时,还可以添加自定义控件的属性,添加属性的话就要在该布局文件中添加自定义的命名空间,格式为:

xmlns:unicair="http://schemas.android.com/apk/res/com.uc.mobilesafe(包名)"

属性的定义格式如:

<span style="font-size:18px;">    <com.uc.mobilesafe.ui.SettingItemView        android:id="@+id/siv_update"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        unicair:desc_off="自动更新已关闭"        unicair:desc_on="自动更新已打开"        unicair:title="设置是否自动更新" >    </com.uc.mobilesafe.ui.SettingItemView></span>

自定义的这些属性,需要在res/values目录下创建的attrs.xml文件中声明。如:

<span style="font-size:18px;"><declare-styleable name="SettingItemView">    <attr name="title" format="string" />    <attr name="desc_off" format="string" />    <attr name="desc_on" format="string" /></declare-styleable></span>

再把自定义的属性和自定义组合控件类进行关联:调用的是该类带有两个参数的构造方法,属性都被封装在AttributeSet属性集合中。通过attrs.getAttributeValue(命名空间,属性名);方法可以获得属性的实例。然后,自定义组合控件就可以调用属性了。


13.自定义对话框

系统自带的对话框都是没有输入框的,为了设置密码,我们可以自定义带输入框的对话框:

自定义对话框布局文件,然后通过View.inflate();将布局文件转换成View对象,再调用对话框构造器builder的setView(view)方法构造出该对话框,然后再builder.show()给show出来。

对话框在低版本上显示效果不佳,可以将背景设置为白色,并且在创建对话框时用:

dialog1 = builder.create(); dialog1.setView(view,0,0,0,0);  dialog1.show();


14.MD5算法加密

算法步骤:1.用每个byte去和11111111做与运算,得到的是int类型的值:byte & 0xff

2.把int类型转换成16进制并返回String类型

3.不满八个二进制位就补全

<span style="font-size:18px;">public static String getMd5(String password) {try {// 得到一个信息摘要器的实例MessageDigest digest = MessageDigest.getInstance("md5");byte[] result = digest.digest(password.getBytes());StringBuffer buffer = new StringBuffer();for (byte b : result) {// 与运算int num = b & 0xff;// 转换成16进制String str = Integer.toHexString(num);// 补全不满8个二进制位if (str.length() == 1) {buffer.append("0");}buffer.append(str);}return buffer.toString();} catch (NoSuchAlgorithmException e) {e.printStackTrace();return "";}}</span>

15.自定义按钮背景

参考API,App Resources-->Resource Types-->Drawable中的State List下.

在res目录下创建drawable目录,在该目录下创建button.xml文件。



0 0