簡體   English   中英

關閉應用程序后保存用戶名和密碼

[英]Save username and password after closing the app

我真的很困惑,在其他問題上我找不到答案。 我有一個簡單的頁面,其中包含三個編輯文本。 第一個是用戶名,第二個是密碼,第三個是重新輸入密碼。 我打算在第二個活動中向他們展示。 然后我要求寫用戶名和密碼以進入第三頁,我不想再看到第一頁(在關閉應用程序之后),我想輸入輸入的用戶名和密碼以備將來使用。

好吧,這是我在第一堂課中的代碼:

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");
    }
});
    }




}

這是我的第二堂課

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");


                }
            }
        });

    }
}

我想用其他東西代替txt4(一個TextView,它應該與EditText相等)並保存。

如果您只是不明白我的需求,請告訴我解釋一下。

您應該使用SharedPreference。 例如

獲取SharedPreference對象(上下文並不重要,很少有上下文提供相同的首選項)

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

保存一些東西到SharedPreferences

SharedPreferences.Editor editor = settings.edit();
editor.putString("Username", "user");
editor.putString("Password", "pass");
editor.commit();

從SharedPreferences獲得一些東西

String name = settings.getString("Username", "null");

如果未為此第一個設置SharedPreference,則第二個值為value。

然后在onCreate中,您可以執行以下操作:

String name = settings.getString("Username", "null");
if (!name.equals("null")) {
    startActivity(new Intent(this, SecondActivity.class);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM