![](/img/trans.png)
[英]Android Studio how do I check email address and password before starting a new activity
[英]Java code not starting new activity in Android Studio? Is it to do with the context?
這是代碼:
public void check(View button){ //checks if user's username and password correct
String usernameInput;
String passwordInput;
usernameInput = ((EditText)findViewById(R.id.username)).getText().toString(); //gets user inputs as strings
passwordInput = ((EditText)findViewById(R.id.password)).getText().toString();
Log.d("username input", usernameInput);
Log.d("password input", passwordInput);
if (usernameInput == "user" && passwordInput == "password123") { //checks if correct
correct(usernameInput);
}
else incorrect(button);
}
public void correct(String usernameInput) { //if correct, launches the main activity (main menu) through an intent (see below)
Intent i = new Intent(this, MainActivity.class);
i.putExtra("username", usernameInput); //passes data from LoginRegister activity to MainActivity
startActivity(i);
}
我正在嘗試創建一個登錄系統。 基本上,當單擊按鈕時,會調用“檢查”方法。 檢查來自 2 個文本框的用戶輸入以查看它們是否是正確的用戶名和密碼; 如果是,則調用“正確”方法。 到目前為止它工作正常,但由於某種原因,新活動沒有開始(沒有錯誤,它只是沒有開始)。 我已經嘗試將各種上下文放在Intent i = new Intent(this, MainActivity.class);
行中。 ,但似乎沒有任何效果。 請幫忙。
您應該使用equals
而不是==
。
==
應在參考比較期間使用。 ==
檢查兩個引用是否指向相同的位置。 另一方面, equals()
方法應該用於內容比較。 equals()
方法評估內容以檢查相等性。
因此,您應該將代碼更改為:
public void check(View button){ //checks if user's username and password correct
String usernameInput;
String passwordInput;
usernameInput = ((EditText)findViewById(R.id.username)).getText().toString(); //gets user inputs as strings
passwordInput = ((EditText)findViewById(R.id.password)).getText().toString();
Log.d("username input", usernameInput);
Log.d("password input", passwordInput);
if (usernameInput.equals("user") && passwordInput.equals("password123")) { //checks if correct
correct(usernameInput);
}
else incorrect(button);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.