首页 > 代码库 > android 新建/编辑联系人时,检测输入的email/邮件地址格式是否合法
android 新建/编辑联系人时,检测输入的email/邮件地址格式是否合法
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<ValuesDelta> data = http://www.mamicode.com/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<ValuesDelta> data = http://www.mamicode.com/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.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;
}
之后添加:
for (int n = 0; n < mState.size(); n++) {
RawContactDelta field = mState.get(n);
ArrayList<ValuesDelta> data = http://www.mamicode.com/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;
}
之后添加:
for (int n = 0; n < mState.size(); n++) {
RawContactDelta field = mState.get(n);
ArrayList<ValuesDelta> data = http://www.mamicode.com/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;
}
}
}
}
android 新建/编辑联系人时,检测输入的email/邮件地址格式是否合法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。