package pgt_company.project01;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class FormInfo implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
private Activity activity;
private LinearLayout LayoutFormInfo;
private EditText inputName;
private EditText inputPhone;
private EditText inputEmail;
private CheckBox cbSharePhone;
private Button btnSubmit;
public FormInfo (Activity activity , int LayoutId){
this.activity = activity ;
this.LayoutFormInfo = (LinearLayout) activity.findViewById(LayoutId);
this.activity = activity;
init();
}
private void init() {
if (LayoutFormInfo == null){
return;
}
inputName = (EditText) LayoutFormInfo.findViewById(R.id.input_name);
inputEmail = (EditText) LayoutFormInfo.findViewById(R.id.input_email);
inputPhone = (EditText) LayoutFormInfo.findViewById(R.id.input_phone);
cbSharePhone = (CheckBox) LayoutFormInfo.findViewById(R.id.cb_share_phone);
btnSubmit = (Button) LayoutFormInfo.findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(this);
cbSharePhone.setOnCheckedChangeListener(this);
}
///checked
@Override
public void onClick(View v) {
if (v.getId() == btnSubmit.getId()){
String email = inputEmail.getText().toString().trim();
String phone = inputPhone.getText().toString().trim();
String name = inputName.getText().toString().trim();
if (isValidInput(phone,name,email)){
Intent intent = new Intent(activity,SecondActivity.class);
intent.putExtra("name",name);
if (cbSharePhone.isChecked()){
intent.putExtra("phone",phone);
}
intent.putExtra("email",email);
activity.startActivity(intent);
}
}
}
//checked
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == cbSharePhone.getId()){
inputPhone.setEnabled(isChecked);
}
}
private boolean isValidInput(String name , String phone , String email ) {
//name checked
if (name.length() < 3 ){
Toast.makeText(activity, "Please write your real name", Toast.LENGTH_SHORT).show();
inputName.requestFocus();
return false;
}
مشکل //***********************************************************************************************************************
if (!phone.isEmpty() && (phone.length() !=11 || !phone.startsWith("09")) ){
Toast.makeText(activity, "Wrong phone number", Toast.LENGTH_SHORT).show();
inputPhone.requestFocus();
return false;
}
//email checked
if ( email.lastIndexOf('@') <= 0
|| !email.contains(".")
|| email.lastIndexOf('.') < email.lastIndexOf('@')
|| email.split("@").length > 2
){
Toast.makeText(activity, "Wrong email address!", Toast.LENGTH_SHORT).show();
inputEmail.requestFocus();
return false;
}
return true;
}
}