第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

第一次啟動時將 Activity 設(shè)置為默認 Activity

第一次啟動時將 Activity 設(shè)置為默認 Activity

ibeautiful 2023-07-28 15:15:27
我編寫了一個 Android 應(yīng)用程序,用于檢查 EditText 中輸入的字符串是否與共享首選項中存儲的字符串相同,如果錯誤,則會出現(xiàn)一個 Toast,告訴用戶他剛剛輸入的字符串不正確,這是真的?;顒右验_放。我希望每次用戶打開應(yīng)用程序時都打開此打開的活動。這是我嘗試的:private PreferenceHelper preferenceHelper;private ParseContent parseContent;private final int RegTask = 1;private EditText editText;private Button button;private String string;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_login2);    parseContent = new ParseContent(this);    preferenceHelper = new PreferenceHelper(this);    editText = findViewById(R.id.code);    button = findViewById(R.id.abonnement);    final SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);    button.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            string = editText.getText().toString();            if (string.equals(pref.getString("code", null))){                Intent intent = new Intent(Login.this, WelcomeActivity.class);                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);                startActivity(intent);            }else {                Toast.makeText(Login.this, "Désolé mais votre code est incorrect",Toast.LENGTH_LONG).show();            }        }    });}當用戶輸入正確的代碼(即共享首選項中先前注冊的代碼)時,會WelcomeActivty.class打開,但當用戶退出應(yīng)用程序并再次進入時,Login.class始終會顯示,而我希望WelcomeActivity.class每次用戶再次進入應(yīng)用程序時都會啟動。 ..請問我該怎么做?
查看完整描述

4 回答

?
繁星coding

TA貢獻1797條經(jīng)驗 獲得超4個贊

注意:我制作了一個登錄系統(tǒng)(沒有密碼),即使您沒有在登錄系統(tǒng)上工作,我仍然認為該技術(shù)仍然適用于您的情況。

嘗試添加另一個存儲布爾值的共享首選項,以檢查用戶是否在線。例如,在您的 LoginActivity 中,添加

editor.putBoolean("isOnline", true); 編輯器.apply();

當您單擊登錄按鈕時。

同樣,當您注銷時,只需輸入

editor.putBoolean("isOnline", false); 編輯器.apply();

我是按照你的問題做的。

MainActivity.java(這是登錄活動)

  public class MainActivity extends AppCompatActivity {


    SharedPreferences pref;

    SharedPreferences.Editor editor;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        final EditText et = findViewById(R.id.et);

        Button btnRegister = findViewById(R.id.btn_register);

        Button btnLogin = findViewById(R.id.btn_login);




        pref = getSharedPreferences("MyPref", MODE_PRIVATE);

        editor = pref.edit();


        btnRegister.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {


                editor.putString("username", et.getText().toString());

                editor.putBoolean("isOnline", false);

                editor.apply();

            }

        });


        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {


                if (et.getText().toString().equals(pref.getString("username", null))) {


                    Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class);

                    intent.putExtra("name", et.getText().toString());

                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);


                    editor.putBoolean("isOnline", true);

                    editor.apply();


                    startActivity(intent);

                    finish();

                }

                else {

                    Toast.makeText(MainActivity.this, "Incorrect input", Toast.LENGTH_LONG).show();

                }

            }

        });


// This is when user has not clicked the log out button, then we go to the WelcomeActivity instead

        if (pref.getBoolean("isOnline", false)) {


            startActivity(new Intent(getApplicationContext(), WelcomeActivity.class));

            finish();

        }

    }

}

WelcomeActivity.java(登錄后訪問的Activity)


public class WelcomeActivity extends AppCompatActivity {

    SharedPreferences pref;

    SharedPreferences.Editor editor;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_welcome);



        pref = getSharedPreferences("MyPref", MODE_PRIVATE);

        editor = pref.edit();


        editor.putBoolean("isOnline", true);

        editor.apply();


        Button btnLogout = findViewById(R.id.logout);

        btnLogout.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {


                // When user clicks on the logout button, we set this to false. So everything will be back to normal.

                editor.putBoolean("isOnline", false);

                editor.apply();


                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {

                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);

                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    startActivity(intent);

                    finishAffinity();

                }

                else {

                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);

                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    startActivity(intent);

                    finish();

                }

            }

        });

    }

}


查看完整回答
反對 回復(fù) 2023-07-28
?
MMTTMM

TA貢獻1869條經(jīng)驗 獲得超4個贊

將輸入的值保存在共享首選項中,并使用“EncryptedSharedPreferences”以確保安全


在 onCreate 檢查用戶是否輸入了值并在進行檢查之前保存


if (string.equals(pref.getString("code", null))){

        Intent intent = new Intent(Login.this, WelcomeActivity.class);

      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

      startActivity(intent);`enter code here`

}

并完成當前活動所以現(xiàn)在下一個屏幕將自動打開


查看完整回答
反對 回復(fù) 2023-07-28
?
梵蒂岡之花

TA貢獻1900條經(jīng)驗 獲得超5個贊

您需要更改您的邏輯,當您打開應(yīng)用程序時,Login.class 將是您的第一個屏幕,在該屏幕中您必須檢查輸入的代碼是否與共享首選項值類似,如果是 true,則將登錄屏幕移至歡迎屏幕,希望我的回答對您有所幫助



查看完整回答
反對 回復(fù) 2023-07-28
?
夢里花落0921

TA貢獻1772條經(jīng)驗 獲得超6個贊

伙計,這可以通過創(chuàng)建會話來完成。


就像當您的編輯文本與所需文本匹配時,您應(yīng)該在共享首選項中分配一個值,該值將在創(chuàng)建會話時充當。


button.setOnClickListener(new View.OnClickListener() {


 @Override

 public void onClick(View v) {

 string = editText.getText().toString();

 if (string.equals(pref.getString("code", null))){

    SharedPreferences.Editor edit = pref.edit();

    edit.putString("Session", "1");

    edit.apply();

    Intent intent = new Intent(Login.this, WelcomeActivity.class);

  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

  startActivity(intent);

}else {

  Toast.makeText(Login.this, "Désolé mais votre code est incorrect",Toast.LENGTH_LONG).show();

}

現(xiàn)在在您所需屏幕的 onCreate 方法中。只需檢查會話值是否包含在您的 SharedPreferences 中,然后根據(jù)您的需要執(zhí)行任何操作。


查看完整回答
反對 回復(fù) 2023-07-28
  • 4 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號