1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
import java.util.regex.Pattern;
public class FormVialation {
public static boolean isMobile(String phoneNum) { if (phoneNum == null) return false;
return validation("^[1][3,4,5,7,8][0-9]{9}$", phoneNum.replace("+86", "")); }
public static boolean isUserName(String username) {
return validation("^[a-z0-9_-]{3,15}$", username); }
public static boolean isPassword(String pwd) {
return validation("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$", pwd); }
public static boolean isEmail(String mail) { return validation( "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$", mail); }
public static boolean validation(String pattern, String str) { if (str == null) return false; return Pattern.compile(pattern).matcher(str).matches(); }
}
```
### 连续按两次退出键 退出效果 ```Java public class MainActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } private boolean isFirst = true; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { if(msg.what==1) { isFirst = true; } } }; @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if(event.getKeyCode()==KeyEvent.KEYCODE_BACK) { if(isFirst) { isFirst = false; Toast.makeText(MainActivity.this, "再按一次退出应用", 0).show(); handler.sendEmptyMessageDelayed(1, 2000); return true; } } return super.onKeyUp(keyCode, event); } } private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { exit = false; } }; private boolean exit = false; @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK) { if(!exit) { MSUtils.showMsg(this, "再按一次退出应用!"); exit = true; handler.sendEmptyMessageDelayed(1, 2000); return true; } } return super.onKeyUp(keyCode, event); }
原理: 第一次点击, 首先判断exit 的值,如果是false的话返回true 同时提示,载2秒后发送延迟消息,将 exit置false 默认点back就退出,如果 载在两秒内点击
|