【发布时间】:2015-03-26 13:24:30
【问题描述】:
在注册活动中,我尝试了几个验证edittext的代码,但不幸的是我的应用程序关闭了。
public class RegisterActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private EditText eusername, eemail, emobile, epassword;
private Button btnSubmit;
Context context; Matcher matcher;
// Progress Dialog
private ProgressDialog pDialog;
private String username, email, mobile, password;
TextView textViewPasswordStrengthIndiactor;
// JSON parser class
JSONParser jsonParser = new JSONParser();
private static final String REGISTER_URL = "MY_URL";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
eusername = (EditText) findViewById(R.id.reg_name);
eemail = (EditText) findViewById(R.id.reg_email);
emobile = (EditText) findViewById(R.id.reg_phone);
epassword = (EditText) findViewById(R.id.reg_password);
btnSubmit = (Button) findViewById(R.id.Submit);
btnSubmit.setOnClickListener(this);
//chb = (CheckBox) findViewById(R.id.cbShowPwd);
//chb.setOnClickListener(this);
eusername.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
}
});
eemail.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
}
});
emobile.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//textView.setText("Not Entered");
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
}
});
epassword.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
}
});}
@Override
public void onClick(View v) {
if (username.length() == 0) {
Toast.makeText(context, "Please enter UserName", Toast.LENGTH_SHORT).show();
}
//else if (!matcher.matches()) {
//Toast.makeText(context, "Please enter valid UserName",Toast.LENGTH_SHORT).show();
//}
else if (mobile.length() < 10) {
Toast.makeText(context, "Please enter 10 digit Mobile Number",Toast.LENGTH_SHORT).show();
}
else if (email.length() == 0) {
Toast.makeText(context, "Please enter Email ID",Toast.LENGTH_SHORT).show();
}
else if (!matcher.matches()) {
Toast.makeText(context, "Please enter valid Email ID",Toast.LENGTH_SHORT).show();
}
else if (password.length() == 0) {
Toast.makeText(context, "Please retry another password", Toast.LENGTH_SHORT).show();
}
else{
new CreateUser().execute();
Toast.makeText(RegisterActivity.this, "User Registered", Toast.LENGTH_LONG).show();
}
}
class CreateUser extends AsyncTask<String, String, String> {
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegisterActivity.this);
pDialog.setMessage("Creating User...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@SuppressWarnings("deprecation")
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = eusername.getText().toString();
String password = epassword.getText().toString();
String email = eemail.getText().toString();
String mobile = emobile.getText().toString();
try {
// Building Parameters
@SuppressWarnings("deprecation")
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("mobile", mobile));
Log.d("request!", "starting");
// Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(REGISTER_URL,
"POST", params);
// full json response
Log.d("Register attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("User Created!", json.toString());
finish();
return json.getString(TAG_MESSAGE);
} else {
Log.d("Register Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(RegisterActivity.this, file_url,
Toast.LENGTH_LONG).show();
}
}
}
}
我也尝试使用 if-elseif-if 循环方法和模式匹配器,但没有得到想要的结果。 我需要指导的任务- (i) 我需要验证电子邮件 ID 模式、密码长度、电话号码模式。 (ii)单击提交按钮,检查是否所有字段都已填写,然后执行异步任务,否则将吐司返回到相应的空字段。 提前致谢
【问题讨论】:
-
你能添加堆栈跟踪吗?
-
打开 logcat 并复制日志
-
验证取决于您自己的逻辑和其他关于电子邮件 ID 的信息,如果您搜索,则可以使用各种功能
-
@Hulk 我已经使用了方法,但它意外关闭了我的活动。
-
好的,在 logcat 中检查这个崩溃。发送 logcat 详细信息
标签: android validation pattern-matching textwatcher