Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 新建/編輯聯系人時,檢測輸入的email/郵件地址格式是否合法

android 新建/編輯聯系人時,檢測輸入的email/郵件地址格式是否合法

編輯:關於Android編程

JB版本:
1.SIM/USIM卡聯系人:可以直接使用EditSimContactActivity中的USIM_EMAIL_PATTERN,搜索與“USIM_EMAIL_PATTERN”,將相關code所加的注釋去掉即可


2.手機本地聯系人:修改代碼如下
ContactEditorFragment.java (packages\apps\contacts\src\com\android\contacts\editor)
A 引入包
import java.util.regex.Pattern;
B 增加變量
private static final String USIM_EMAIL_PATTERN = "0-9][a-z][A-Z][_0-9][a-z][A-Z][-_.*@0-9][a-z][A-Z][-_.+";
C save函數中,
在如下語句:
if (!hasValidState() || mStatus != Status.EDITING) {
Log.i(TAG, "[save] !hasValidState() : " + (!hasValidState())
+ " | mStatus != Status.EDITING : " + (mStatus != Status.EDITING)
+ " , mStatus : " + mStatus);
return false;
}
之後添加:
//email
for (int n = 0; n < mState.size(); n++) {
RawContactDelta field = mState.get(n);
ArrayList data = field.getMimeEntries("vnd.android.cursor.item/email_v2");
if(data != null )
for (int m = 0; m < data.size(); m++) {
String email = data.get(m).getAsString("data1");
if (!TextUtils.isEmpty(email)) {
if (!Pattern.matches(USIM_EMAIL_PATTERN, email)) {

Toast.makeText(mContext, R.string.email_invalid, Toast.LENGTH_SHORT).show();
getActivity().finish();
return false;
}
}
}
}
KK版本
1. SIM/USIM卡聯系人:
SIMEditProcessor.java (alps\packages\apps\contacts\src\com\mediatek\contacts\simservice)
先添加 private boolean mEmailInvalid = false;
然後修改
private boolean isUSIMUpdateValuesInvalid()
//mtk add
if (!TextUtils.isEmpty(mUpdatemail)) {
if (!Pattern.matches(USIM_EMAIL_PATTERN, mUpdatemail)) {
mEmailInvalid = true;
}
}
//mtk add end
LogUtils.i(TAG, "mNumberIsNull:" + mNumberIsNull + ",mNumberInvalid:" +
mNumberInvalid + ",mFixNumberInvalid:" + mFixNumberInvalid); //參考位置

再修改
private boolean isInsertValuesInvalid()
if (!TextUtils.isEmpty(mUpdateAdditionalNumber)) {
if (!Pattern.matches(SIM_NUM_PATTERN, PhoneNumberUtilsEx
.extractCLIRPortion(mUpdateAdditionalNumber))) {
mFixNumberInvalid = true;
}
}//參考位置
//mtk add
if (!TextUtils.isEmpty(mUpdatemail)) {
if (!Pattern.matches(USIM_EMAIL_PATTERN, mUpdatemail)) {
mEmailInvalid = true;
}
}
//mtk add end

最後修改
private boolean setSaveFailToastText()
}*/ else if (mEmailInvalid) {
mSaveFailToastStrId = R.string.cannot_insert_error_format_email; //資源請自己添加
mEmailInvalid = false;
2.手機本地聯系人:修改代碼如下
ContactEditorFragment.java (packages\apps\contacts\src\com\android\contacts\editor)
A 引入包
import java.util.regex.Pattern;
B 增加變量
private static final String USIM_EMAIL_PATTERN = "0-9][a-z][A-Z][_0-9][a-z][A-Z][-_.*@0-9][a-z][A-Z][-_.+";
C save函數中,
在如下語句:
if (!hasValidState() || mStatus != Status.EDITING) {
Log.i(TAG, "[save] !hasValidState() : " + (!hasValidState())
+ " | mStatus != Status.EDITING : " + (mStatus != Status.EDITING)
+ " , mStatus : " + mStatus);
return false;
}
之後添加:
//email
for (int n = 0; n < mState.size(); n++) {
RawContactDelta field = mState.get(n);
ArrayList data = field.getMimeEntries("vnd.android.cursor.item/email_v2");
if(data != null )
for (int m = 0; m < data.size(); m++) {
String email = data.get(m).getAsString("data1");
if (!TextUtils.isEmpty(email)) {
if (!Pattern.matches(USIM_EMAIL_PATTERN, email)) {

Toast.makeText(mContext, R.string.email_invalid, Toast.LENGTH_SHORT).show();
getActivity().finish();
return false;
}
}
}
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved