ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
上回主要做了设置向导界面的界面设计,主要涉及到界面的布局和一些控件的使用。这次要做设置向导界面的功能具体实现。 首先,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); } ~~~ ### 运行效果 ![](image/d41d8cd98f00b204e9800998ecf8427e.png) ![](image/d41d8cd98f00b204e9800998ecf8427e.png) #### 完整代码: - [/mobilesafe/src/com/liuhao/mobilesafe/ui/SetupWizard2Activity.java](https://github.com/hoxis/mobilesafe/blob/master/src/com/liuhao/mobilesafe/ui/SetupWizard2Activity.java)