I became really confused and I couldn't find my answer in other questions . I have a simple page with three edit texts . The first one is the Username , the second one is the Password and the third one is re entering the Password . I used intent to show them in the second Activity . Then I asked to write Username and Password to enter the third page,I don't want to see the first page any more (After closing the app) and I want to have the entered Username and Password for future uses.
Well here is my code in the first class:
package com.example.Test1;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyActivity extends Activity {
Button btn1;
EditText edit1;
EditText edit2;
EditText edit3;
TextView txt1;
TextView txt2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.btn1);
edit1 = (EditText) findViewById(R.id.edit1);
edit2 = (EditText) findViewById(R.id.edit2);
edit3 = (EditText) findViewById(R.id.edit3);
txt1 = (TextView) findViewById(R.id.txt1);
txt2 = (TextView) findViewById(R.id.txt2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if((edit2.getText().toString().equals(edit3.getText().toString()))&(!edit2.getText().toString().equals("")))
if (edit1.getText().toString().equals(""))
txt1.setText("username field is empty");
else{
Intent intent = new Intent
(MyActivity.this,MyActivity2.class);
intent.putExtra("myString", edit1.getText().toString());
intent.putExtra("myString2", edit2.getText().toString());
startActivity(intent);
}
else txt1.setText("passwords aren't same");
}
});
}
}
And this one is my second class
package com.example.Test1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyActivity2 extends Activity {
TextView txt2;
TextView txt4;
EditText us1;
EditText pw1;
Button button1;
TextView txt5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
String myString = getIntent().getStringExtra("myString");
String myString2 = getIntent().getStringExtra("myString2");
txt2 = (TextView) findViewById(R.id.txt2);
txt4 = (TextView) findViewById(R.id.txt4);
txt5 = (TextView) findViewById(R.id.txt5);
txt2.setText(myString);
txt4.setText(myString2);
us1 = (EditText) findViewById(R.id.us1);
pw1 = (EditText) findViewById(R.id.pw1);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if((us1.getText().toString().equals(txt2.getText().toString())&(pw1.getText().toString().equals(txt4.getText().toString()))))
startActivity(new Intent(MyActivity2.this,MyActivity3.class));
else {
txt5.setText("Username or Password is incorrect");
}
}
});
}
}
I want to use something else instead of txt4 (a TextView that it should be equal with an EditText) and have it saved.
If you just didnt understand what I need,please tell me to explain more.:D
you should use SharedPreference. eg
to get the SharedPreference object (context isn't important, few contexts gives this same prefs)
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
to save something to SharedPreferences
SharedPreferences.Editor editor = settings.edit();
editor.putString("Username", "user");
editor.putString("Password", "pass");
editor.commit();
to get something from SharedPreferences
String name = settings.getString("Username", "null");
the second value is value if SharedPreference for this first one isn't set.
then in onCreate you can do this:
String name = settings.getString("Username", "null");
if (!name.equals("null")) {
startActivity(new Intent(this, SecondActivity.class);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.