Back to project page ExpertAndroid.
The source code is released under:
MIT License
If you think the Android project ExpertAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.iuriio.demos.expertandroid.ch6forms; //www .ja va2s . c o m import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends FormActivity implements View.OnClickListener { private static final String TAG = "MainActivity"; private EditText userid; private EditText password1; private EditText password2; private EditText email; public MainActivity() { super(TAG); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button signUp = (Button) this.findViewById(R.id.signup); signUp.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. switch (item.getItemId()) { case R.id.action_settings: return true; } return super.onOptionsItemSelected(item); } @Override protected void initializeFormFields() { this.reportBack("Form initialized"); this.userid = (EditText) this.findViewById(R.id.userid); this.password1 = (EditText) this.findViewById(R.id.password1); this.password2 = (EditText) this.findViewById(R.id.password2); this.email = (EditText) this.findViewById(R.id.email); this.addValidator(new Field(this.userid)); this.addValidator(new Field(this.password1)); this.addValidator(new Field(this.password2)); this.addValidator(new Field(this.email)); this.addValidator(new PasswordFieldRule(this.password1, this.password2)); } /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.signup: if (!this.validateForm()) { this.reportTransient("Make sure all fields have valid values"); return; } final String userid = this.getUserId(); final String password = this.getPassword1(); final String email = this.getUserEmail(); this.signup(userid, email, password); break; } } private void signup(String userid, String email, String password) { this.gotoActivity(WelcomeActivity.class); } private String getUserId() { return this.getStringValue(R.id.userid); } private String getUserEmail() { return this.getStringValue(R.id.email); } private String getPassword1() { return this.getStringValue(R.id.password1); } private String getStringValue(int controlId) { final TextView textView = (TextView) this.findViewById(controlId); if (textView == null) { throw new RuntimeException("Sorry cant find control by id"); } return textView.getText().toString(); } }