2016-08-12 91 views
-1

我要開始審定後下一個活動,我想成功保存在sharedpreferences的信息,當我點擊提交按鈕驗證和保存數據SharedPreference

public class Main2Activity extends AppCompatActivity implements SearchView.OnQueryTextListener { 
EditText fullname, contact, emailaddress, address, password, repassword, username; 
RadioGroup radiogroup; 
Button submits,clear; 
Toolbar toolbar; 
SearchView mSearchView; 
SharedPreferences pref; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.register); 
    pref = getSharedPreferences("myfile", Context.MODE_PRIVATE); 
    fullname = (EditText) findViewById(R.id.edittextfullname); 
    contact = (EditText) findViewById(R.id.edittextcontact); 
    emailaddress = (EditText) findViewById(R.id.edittextemailaddress); 
    address = (EditText) findViewById(R.id.edittextaddress); 
    password = (EditText) findViewById(R.id.edittextpassword); 
    repassword = (EditText) findViewById(R.id.edittextrepassword); 
    username = (EditText) findViewById(R.id.edittextusername); 
    radiogroup = (RadioGroup) findViewById(R.id.radiogroup); 
    submits = (Button) findViewById(R.id.submit); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle(R.string.Title); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 


    submits.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
       final String name = fullname.getText().toString(); 
       if (!isValidFullname(name)) { 
        fullname.setError("Invalid Name"); 
       } 

       final String number = contact.getText().toString(); 
       if (!isValidContact(number)) { 
        contact.setError("Invalid Contact"); 
       } 

       final String email = emailaddress.getText().toString(); 
       if (!isValidEmailaddress(email)) { 
        emailaddress.setError("Invalid Email Address"); 
       } 

       final String addres = address.getText().toString(); 
       if (!isValidAddress(addres)) { 
        address.setError("Invalid address"); 
       } 
       final String pass = password.getText().toString(); 
       final String rpassword = repassword.getText().toString(); 

       if (pass.equals(rpassword) && rpassword.equals(pass)) { 
       } else { 
        password.setError("invalid"); 
        repassword.setError("invalid"); 
       } 

       final String uname = username.getText().toString(); 
       if (!isValidUsername(uname)) { 
        username.setError("Invalid Username"); 
       } 
      else { 
        Intent startNewActivity = new Intent(Main2Activity.this, DisplayActivity.class); 
        startActivity(startNewActivity); 

       } 


      SharedPreferences.Editor editor = pref.edit(); 
      editor.putString("fullname",name); 
      editor.putString("contact",number); 
      editor.putString("emailaddress",email); 
      editor.putString("address",addres); 
      editor.putString("password",pass); 
      editor.putString("repassword",rpassword); 
      editor.putString("username",uname); 
      editor.apply(); 
      Toast.makeText(Main2Activity.this, "Save succesful", Toast.LENGTH_LONG).show(); 

     } 

    }); 
    private boolean isValidContact(String number) { 
    if (number != null && number.length() == 10) { 
     return true; 
    } 
    return false; 
} 

private boolean isValidEmailaddress(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(); 
} 

private boolean isValidAddress(String addres) { 
    if (addres != null) { 
     return true; 
    } 
    return false; 
} 

private boolean isValidPassword(String pass) { 
    if (pass.equals(repassword) && repassword.equals(pass)) { 
     return true; 
    } else { 
     return false; 
    } 
} 

private boolean isValidUsername(String name) { 
    if (name != null && name.length() > 5) { 
     return true; 
    } 
    return false; 
} 
+0

你忘了添加你的問題如何去問問http://stackoverflow.com/help/how-to-ask –

回答

0

你應該只把

SharedPreferences.Editor editor = pref.edit(); 
     editor.putString("fullname",name); 
     editor.putString("contact",number); 
     editor.putString("emailaddress",email); 
     editor.putString("address",addres); 
     editor.putString("password",pass); 
     editor.putString("repassword",rpassword); 
     editor.putString("username",uname); 
     editor.apply(); 

在你的

else { 
       Intent startNewActivity = new Intent(Main2Activity.this, DisplayActivity.class); 
       startActivity(startNewActivity); 

      } 

或者我想知道你想做什麼?

+0

當我點擊提交按鈕比比驗證成功比保存在sharedpref文件並轉到下一個活動其他不 – nitamz