Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Thursday, 18 October 2012

Create login application where you will have to validate EmailID(UserName). Till the username and password is not validated , login button should remain disabled.

PRO3Activity.java
package vc.pro3;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PRO3Activity extends Activity implements OnClickListener, TextWatcher {
    /**
     *  www.master-gtu.blogspot.com
     *  pankaj sharma(8460479175),
     *  chavda vijay(8460420769) 
     */
 Button btnLogin,btnCancel;
 EditText txtuname,txtpass;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnLogin=(Button) findViewById(R.id.btnLogin);
        btnCancel=(Button) findViewById(R.id.btnCancel);
        txtuname=(EditText) findViewById(R.id.txtuname);
        txtpass=(EditText)findViewById(R.id.txtpass);
        
        btnLogin.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        btnLogin.setEnabled(false);
        txtuname.addTextChangedListener(this);
        txtpass.addTextChangedListener(this);
    }
    
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if(v.getId()==btnLogin.getId())
  {
   if(txtuname.getText().toString().equals(""))
   {
    Toast.makeText(this,"Please Enter Use Name", Toast.LENGTH_SHORT).show();
   }
   else if(txtpass.getText().toString().equals(""))
   {
    Toast.makeText(this,"Please Enter Pass", Toast.LENGTH_SHORT).show();
   }
   else 
   {
     Toast.makeText(this,"Login Success Fully", Toast.LENGTH_SHORT).show();
   }
   
  }
  else if(v.getId() == btnCancel.getId())
  {
   if(txtuname.getText().toString().equals("") && txtpass.getText().toString().equals(""))
   {
    Toast.makeText(this,"Text is Already Empty...",Toast.LENGTH_SHORT).show();
   }
   else
   {
    
    txtuname.setText("");
    txtpass.setText("");
   }
  }
 }
 @Override
 public void afterTextChanged(Editable v) {
  // TODO Auto-generated method stub
  int firstat_rat,lastat_rat,first_dot,last_dot;
  boolean flag_email=true,flag_pass=true,flag=false;
  String email,pass;
   email=txtuname.getText().toString();
   pass=txtpass.getText().toString();
       
  firstat_rat=email.indexOf("@");
  lastat_rat=email.lastIndexOf("@");
  first_dot=email.indexOf(".");
  last_dot=email.lastIndexOf(".");
     
  if(firstat_rat<=0)
  {
   flag_email=false;
  }
  else if(firstat_rat!=lastat_rat )
  {
   flag_email=false;
  }
  else if(lastat_rat==email.length()-1)
  {
   flag_email=false;
  }
  else if(first_dot<= 0)
  {
   flag_email=false;
  }
  else if('.'==email.charAt(lastat_rat-1) ||'.'==email.charAt(lastat_rat+1))
  {
   flag_email=false;
  }
  else if(last_dot==email.length()-1)
  {
   flag_email=false;
  }
  else if(last_dot<lastat_rat)
  {
   flag_email=false;
  }
  else if(first_dot!=last_dot)
  {
   for(int i=first_dot; i<=last_dot; i++)
   {
    if(email.charAt(i)=='.')
    {
     if(flag==true)
     {
      flag_email=false;
      break;
     }
     else
      flag=true;
    }
    else
    {
     flag=false;
    }
   }
  }
  else
  {
   flag_email=true;
  }
  
  if(pass.equals(""))
  {
   flag_pass=false;
  }
  else
  {
   flag_pass=true;
  }
  if(flag_email==true && flag_pass==true )
    btnLogin.setEnabled(true);
  else 
   btnLogin.setEnabled(false);
 }

 @Override
 public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
   int arg3) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
  // TODO Auto-generated method stub
  
 }
}

No comments:

Post a Comment