如图当点击按钮时,会先判断是否第一次登陆,如果是第一次登陆登陆则会弹出设置密码的弹窗,若果登陆过则弹出登陆弹窗
其中输入的密码会用MD5加密下
package com.org.demo.wangfeng.demo; import com.org.wangfeng.R; import android.app.Activity; import android.app.AlertDialog; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class HomeActivity extends Activity { private Button bbButton; private SharedPreferences mPref; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.homeactivity); mPref = getSharedPreferences("config", MODE_PRIVATE); bbButton = (Button) findViewById(R.id.bb_home); bbButton.setOnClickListener(new OnclickItem()); } private class OnclickItem implements View.OnClickListener { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String savedPassword = mPref.getString("password", null); if(!TextUtils.isEmpty(savedPassword)){ //输入密码弹窗 showPasswordInputDialog(); }else{ //如果没有设置过,弹出设置密码的弹窗 showPasswordSetDialog(); } } } /** * 设置密码的弹窗 */ private void showPasswordSetDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dialog_set_password, null); dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题 final EditText edPassWord = (EditText) view .findViewById(R.id.et_password); final EditText edPassWordConfirm = (EditText) view .findViewById(R.id.et_password_confirm); Button btnOK = (Button) view.findViewById(R.id.btn_ok); Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); btnOK.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String password = edPassWord.getText().toString(); String passwordConfirm = edPassWordConfirm.getText().toString(); if (!TextUtils.isEmpty(password) && !passwordConfirm.isEmpty()) { // 当输入的2个内容相同 if (password.equals(passwordConfirm)) { Toast.makeText(HomeActivity.this, "登陆成功", Toast.LENGTH_SHORT).show(); // 将密码保存起来 mPref.edit() .putString("password", MD5Utils.encode(password)).commit(); dialog.dismiss(); } else { Toast.makeText(HomeActivity.this, "2此输入密码不一致", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(HomeActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show(); } } }); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub dialog.dismiss(); } }); dialog.show(); } /** * 输入密码弹窗 */ private void showPasswordInputDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dialog_input_password, null); dialog.setView(view, 0, 0, 0, 0); final EditText etPassword = (EditText) view .findViewById(R.id.et_password); Button btnOk = (Button) view.findViewById(R.id.btn_ok); Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String password = etPassword.getText().toString(); if (!TextUtils.isEmpty(password)) { String savedPassword = mPref.getString("password", null); if (MD5Utils.encode(password).equals(savedPassword)) { Toast.makeText(HomeActivity.this, "登陆成功", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } else { Toast.makeText(HomeActivity.this, "密码错误", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(HomeActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show(); } } }); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub dialog.dismiss(); } }); dialog.show(); } }
package com.org.demo.wangfeng.demo; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Utils { /** * md5加密 * * @param password * @return */ public static String encode(String password) { try { MessageDigest instance = MessageDigest.getInstance("MD5");// 获取MD5算法对象 byte[] digest = instance.digest(password.getBytes());// 对字符串加密,返回字节数组 StringBuffer sb = new StringBuffer(); for (byte b : digest) { int i = b & 0xff;// 获取字节的低八位有效值 String hexString = Integer.toHexString(i);// 将整数转为16进制 if (hexString.length() < 2) { hexString = "0" + hexString;// 如果是1位的话,补0 } sb.append(hexString); } return sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); // 没有该算法时,抛出异常, 不会走到这里 } return ""; } }
<?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="#fff" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#66ff6600" android:gravity="center" android:padding="10dp" android:text="设置密码" android:textColor="@android:color/black" android:textSize="20sp" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword" > </EditText> <EditText android:id="@+id/et_password_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请再次输入密码" android:inputType="textPassword" > </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_ok" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定" /> <Button android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout> </LinearLayout>
其实在android里 if(text ==null || text.length()==0)是有封装的。
在android.text.TextUtils里
public static boolean isEmpty(CharSequence str) { if (str == null || str.length() == 0) return true; else return false; } 所以我们可以使用
TextUtils.isEmpty(text)
代替
if(text == null || text.length() == 0)
时间: 2024-10-06 04:39:00