安全卫士第三天

23 篇文章 0 订阅
20 篇文章 0 订阅
自定义属性
[1]在values文件夹下新建一个attrs的xml文件
格式如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ligang.huse.cn.ui.SettingView">
        <attr name="title2" format="string" />
        <attr name="des_on" format="string" />
        <attr name="des_off" format="string" />
    </declare-styleable>
</resources>
其中name:代表你定义类的全路径
其他的根据自己的需要按照模式自己写就ok

[2]在SettingView布局文件中使用刚刚定义的属性
    [2.1]先添加这么一条命名空间的属性 xmlns:ligang="http://schemas.android.com/ligang.huse.cn.mobilesafe"
    [2.2]然后就可以通过ligang:+"之前设置的属性名" 设置值了

[3]在SettingView类中的 public SettingView(Context context, AttributeSet attrs)
方法中通过attrs.getAttributeValue方法初始化属性值了

 tiltle2 = attrs.getAttributeValue("http://schemas.android.com/ligang.huse.cn.mobilesafe", "title2");
        des_on = attrs.getAttributeValue("http://schemas.android.com/ligang.huse.cn.mobilesafe", "des_on");
        des_off = attrs.getAttributeValue("http://schemas.android.com/ligang.huse.cn.mobilesafe", "des_off");
设置密码框
[1]给“手机防盗"添加监听事件
   gv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0://手机防盗
                    showsetPasswordDialog();
[2]实现showsetPasswordDialog方法
 //设置手机防盗模块的用户密码问题
    private void showsetPasswordDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(HomeActivity.this);
        builder.setCancelable(false);
        View view = View.inflate(getApplicationContext(), R.layout.dialog_setpassword, null);
        EditText pw= (EditText) view.findViewById(R.id.password_pw);
        EditText confirm= (EditText) view.findViewById(R.id.password_confirm);
        Button ok= (Button) view.findViewById(R.id.password_ok);
        Button cancle= (Button) view.findViewById(R.id.password_cancle);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            dialog.dismiss();
            }
        });
        builder.setView(view);
        //builder.show();
        dialog = builder.create();
        dialog.show();
    }
注释:里面的对话框是写了一个布局文件,然后用View.inflate(),加载布局文件,builder.setView(view);显示布局文件
            AlertDialog 显示有两种方法(1,builder.show() 
                                                             2,  AlterDialog dialog=bulider.create() dialog.show();)
             这里用第二种方式的原因是当我按取消按钮时,需要将对话框消失掉 (dialog.dismiss())
保存密码
ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password=pw.getText().toString().trim();
                String confirm_password=confirm.getText().toString().trim();
                //判断密码是否为空
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(getApplicationContext(),"亲,密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    //判断两次的密码是否一致
                    if(password.equals(confirm_password)){
                        //用sharperdPrefereces来保存密码
                        SharedPreferences.Editor editor=sp.edit();
                        editor.putString("password",password);
                        editor.commit();
                        dialog.dismiss();
                        Toast.makeText(getApplicationContext(),"亲,密码已保存,请放心使用本产品",Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getApplicationContext(),"亲,两次的密码不一致,请重新输入",Toast.LENGTH_LONG).show();
                        //清空密码
                        pw.setText(null);
                        confirm.setText(null);
                    }
                }
            }
        });
输入密码框    
case 0://手机防盗
                        //判断是否有密码
                        if (TextUtils.isEmpty(sp.getString("password", ""))) {
                            showsetPasswordDialog();
                        } else {
                            showentPasswordDialog();
                        }
                        break;
  private void showentPasswordDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(HomeActivity.this);
        builder.setCancelable(false);
        View view = View.inflate(getApplicationContext(), R.layout.dialog_entpassword, null);
        final EditText pw= (EditText) view.findViewById(R.id.password_pw);
        Button ok= (Button) view.findViewById(R.id.password_ok);
        Button cancle= (Button) view.findViewById(R.id.password_cancle);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pws = pw.getText().toString().trim();
                //判断密码是否一致
                if(pws.equals(sp.getString("password",""))){
                    Toast.makeText(getApplicationContext(),"亲,密码正确,放心使用",Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                }else{
                    Toast.makeText(getApplicationContext(),"亲,密码不正确,请重新输入",Toast.LENGTH_LONG).show();
                    pw.setText(null);
                }
            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        builder.setView(view);
        //builder.show();
        dialog = builder.create();
        dialog.show();
    }
MD5加密

package ligang.huse.cn.tools;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * MD5加密工具类
 */
