Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. package local.bdoop.tyler.lab_6_1;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.icu.text.SimpleDateFormat;
  6. import android.icu.util.Calendar;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.text.format.DateFormat;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.EditText;
  13. import android.widget.Button;
  14.  
  15. import java.io.FileOutputStream;
  16. import java.util.Date;
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19.  
  20. private final String NUMBER_KEY = "number_key";
  21. private final String STRING_KEY = "string_key";
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27.  
  28. // Code to get a handle to the default SharedPreferences
  29.  
  30.  
  31.  
  32.  
  33. SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
  34.  
  35. int defaultNumValue = sharedPref.getInt(NUMBER_KEY, 0);
  36. String defaultStrValue = sharedPref.getString(STRING_KEY, "What Word?");
  37.  
  38. EditText defaultNumber = (EditText) findViewById(R.id.editTextNum);
  39. EditText defaultString = (EditText) findViewById(R.id.editTextWord);
  40.  
  41. defaultNumber.setText("" + defaultNumValue);
  42. defaultString.setText("" + defaultStrValue);
  43.  
  44. final Button saveButton = findViewById(R.id.saveButton);
  45.  
  46. saveButton.setOnClickListener(new View.OnClickListener() {
  47. @Override
  48. public void onClick(View v) {
  49. save(v);
  50. }
  51. });
  52.  
  53. final Button loginButton = findViewById(R.id.loginButton);
  54. loginButton.setOnClickListener(new View.OnClickListener() {
  55. @Override
  56. public void onClick(View v) {
  57. login(v);
  58. }
  59. });
  60. }
  61.  
  62. public void save(View view) {
  63.  
  64.  
  65. // Code to get a handle to the default SharedPreferences
  66. SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
  67. SharedPreferences.Editor editor = sharedPref.edit();
  68.  
  69. EditText editNum = (EditText) findViewById(R.id.editTextNum);
  70. String number = editNum.getText().toString();
  71. int numb = Integer.parseInt(number);
  72. Log.d("Save", "Saved number " + numb);
  73.  
  74. EditText editStr = (EditText) findViewById(R.id.editTextWord);
  75. String str = editStr.getText().toString();
  76. Log.d("Save", "" + str);
  77.  
  78. // Add an Integer value 10 and store this with the key NUMBER_KEY
  79.  
  80. editor.putInt(NUMBER_KEY, numb);
  81. editor.putString(STRING_KEY, str);
  82. editor.commit();
  83.  
  84. }
  85.  
  86. public void login(View view) {
  87.  
  88. String filename = "login_data.txt";
  89. FileOutputStream outputStream;
  90.  
  91. Date c = Calendar.getInstance().getTime();
  92. SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
  93. String formattedDate = df.format(c);
  94.  
  95. EditText usernameText = (EditText) findViewById(R.id.editTextUsername);
  96. String username = usernameText.getText().toString();
  97.  
  98. EditText passwordText = (EditText) findViewById(R.id.editTextPassword);
  99. String password = passwordText.getText().toString();
  100. Log.d("userpass", "" + username + " " + password + " " + formattedDate);
  101.  
  102. String userPassDate = username + " " + password + " " + formattedDate + "\n";
  103.  
  104. try {
  105. outputStream = openFileOutput(filename, Context.MODE_APPEND);
  106. outputStream.write(userPassDate.getBytes());
  107. outputStream.close();
  108.  
  109. Log.d("hackerman", "" + username + " " + password + " " + formattedDate);
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. }
  113.  
  114.  
  115.  
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement