在注册活动中,我尝试了几个代码来验证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循环方法也是goggled以及模式匹配器,但没有得到理想的结果。 我需要指导的任务 - (i)我需要验证电子邮件ID模式,密码长度,电话号码模式。 (ii)onclick提交按钮,检查是否所有字段都已填满,然后执行asynctask else to return返回相应的空字段。 提前谢谢
答案 0 :(得分:1)
全球宣布。
String username,password,email,mobile;
@Override
public void onClick(View v) {
username = eusername.getText().toString();
password = epassword.getText().toString();
email = eemail.getText().toString();
mobile = emobile.getText().toString();
if (TextUtils.isEmpty(username)) {
eusername.setError("Please Enter username");//using toast is your wish.
eusername.requestFocus();
} else if (TextUtils.isEmpty(password)) {
epassword.setError("Please Enter password");
epassword.requestFocus();
} else if (TextUtils.isEmpty(email)) {
eemail.setError("Please Enter email");
eemail.requestFocus();
}
else{
new CreateUser().execute();
}
}