2016-09-27 152 views
-4

我目前正在開展一項有Web應用和Android應用的服務項目。目前,Web團隊沒有提供Web服務,我需要對錶單進行登錄驗證,並使用戶能夠註冊。我已經嘗試過Realm,但我並不太瞭解它。 有什麼幫助嗎?進行登錄和註冊工作

+0

你在登錄表單有什麼?你有什麼試過? –

+0

嘗試使用webservice ..its更新更穩定和易於use.follow給定鏈接的更多信息[http://square.github.io/retrofit/] –

+0

嗯,Realm **是**數據庫,和不是驗證庫,也不是REST API實現:P – EpicPandaForce

回答

0

你可以按照這個

http://sourcey.com/beautiful-android-login-and-signup-screens-with-material-design/

http://www.androidhive.info/2011/10/android-login-and-registration-screen-design/

public class LoginActivity extends AppCompatActivity { 
    private static final String TAG = "LoginActivity"; 
    private static final int REQUEST_SIGNUP = 0; 

    @InjectView(R.id.input_email) EditText _emailText; 
    @InjectView(R.id.input_password) EditText _passwordText; 
    @InjectView(R.id.btn_login) Button _loginButton; 
    @InjectView(R.id.link_signup) TextView _signupLink; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     ButterKnife.inject(this); 

     _loginButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       login(); 
      } 
     }); 

     _signupLink.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // Start the Signup activity 
       Intent intent = new Intent(getApplicationContext(), SignupActivity.class); 
       startActivityForResult(intent, REQUEST_SIGNUP); 
      } 
     }); 
    } 

    public void login() { 
     Log.d(TAG, "Login"); 

     if (!validate()) { 
      onLoginFailed(); 
      return; 
     } 

     _loginButton.setEnabled(false); 

     final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this, 
       R.style.AppTheme_Dark_Dialog); 
     progressDialog.setIndeterminate(true); 
     progressDialog.setMessage("Authenticating..."); 
     progressDialog.show(); 

     String email = _emailText.getText().toString(); 
     String password = _passwordText.getText().toString(); 

     // TODO: Implement your own authentication logic here. 

     new android.os.Handler().postDelayed(
       new Runnable() { 
        public void run() { 
         // On complete call either onLoginSuccess or onLoginFailed 
         onLoginSuccess(); 
         // onLoginFailed(); 
         progressDialog.dismiss(); 
        } 
       }, 3000); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_SIGNUP) { 
      if (resultCode == RESULT_OK) { 

       // TODO: Implement successful signup logic here 
       // By default we just finish the Activity and log them in automatically 
       this.finish(); 
      } 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     // disable going back to the MainActivity 
     moveTaskToBack(true); 
    } 

    public void onLoginSuccess() { 
     _loginButton.setEnabled(true); 
     finish(); 
    } 

    public void onLoginFailed() { 
     Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show(); 

     _loginButton.setEnabled(true); 
    } 

    public boolean validate() { 
     boolean valid = true; 

     String email = _emailText.getText().toString(); 
     String password = _passwordText.getText().toString(); 

     if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) { 
      _emailText.setError("enter a valid email address"); 
      valid = false; 
     } else { 
      _emailText.setError(null); 
     } 

     if (password.isEmpty() || password.length() < 4 || password.length() > 10) { 
      _passwordText.setError("between 4 and 10 alphanumeric characters"); 
      valid = false; 
     } else { 
      _passwordText.setError(null); 
     } 

     return valid; 
    } 
} 
0

如果您正在尋找登錄頁面的用戶憑據驗證,下面的代碼片段可以幫助你;

// validating email id 
private boolean isValidEmail(String email) { 
    String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" 
      + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 

    Pattern pattern = Pattern.compile(EMAIL_PATTERN); 
    Matcher matcher = pattern.matcher(email); 
    return matcher.matches(); 
} 

// validating password with retype password 
private boolean isValidPassword(String pass) { 
    if (pass != null && pass.length() > 6) { 
     return true; 
    } 
    return false; 
}