Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package local.bdoop.tyler.lab_6_1;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.icu.text.SimpleDateFormat;
- import android.icu.util.Calendar;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.format.DateFormat;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Button;
- import java.io.FileOutputStream;
- import java.util.Date;
- public class MainActivity extends AppCompatActivity {
- private final String NUMBER_KEY = "number_key";
- private final String STRING_KEY = "string_key";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Code to get a handle to the default SharedPreferences
- SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
- int defaultNumValue = sharedPref.getInt(NUMBER_KEY, 0);
- String defaultStrValue = sharedPref.getString(STRING_KEY, "What Word?");
- EditText defaultNumber = (EditText) findViewById(R.id.editTextNum);
- EditText defaultString = (EditText) findViewById(R.id.editTextWord);
- defaultNumber.setText("" + defaultNumValue);
- defaultString.setText("" + defaultStrValue);
- final Button saveButton = findViewById(R.id.saveButton);
- saveButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- save(v);
- }
- });
- final Button loginButton = findViewById(R.id.loginButton);
- loginButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- login(v);
- }
- });
- }
- public void save(View view) {
- // Code to get a handle to the default SharedPreferences
- SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPref.edit();
- EditText editNum = (EditText) findViewById(R.id.editTextNum);
- String number = editNum.getText().toString();
- int numb = Integer.parseInt(number);
- Log.d("Save", "Saved number " + numb);
- EditText editStr = (EditText) findViewById(R.id.editTextWord);
- String str = editStr.getText().toString();
- Log.d("Save", "" + str);
- // Add an Integer value 10 and store this with the key NUMBER_KEY
- editor.putInt(NUMBER_KEY, numb);
- editor.putString(STRING_KEY, str);
- editor.commit();
- }
- public void login(View view) {
- String filename = "login_data.txt";
- FileOutputStream outputStream;
- Date c = Calendar.getInstance().getTime();
- SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
- String formattedDate = df.format(c);
- EditText usernameText = (EditText) findViewById(R.id.editTextUsername);
- String username = usernameText.getText().toString();
- EditText passwordText = (EditText) findViewById(R.id.editTextPassword);
- String password = passwordText.getText().toString();
- Log.d("userpass", "" + username + " " + password + " " + formattedDate);
- String userPassDate = username + " " + password + " " + formattedDate + "\n";
- try {
- outputStream = openFileOutput(filename, Context.MODE_APPEND);
- outputStream.write(userPassDate.getBytes());
- outputStream.close();
- Log.d("hackerman", "" + username + " " + password + " " + formattedDate);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement