Ich bin wirklich neu und stetige Fortschritte in Bezug auf eine Anwendung. Ich möchte eine App erstellen, mit der der Benutzer ein zufälliges Passwort mithilfe eines Schiebereglers generieren kann, um die Länge festzulegen und dann generieren zu lassen. Sobald dies geschehen ist, möchte ich, dass der Benutzer seine generierten Passwörter speichern kann. Bis jetzt habe ich alles erreicht, abgesehen von der Speicherung der Passwörter, die später wiederhergestellt werden müssen, nachdem die App geschlossen wurde. Ich habe viele Posts gelesen und ich kann nicht verstehen, wie man Anwendungsdaten speichert, nachdem die App geschlossen wurde, wetter es zu einer Datei oder SharedPrefrences. Etwas Klärung und Hilfe würde sehr geschätzt werden.Wie Sie TextView/Array in Android speichern und später abrufen?
Was ich
Ich möchte eine Schaltfläche, die der Benutzer klickt und deren Passwörter generiert erreichen wollen haben (die in diesem Array gespeichert sind), dann in der App gespeichert werden, wenn sie es in der Nähe und kommen später können sie diese gespeicherten Passwörter abrufen.
Vielen Dank an alle im fortgeschrittenen, kann ich eine Begnadigung in Bezug auf den Code seiner wahrscheinlich sehr chaotisch betteln, aber ich lerne: D
Dank
-Jack
Hauptaktivität XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jrfapplications.passgen.MainActivity">
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/passdesc"
android:layout_marginTop="27dp"
android:id="@+id/seekBar"
android:max="20"
android:progress="10"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="@string/passbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:textSize="12sp"
android:onClick="genpass"
android:layout_marginTop="40dp"
android:layout_below="@+id/seekBar"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/seekBar"
android:layout_centerHorizontal="true"
android:text="@string/SeekValueView"
android:id="@+id/SeekTextView" />
<TextView
android:text="@string/pass_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/pass_desc_size"
android:id="@+id/passdesc"
android:textAlignment="center"
android:layout_marginTop="122dp"
android:layout_below="@+id/password"
android:layout_centerHorizontal="true" />
<TextView
android:text="@string/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/password"
android:textSize="@dimen/password"
android:layout_marginTop="86dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="@string/NextPageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:id="@+id/button2"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button"
android:onClick="goToSavedPassActivity"
android:textSize="12sp" />
</RelativeLayout>
Hauptaktivität Java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView mPassword;
Button mGenPassButton; //Defining Variables
final TextView mSeekBarValue;
Button mSavedPassActivity;
mPassword = (TextView) findViewById(R.id.password);
mGenPassButton = (Button) findViewById(R.id.button); //Settings values and locations
mSeekBarValue = (TextView) findViewById(R.id.SeekTextView);
SeekBar seek=(SeekBar) findViewById(R.id.seekBar);
mSavedPassActivity = (Button) findViewById(R.id.button2);
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { //Seekbar Actions
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { //When user moves it
// TODO Auto-generated method stub
SeekBarValue.seekval = progress;
mSeekBarValue.setText(String.valueOf(progress));
}
});
mSavedPassActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SavedPassActivity.class));
}
});
mGenPassButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
final StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < SeekBarValue.seekval; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
final String output = sb.toString();
mPassword.setText(output);
SavedPassArray.SavedPasswords.add(output);
}
});
}
}
SeekBarValue Java:
public class SeekBarValue {
public static int seekval;
}
SavedPassedArray Java:
public class SeekBarValue {
public static int seekval;
}
ViewPasswordActivity ScreenShot
Passwort anzeigen Aktivität XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_saved_pass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jrfapplications.passgen.SavedPassActivity">
<TextView
android:text="@string/PassList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/PasswordsList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="18sp"
android:scrollbars="vertical"
android:textStyle="normal|bold" />
</RelativeLayout>
Passwort anzeigen Java:
public class SavedPassActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved_pass);
final TextView mPasswordsLists;
mPasswordsLists = (TextView) findViewById(R.id.PasswordsList);
StringBuilder allpasswords = new StringBuilder();
for (String s : SavedPasswords){
if(allpasswords.length() > 0){
allpasswords.append("\n");
}
allpasswords.append(s); }
mPasswordsLists.setText(allpasswords.toString());
mPasswordsLists.setMovementMethod(new ScrollingMovementMethod());
//for(int i=0; i < SavedPasswords.size(); i++){
}
}
Sie möchten eine Datenbank. Listen in SharedPreferences funktionieren nicht so gut. https://developer.android.com/training/basics/data-storage/databases.html –