Android项目实战--手机卫士07--设置向导



最新实战教程,让你了解Android自动化刷量、作弊与防作弊的那些事,案例:刷友盟统计、批量注册苹果帐号




 

今天,我们要做的就是给我们的手机防盗这个功能添加一个设置向导的功能啦,指导用户进行一些设置,废话不多说,直接上图,看看我们要做的效果

   

 

UI不是很好看,各位可以自己修改一下哈

好啦,我们今天主要是把这两个界面写出来,还有把一些逻辑代码写一下

其实这主要就是两个xml文件啦

setup_guide1.xml

<?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:background="@drawable/title_background"
    android:orientation="vertical" >
    
    <TextView 
        style="@style/SetupGuideTitle"
        android:text="@string/guide1"/>
    
    <TextView 
        style="@style/SetGuideUnderline"/>
    
    <TextView 
        style="@style/SetupGuideContent"
        android:text="@string/guide1_content"/>
    
    <TextView 
        style="@style/SetupGuideItem"
        android:text="@string/guide1_item1"/>
    
    <TextView 
        style="@style/SetupGuideItem"
        android:text="@string/guide1_item2"/>
    
    <TextView 
        style="@style/SetupGuideItem"
        android:text="@string/guide1_item3"/>
    
    <TextView 
        style="@style/SetupGuideItem"
        android:text="@string/guide1_item4"/>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dip"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_online"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
    </LinearLayout>
    
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|center_vertical">
        
        <LinearLayout 
            android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:gravity="center_horizontal|center_vertical">
	        
	        <ImageView 
	            android:layout_width="150dip"
	            android:layout_height="150dip"
	            android:src="@drawable/widget01"
	            android:contentDescription="@string/hello_world"/>
        
        </LinearLayout>
        
        <Button 
            android:id="@+id/bt_guide_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next"
            android:drawableRight="@drawable/next"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
        
    </RelativeLayout>
        
</LinearLayout>


 

setup_guide2.xml

<?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:background="@drawable/title_background"
    android:orientation="vertical" >
    
    <TextView 
        style="@style/SetupGuideTitle"
        android:text="@string/guide2"/>
    
    <TextView 
        style="@style/SetGuideUnderline"/>
    
    <TextView 
        style="@style/SetupGuideContent"
        android:text="@string/guide2_content"/>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <Button 
            android:id="@+id/bt_guide_bind"
            android:layout_width="240dip"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dip"
            android:text="@string/guide2_item1"/>
        
        <CheckBox 
            android:id="@+id/cb_guide_check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/guide2_item2"/>
        
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_online"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
    </LinearLayout>
    
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|center_vertical">
        
        <LinearLayout 
            android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:gravity="center_horizontal|center_vertical">
	        
	        <ImageView 
	            android:layout_width="150dip"
	            android:layout_height="150dip"
	            android:src="@drawable/guide2"
	            android:contentDescription="@string/hello_world"/>
        
        </LinearLayout>
        
        <Button 
            android:id="@+id/bt_guide_pervious"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/perviout"
            android:drawableRight="@drawable/previous"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"/>
        
        <Button 
            android:id="@+id/bt_guide_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next"
            android:drawableRight="@drawable/next"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
        
    </RelativeLayout>
        
</LinearLayout>


 

setup_guide3.xml

<?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:background="@drawable/title_background"
    android:orientation="vertical" >
    
    <TextView 
        style="@style/SetupGuideTitle"
        android:text="@string/guide1"/>
    
    <TextView 
        style="@style/SetGuideUnderline"/>
    
    <TextView 
        style="@style/SetupGuideContent"
        android:text="@string/guide1_content"/>
    
    <TextView 
        style="@style/SetupGuideItem"
        android:text="@string/guide1_item1"/>
    
    <TextView 
        style="@style/SetupGuideItem"
        android:text="@string/guide1_item2"/>
    
    <TextView 
        style="@style/SetupGuideItem"
        android:text="@string/guide1_item3"/>
    
    <TextView 
        style="@style/SetupGuideItem"
        android:text="@string/guide1_item4"/>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dip"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_online"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
    </LinearLayout>
    
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|center_vertical">
        
        <LinearLayout 
            android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:gravity="center_horizontal|center_vertical">
	        
	        <ImageView 
	            android:layout_width="150dip"
	            android:layout_height="150dip"
	            android:src="@drawable/widget01"
	            android:contentDescription="@string/hello_world"/>
        
        </LinearLayout>
        
        <Button 
            android:id="@+id/bt_guide_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next"
            android:drawableRight="@drawable/next"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
        
    </RelativeLayout>
        