public class MD5Utils {
    public static String passwordMd5(String password) {
        StringBuilder sb = new StringBuilder();
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] bytes = digest.digest(password.getBytes());
            for (int i = 0; i < bytes.length; i++) {
                int result = bytes[i] & 0xff;
                String hexString = Integer.toHexString(result) + 1;
                if (hexString.length() < 2) {
                    sb.append(0);
                }
                sb.append(hexString);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
设置密码的显示与隐藏
  ImageView password_hide= (ImageView) view.findViewById(R.id.password_hide);
        //设置一个小按钮,按下表示显示密码,不按表示隐藏密码
        password_hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(count%2==0){
                    pw.setInputType(0);
                }else{
                    pw.setInputType(129);
                }
                count++;
            }
        });
设置手机防盗界面
 ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pws = pw.getText().toString().trim();
                //判断密码是否一致
                if(MD5Utils.passwordMd5(pws).equals(sp.getString("password", ""))){
                    Toast.makeText(getApplicationContext(),"亲,密码正确,放心使用",Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                    //进入手机防盗界面
                    Intent intent=new Intent(HomeActivity.this,LostFindActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(getApplicationContext(),"亲,密码不正确,请重新输入",Toast.LENGTH_LONG).show();
                    pw.setText(null);
                }
            }
        });
设置手机防盗设置界面
/**
 * 手机防盗功能界面
 */
public class LostFindActivity extends AppCompatActivity {
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getSharedPreferences("config", MODE_PRIVATE);
        if (sp.getBoolean("first", true)) {
            //跳转到手机防盗设置功能界面
            Intent intent = new Intent(this, SetUp1Activity.class);
            startActivity(intent);
            finish();
        } else {
            //跳转到手机防盗界面
            setContentView(R.layout.activity_lost_find);
        }
    }
}
设置手机防盗设置界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="ligang.huse.cn.mobilesafe.SetUp1Activity">
    <TextView
        android:id="@+id/tv_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="#8866ff00"
        android:gravity="center_horizontal"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:text="1.欢迎使用手机防盗"
        android:textSize="25sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="您的手机防盗卫士:"
        android:textSize="22sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:drawableLeft="@android:drawable/star_big_on"
        android:gravity="center_vertical"
        android:text="SIM卡变更报警"
        android:textSize="20sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:drawableLeft="@android:drawable/star_big_on"
        android:gravity="center_vertical"
        android:text="GPS追踪"
        android:textSize="20sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:drawableLeft="@android:drawable/star_big_on"
        android:gravity="center_vertical"
        android:text="远程数据销毁"
        android:textSize="20sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:drawableLeft="@android:drawable/star_big_on"
        android:gravity="center_vertical"
        android:text="远程锁屏"
        android:textSize="20sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_online" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible" />
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/setup1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="3dp"
            android:layout_marginRight="3dp"
            android:text="下一步"
            android:textSize="20sp" />
    </RelativeLayout>
</LinearLayout>

创建状态选择器
在res/drawable新建一个button.xml文件(android studio不用新建drawable文件夹了)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/function_greenbutton_pressed" /> <!-- 按下按钮时显示的图片 -->
    <item android:drawable="@drawable/function_greenbutton_normal" /> <!-- 默认显示的图片-->
</selector>
在控件空使background属性调用drawable下的button文件就ok


设置样式(抽取样式)
<!--在styles文件中抽取的样式-->
    <style name="next">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_alignParentRight">true</item>
        <item name="android:layout_alignParentBottom">true</item>
        <item name="android:layout_marginBottom">3dp</item>
        <item name="android:layout_marginRight">3dp</item>
        <item name="android:text">下一步</item>
        <item name="android:onClick">next</item>
        <item name="android:background">@drawable/button</item>
        <item name="android:textSize">20sp</item>
    </style>
    <style name="pre">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_alignParentLeft">true</item>
        <item name="android:layout_alignParentBottom">true</item>
        <item name="android:layout_marginBottom">3dp</item>
        <item name="android:layout_marginLeft">3dp</item>
        <item name="android:text">上一步</item>
        <item name="android:onClick">pre</item>
        <item name="android:background">@drawable/button</item>
        <item name="android:textSize">20sp</item>
    </style>
调用样式
 <!--抽取了样式-->
        <Button
            style="@style/next"
            />

抽取4个Activity的共有的pre和next方法,将他们抽取到一个基类中,写上两个抽象方法让4个Activity继承该基类,调用他们,再重写onkeyDown方法,
监听调用返回键时调用子类中pre方法(这样会避免按返回键时,直接跳到主界面)

package ligang.huse.cn.mobilesafe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
public abstract  class SetUpBaseActivity extends AppCompatActivity {
    //抽取子类共有的方法,进行抽像化,并让子类继承该基类
    public void next(View view){
        next_activity();
    }
    public void pre(View view){
        per_activity();
    }
    public  abstract void next_activity();
    public abstract  void per_activity();
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //如果是返回键就调用,子类中上一个界面的方法,这样就解决了按返回键直接跳到主界面的
        //KEYCODE_BACK:返回键
        if(keyCode==KeyEvent.KEYCODE_BACK){
            per_activity();
        }
        return super.onKeyDown(keyCode, event);
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值