天天看点

手机卫士05-自定义对话框

好,今天天我们在完成我们这个项目里面的一个自定义对话框的功能啦,它是在我们的第一个功能,手机防盗里面的,我们在给手机防盗那里加一个登陆的操作,这样会更安全一些,所以我们就用到了一个对话框,为了让它更好看一些,而且也学习一下怎样自定义对话框,所以我们就开始学习一下啦

首先,我们先给我们的手机防盗的启动界面,加一个快捷启动的方式,就是在拨打电话的时候,输入一个特定的号码,然后就会启动手机防盗那个界面,听起来,是不是很酷呢,其实很简单的,就是用一个广播接收都,来接收打电话时发出来的广播,然后捕获它,然后就可以进行我们想要的操作啦,好,直接上代码

先新建一个类com.xiaobin.security.receiver.callphonereceiver

<font color="#333333"><font face="arial">package com.xiaobin.security.receiver;

import android.content.broadcastreceiver;

import android.content.context;

import android.content.intent;

import com.xiaobin.security.ui.lostprotectedactivity;

public class callphonereceiver extends broadcastreceiver

{

        @override

        public void onreceive(context context, intent intent)

        {

                string outphonenumber = this.getresultdata();

                if(outphonenumber.equals("1314"))        //当监听到用户拨打的是1314的时候,就进行下面的操作,你可以把拨打的号码做成参数的形式,让用户可配置

                {

                        intent i = new intent(context, lostprotectedactivity.class);

                        //这个很重要,如果没有这一句,那就会报错,这一句是因为我们是在一个receiver里面启动一个activity的,但activity的启动,都是放到一个栈里面的,

                        //但receiver里面没有那个栈,所以我们要在这里启动一个activity,那就必须要指定这行代码啦

                        i.setflags(intent.flag_activity_new_task);        

                        context.startactivity(i);

                        setresultdata(null);        //这行代码是把广播的数据设置为null,这样就不会把刚刚那个号码拨打出去啦,只会启动我们的activity

                }

        }

}

</font></font>

复制代码

现在还不行的,我们还要在androidmainfest文件里面注册这个广播接收者

<font color="#333333"><font face="arial"><receiver

            android:name="com.xiaobin.security.receiver.callphonereceiver">

            <intent-filter android:priority="1000"><!-- 把优先级设置高一些,以便第一个拿到广播 -->

                <action android:name="android.intent.action.new_outgoing_call"/>

            </intent-filter>

        </receiver></font></font>

当然,我们还要加上相应的权限呢

<font color="#333333"><font face="arial"><uses-permission android:name="android.permission.process_outgoing_calls"/></font></font>

好啦,到现在为止,我们通过拨打电话,来启动手机防盗这个功能就完成的啦,各位可以去试试 现在进入我们今天的重点,那就是自定义对话框啦,先上图,看看我们的效果

手机卫士05-自定义对话框
手机卫士05-自定义对话框
手机卫士05-自定义对话框

下载完之后,直接它两个解压出来,然后把apktool1.5.2.tar里面的那个jar包,放到apktool-install-windows-r05-ibot.tar这个目录下面,如图

手机卫士05-自定义对话框

这样子就会有3个文件的啦,现在我们把android.jar出拷贝到这个目录下面,然后在dos下面进行这个目录,然后就运行apktool

d android.jar

手机卫士05-自定义对话框

就这样,我们就会在刚刚那个目录下面看到有一个解析出来的资源文件夹的啦,而我们系统自己写的ui资源全在里面啦,我们有空的话,可以去看看,系统是怎样自己写的,然后自己就可以相应的进行修改啦好啦,说回我们今天的对话框,系统自己写的对话框,其实就是一个主题,要用的时候,就使用那个主题,那么,它就是在res/value/styles.xml里面的,我们把那个文件打开,然后搜索theme.dialog,然后就可以看到系统自己写的对话框啦

手机卫士05-自定义对话框

好啦,知道这些之后,我们有空就可以参考一下怎样写啦现在我们自己来写,我们在我们的项目里面的styles.xml文件里面写上我们自己的对话框的样式啦

    <style name="mydialog" parent="@android:style/theme.dialog"><!-- 要继承系统原来的对话框 -->

        <item name="android:windowbackground">@drawable/title_background</item><!-- 指定背景颜色 -->

        <item name="android:windownotitle">true</item><!-- 隐藏系统原来的标题栏 -->

    </style>

因为我是教大家怎样自定义对话框,所以为了简单,我只是写了这两个属性而已,有兴趣的,你们可以参考一下系统自己的,然后写多一点,写好我们的样式之后,怎样用呢其实很简单,只要在new一个dialog的时候,指定一下它的样式就行的啦

dialog = new dialog(this, r.style.mydialog);

好啦,我们现在就结合我们今天的逻辑来看一下,我们的对话框的效果,我们今天要做的是,当用户第一次启动这个手机防盗这个功能的时候,要求用户输入一个登录密码,然后以后进入这个功能的时候,都要输入以前设置的密码,而且我们的密码是以md5加密后,存放到sharedpreferences里面的既然是加密,那么我们现在就先来写一个加密的工具类啦com.xiaobin.security.utils.md5encoder

package com.xiaobin.security.utils;

import java.security.messagedigest;

import java.security.nosuchalgorithmexception;