</LinearLayout>


 

setup_guide4.xml

<?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:background="@drawable/title_background"
    android:orientation="vertical" >
    
    <TextView 
        style="@style/SetupGuideTitle"
        android:text="@string/guide4"/>
    
    <TextView 
        style="@style/SetGuideUnderline"/>
    
    <TextView 
        style="@style/SetupGuideContent"
        android:text="@string/guide4_content"/>
    
    <CheckBox 
        android:id="@+id/cb_guide_protected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="@string/guide4_item1"/>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dip"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_invisible"
            android:contentDescription="@string/hello_world"/>
        
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/presence_online"
            android:contentDescription="@string/hello_world"/>
        
    </LinearLayout>
    
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|center_vertical">
        
        <LinearLayout 
            android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:gravity="center_horizontal|center_vertical">
	        
	        <ImageView 
	            android:layout_width="150dip"
	            android:layout_height="150dip"
	            android:src="@drawable/guide4"
	            android:contentDescription="@string/hello_world"/>
        
        </LinearLayout>
        
        <Button 
            android:id="@+id/bt_guide_pervious"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/perviout"
            android:drawableRight="@drawable/previous"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"/>
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/finish"
            android:drawableRight="@drawable/next"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>
        
    </RelativeLayout>
        
</LinearLayout>


 

上面四个xml文件,就是我们要做的向导的界面啦,其中都用到了样式的定义,各位如果有什么不明白的,可以回头再看一下我们之前写的,毕竟是一步接一步的,肯定要先把前面的弄清楚的,因为后面还会用到那些知识点的。

我们今天主要是把前两个页面的逻辑加上,后面两个页面的逻辑在明天再加,因为第三个页面,要涉及到读取用户的联系人这一非常常用的操作,所以单独来讲一下

我们的设置向导是在用户第一次打开我们的手机防盗这个功能时才启动的,所以我们还要在手机防盗这个功能的界面里面加一些逻辑,好,废话不多说, 直接上代码

com.xiaobin.security.ui.LostProtectedActivity

package com.xiaobin.security.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
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;
	
	@Override
	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.setCancelable(false);
		dialog.show();
	}

	private void showFirstDialog()
	{
		dialog = new Dialog(this, R.style.MyDialog);
		//dialog.setContentView(R.layout.first_dialog);
		View view = View.inflate(this, R.layout.first_dialog, null);//这种填充布局的方式比较方便,峭用拿到一个LayoutInflate对象
		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);
		yes.setOnClickListener(this);
		cancel.setOnClickListener(this);
		dialog.setContentView(view);
		dialog.setCancelable(false);
		dialog.show();
	}

	private boolean isSetPassword()
	{
		String pwd = sp.getString("password", "");
		if(pwd.equals("") || pwd == null)
		{
			return false;
		}
		return true;
	}
	
	private boolean isSetupGuide()
	{
		return sp.getBoolean("setupGuide", false);
	}

	@Override
	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;
				}
				else 
				{
					if(fp.equals(cp))
					{
						Editor editor = sp.edit();
						editor.putString("password", MD5Encoder.encode(fp));
						editor.commit();
						dialog.dismiss();
						
						if(!isSetupGuide())
						{
							finish();
							Intent intent = new Intent(this, SetupGuide1Activity.class);
							startActivity(intent);
						}
					}
					else
					{
						Toast.makeText(this, "两次密码不相同", Toast.LENGTH_SHORT).show();
						return;
					}
				}
				dialog.dismiss();
				break;
				
			case R.id.bt_protected_first_no : 
				dialog.dismiss();
				finish();
				break;
				
			case R.id.bt_protected_login_yes : 
				String pwd = password.getText().toString().toString();
				if(pwd.equals(""))
				{
					Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
				}
				else
				{
					String str = sp.getString("password", "");
					if(MD5Encoder.encode(pwd).equals(str))
					{
						if(!isSetupGuide())
						{
							finish();
							Intent intent = new Intent(this, SetupGuide1Activity.class);
							startActivity(intent);
						}
						dialog.dismiss();
					}
					else
					{
						Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
					}
				}
				break;
				
			case R.id.bt_protected_login_no : 
				dialog.dismiss();
				finish();
				break;
				
			default : 
				break;
		}
	}

}


