今天,我们要做的就是给我们的手机防盗这个功能添加一个设置向导的功能啦,指导用户进行一些设置,废话不多说,直接上图,看看我们要做的效果
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就可以看看我们的设置向导啦
今天的代码就到这里了,有什么不明白的可以留言,或看一下我们之前的写的代码,明天我们将会讲解系统联系人的获取