public class md5encoder

        public static string encode(string pwd)

                try

                        messagedigest messagedigest = messagedigest.getinstance("md5");//拿到md5加密的对象

                        byte[] bytes = messagedigest.digest(pwd.getbytes());//返回一个加密后的字节数组

                        stringbuffer sb = new stringbuffer();

                        string tmp;

                        for(int i = 0; i < bytes.length; i++)

                        {

                                tmp = integer.tohexstring(0xff & bytes[i]);//把字节转换为16进制的字符串

                                if(tmp.length() == 1)        //如果这个字符串,只有一个字符,就要补0

                                {

                                        sb.append("0" + tmp);

                                }

                                else

                                        sb.append(tmp);

                        }

                        return sb.tostring();

                catch (nosuchalgorithmexception e)

                        throw new runtimeexception("没有这个加密算法" + e);

好啦,接下来,都是一些很简单的逻辑处理而已,我们直接上代码com.xiaobin.security.ui.lostprotectedactivity

package com.xiaobin.security.ui;

import android.app.activity;

import android.app.dialog;

import android.content.sharedpreferences;

import android.content.sharedpreferences.editor;

import android.os.bundle;

import android.view.view;

import android.view.view.onclicklistener;

import android.widget.button;

import android.widget.edittext;

import android.widget.toast;

import com.xiaobin.security.r;

import com.xiaobin.security.utils.md5encoder;

public class lostprotectedactivity extends activity implements onclicklistener

        private sharedpreferences sp;

        private dialog dialog;

        private edittext password;

        private edittext confirmpassword;

        protected void oncreate(bundle savedinstancestate)

                super.oncreate(savedinstancestate);

                sp = getsharedpreferences("cofig", context.mode_private);

                if(issetpassword())

                        showlogindialog();

                else

                        showfirstdialog();

        private void showlogindialog()

                dialog = new dialog(this, r.style.mydialog);

                view view = view.inflate(this, r.layout.login_dialog, null);

                password = (edittext) view.findviewbyid(r.id.et_protected_password);

                button yes = (button) view.findviewbyid(r.id.bt_protected_login_yes);

                button cancel = (button) view.findviewbyid(r.id.bt_protected_login_no);

                yes.setonclicklistener(this);

                cancel.setonclicklistener(this);

                dialog.setcontentview(view);

                dialog.show();

        private void showfirstdialog()

                //dialog.setcontentview(r.layout.first_dialog);

                view view = view.inflate(this, r.layout.first_dialog, null);        //这样来填充一个而已文件,比较方便

                password = (edittext) view.findviewbyid(r.id.et_protected_first_password);

                confirmpassword = (edittext) view.findviewbyid(r.id.et_protected_confirm_password);

                button yes = (button) view.findviewbyid(r.id.bt_protected_first_yes);

                button cancel = (button) view.findviewbyid(r.id.bt_protected_first_no);

        private boolean issetpassword()

                string pwd = sp.getstring("password", "");

                if(pwd.equals("") || pwd == null)

                        return false;

                return true;

        public void onclick(view v)

                switch(v.getid())

                        case r.id.bt_protected_first_yes :

                                string fp = password.gettext().tostring().trim();

                                string cp = confirmpassword.gettext().tostring().trim();

                                if(fp.equals("") || cp.equals(""))

                                        toast.maketext(this, "密码不能为空", toast.length_short).show();

                                        return;

                                        if(fp.equals(cp))

                                        {

                                                editor editor = sp.edit();

                                                editor.putstring("password", md5encoder.encode(fp));

                                                editor.commit();

                                        }

                                        else

                                                toast.maketext(this, "两次密码不相同", toast.length_short).show();

                                                return;

                                dialog.dismiss();

                                break;

                        case r.id.bt_protected_first_no :

                                finish();

                        case r.id.bt_protected_login_yes :

                                string pwd = password.gettext().tostring().tostring();

                                if(pwd.equals(""))

                                        toast.maketext(this, "请输入密码", toast.length_short).show();

                                        string str = sp.getstring("password", "");

                                        if(md5encoder.encode(pwd).equals(str))

                                                dialog.dismiss();

                                                toast.maketext(this, "密码错误", toast.length_short).show();

                        case r.id.bt_protected_login_no :

                        default :

另外,还有两个布局文件,一个是第一次登录时,设置密码的,一个是以后登录时,输入密码的first_dialog

<?xml version="1.0" encoding="utf-8"?>

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="300dip"

    android:layout_height="280dip"

    android:gravity="center_horizontal"

    android:orientation="vertical" >

    <textview

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textcolor="@android:color/white"

        android:textsize="24sp"

        android:text="@string/setpassword"/>

    <linearlayout

        android:layout_width="300dip"

        android:layout_height="180dip"

        android:background="#ffc8c8c8"

        android:orientation="vertical">

        <textview

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/setpassworddescription"/>

        <edittext

            android:id="@+id/et_protected_first_password"

            android:layout_width="match_parent"

            android:inputtype="textpassword"/>

            android:text="@string/againpassword"/>

            android:id="@+id/et_protected_confirm_password"

        <linearlayout

            android:layout_width="300dip"

            android:layout_height="50dip"

            android:gravity="center"

            android:orientation="horizontal">

            <button

                android:id="@+id/bt_protected_first_yes"

                android:layout_width="140dip"

                android:layout_height="40dip"

                android:text="@string/protectedyes"/>

                android:id="@+id/bt_protected_first_no"

                android:layout_marginleft="10dip"

                android:text="@string/protectedno"/>

        </linearlayout>

    </linearlayout>

</linearlayout>

login_dialog.xml

    android:layout_height="180dip"

        android:text="@string/login"/>

        android:layout_height="120dip"

            android:text="@string/inputpassword"/>

            android:id="@+id/et_protected_password"

                android:id="@+id/bt_protected_login_yes"

                android:id="@+id/bt_protected_login_no"

好啦,今天的代码就到这里啦,代码有点多,有什么不明白的,欢迎留言,有什么要指导的,也欢迎留言!!!

手机卫士05-自定义对话框

kb, 下载次数: 173)

继续阅读