com.xiaobin.security.ui.SetupGuide1

package com.xiaobin.security.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.xiaobin.security.R;

public class SetupGuide1Activity extends Activity implements OnClickListener
{
	private Button next;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup_guide1);
		
		next = (Button) findViewById(R.id.bt_guide_next);
		next.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
			case R.id.bt_guide_next : 
				Intent intent = new Intent(this, SetupGuide2Activity.class);
				finish();
				startActivity(intent);
				//这个是定义activity切换时的动画效果的
				overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
				break;
				
			default : 
				break;
		}
	}

}


大家可以看到,我们在上面切换activity时,加了一些效果,其实就是通过overridePendingTransition这个方法来实现的,所以大家以后想让自己的activity之间切换,多一些动态的效果,就可以用这个方法啦。

上面只是我自己定义的一个简单的动画文件,要定义一个动画文件也很简单,只要在res下面新建一个anim目录,然后在里面新建一些动画的xml就行啦

下面是我们上面的那两个xml动画

alpha_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="300" >
    

</alpha>


 

alpha_out.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="300" >
    

</alpha>


有一个地方要注意的,我们新建动画的xml时,一定要把那个命名空间http://schemas.android.com/apk/res/android指定好,不然就不行的

 

好啦,现在我们把第二个guide的逻辑代码也写一下

com.xiaobin.security.ui.SetupGuide2

package com.xiaobin.security.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.xiaobin.security.R;

public class SetupGuide2Activity extends Activity implements OnClickListener
{
	private Button bt_bind;
	private Button bt_next;
	private Button bt_perviout;
	private CheckBox cb_bind;
	private SharedPreferences sp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup_guide2);
		
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		
		bt_bind = (Button) findViewById(R.id.bt_guide_bind);
		bt_next = (Button) findViewById(R.id.bt_guide_next);
		bt_perviout = (Button) findViewById(R.id.bt_guide_pervious);
		bt_bind.setOnClickListener(this);
		bt_next.setOnClickListener(this);
		bt_perviout.setOnClickListener(this);
		
		cb_bind = (CheckBox) findViewById(R.id.cb_guide_check);
		//初始化CheckBox状态
		String sim = sp.getString("simSerial", null);
		if(sim != null)
		{
			cb_bind.setText("已经绑定");
			cb_bind.setChecked(true);
		}
		else
		{
			cb_bind.setText("没有绑定");
			cb_bind.setChecked(false);
		}
		cb_bind.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				//那个解除绑定的我还没做的呢,各位可以自己去完成,就是把那个SharedPreferences里面的值设置一下就行啦
				if(isChecked)
				{
					cb_bind.setText("已经绑定");
					setSimInfo();
				}
				else
				{
					cb_bind.setText("没有绑定");
				}
			}
		});
	}

	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
			case R.id.bt_guide_bind : 
				setSimInfo();
				cb_bind.setText("已经绑定");
				cb_bind.setChecked(true);
				break;
				
			case R.id.bt_guide_next : 
				Intent intent = new Intent(this, SetupGuide3Activity.class);
				finish();
				startActivity(intent);
				//这个是定义activity切换时的动画效果的
				overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
				break;
			case R.id.bt_guide_pervious : 
				
				Intent i = new Intent(this, SetupGuide1Activity.class);
				finish();
				startActivity(i);
				//这个是定义activity切换时的动画效果的
				overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
				break;
				
			default : 
				break;
		}
	}
	
	private void setSimInfo()
	{
		TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		String simSerial = telephonyManager.getSimSerialNumber();//拿到sim卡的序列号,是唯一的
		Editor editor = sp.edit();
		editor.putString("simSerial", simSerial);
		editor.commit();
	}

}


 

好,现在只要去AndroidManifest文件里面注册一下这两个activity就可以看看我们的设置向导啦

 

今天的代码就到这里了,有什么不明白的可以留言,或看一下我们之前的写的代码,明天我们将会讲解系统联系人的获取

 

 今天源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值