上回主要做了設置向導界面的界面設計,主要涉及到界面的布局和一些控件的使用。這次要做設置向導界面的功能具體實現。
首先,4個界面分別是(重復度很大,這里就不再貼到正文中了)
1.
[/mobilesafe/res/layout/setup_wizard1.xml](https://github.com/hoxis/mobilesafe/blob/master/res/layout/setup_wizard1.xml)
1.
[/mobilesafe/res/layout/setup_wizard2.xml](https://github.com/hoxis/mobilesafe/blob/master/res/layout/setup_wizard2.xml)
1.
[/mobilesafe/res/layout/setup_wizard3.xml](https://github.com/hoxis/mobilesafe/blob/master/res/layout/setup_wizard3.xml)
1.
[/mobilesafe/res/layout/setup_wizard4.xml](https://github.com/hoxis/mobilesafe/blob/master/res/layout/setup_wizard4.xml)
### Activity之間的切換動畫效果
- public void **overridePendingTransition**(int enterAnim, int exitAnim)
> 兩個參數:
> - **enterAnim**:進入新Activity的動畫效果
> - **exitAnim**:退出當前Activity的動畫效果
### 創建動畫效果:
- /mobilesafe/res/anim/alpha_in.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromAlpha="0.0"
android:toAlpha="1.0" >
</alpha>
~~~
- [/mobilesafe/res/anim/alpha_out.xml](https://github.com/hoxis/mobilesafe/blob/master/res/anim/alpha_out.xml)
### 為“下一步”按鈕添加點擊事件:
- [/mobilesafe/src/com/liuhao/mobilesafe/ui/SetupWizard1Activity.java](https://github.com/hoxis/mobilesafe/blob/master/src/com/liuhao/mobilesafe/ui/SetupWizard1Activity.java)
~~~
package com.liuhao.mobilesafe.ui;
import com.liuhao.mobilesafe.R;
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;
public class SetupWizard1Activity extends Activity implements OnClickListener {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_wizard1);
button = (Button) this.findViewById(R.id.bt_next);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_next:
finish();// 用戶點擊“后退”時不會再看到這個界面
Intent intent = new Intent(this, SetupWizard2Activity.class);
startActivity(intent);
// 設置Activity切換時的動畫效果
overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
break;
}
}
}
~~~
### 設置界向導面2
需求功能: 1. 綁定SIM卡 2. CheckBox狀態的處理 3. 上一步、下一步 點擊功能的實現
- **綁定SIM卡**
在用戶點擊“綁定SIM卡”時,觸發相應的處理邏輯,獲取當前SIM卡的串號,并將串號存取到SharePreference中。
要獲取手機SIM卡串號,需要添加權限:**android.permission.READ_PHONE_STATE**
~~~
/**
* 綁定SIM串號
*
*/
private void setSimInfo() {
TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String simSerialNumber = manager.getSimSerialNumber();
Editor edit = sp.edit();
edit.putString("sim", simSerialNumber);
edit.commit();
Toast.makeText(getApplicationContext(), "SIM卡已綁定", Toast.LENGTH_SHORT).show();
}
~~~
- **CheckBox狀態的處理**
~~~
// 首先初始化chexkbox的狀態
String sim = sp.getString("sim", null);
if(sim != null){
cb_bind.setText("已綁定SIM卡");
cb_bind.setChecked(true);
}else{
cb_bind.setText("未綁定SIM卡");
cb_bind.setChecked(false);
}
cb_bind.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
setSimInfo();
cb_bind.setText("已綁定SIM卡");
}else{
cb_bind.setText("未綁定SIM卡");
}
}
});
~~~
### 異常處理
弄好,運行代碼,綁定手機SIM卡串號,沒有問題。
再次打開,進入向導2界面時,出錯,程序崩潰。
### 錯誤日志(摘取了主要部分)
E/AndroidRuntime(26463): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liuhao.mobilesafe/com.liuhao.mobilesafe.ui.SetupWizard2Activity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
...
E/AndroidRuntime(26463): at com.liuhao.mobilesafe.ui.SetupWizard2Activity.onCreate(SetupWizard2Activity.java:42)
### 原因:
由于之前判斷SharePreference中是否存在SIM信息是根據下面的邏輯:
~~~
// 首先初始化chexkbox的狀態
if(sp.getBoolean("sim", false)){
cb_bind.setText("已綁定SIM卡");
cb_bind.setChecked(true);
}else{
cb_bind.setText("未綁定SIM卡");
cb_bind.setChecked(false);
}
~~~
而**boolean android.content.SharedPreferences.getBoolean(String key, boolean defValue)**方法,
> Retrieve a boolean value from the preferences.
> Parameters: key The name of the preference to retrieve. defValue Value to return if this preference does not exist. Returns: Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a boolean. Throws: ClassCastException
可以發現,若已存在值,而這個值不是Boolean類型時將會拋出ClassCastException。
### 修改
~~~
// 首先初始化chexkbox的狀態
String sim = sp.getString("sim", null);
if(sim != null){
cb_bind.setText("已綁定SIM卡");
cb_bind.setChecked(true);
}else{
cb_bind.setText("未綁定SIM卡");
cb_bind.setChecked(false);
}
~~~
### 運行效果


#### 完整代碼:
- [/mobilesafe/src/com/liuhao/mobilesafe/ui/SetupWizard2Activity.java](https://github.com/hoxis/mobilesafe/blob/master/src/com/liuhao/mobilesafe/ui/SetupWizard2Activity.java)
- 前言
- Appcompat_V7問題
- This Android SDK requires Android Developer Toolkit version 23.0.0 or above
- 創建Android項目不自動生成Activity,layout目錄為空
- 新建android項目gen目錄下未生成R文件
- 手機安全衛士02:splash界面ui
- 知識點:Android控件系列之Toast
- 手機安全衛士03:獲取更新的服務器配置,顯示更新對話框
- 異常處理:android.os.NetworkOnMainThreadException--多線程問題
- 知識點:Android控件系列之對話框AlertDialog.Builder
- 手機安全衛士04_01:界面(Activity)之間的切換,Activity和任務棧
- 知識點:Android控件系列之ProgressDialog與ProgressBar
- 手機安全衛士04_02:從服務器下載并安裝新版本安裝包
- 知識點:Intent
- 知識點:Adapter適配器
- 手機安全衛士05_1:程序主界面
- 手機安全衛士05_2:程序主界面,為每個條目添加事件
- 知識點:動態設置布局LayoutInflater
- 知識點:SharedPreferences
- 手機安全衛士06-手機防盜之自定義對話框
- 手機安全衛士07-手機防盜之進入限制
- 手機安全衛士08-一些布局和顯示的細節:State List
- 手機安全衛士09-手機防盜界面設置向導1
- 手機安全衛士10-設置向導之綁定SIM卡