我在创建登录活动时遇到一些问题。 即使我输入了正确的用户名(123456)和密码(123456789)。
我确保将getText()方法放在onLogin内,但是我总是收到“登录失败”消息,因此我假设我没有从EditText获得任何数据,所以我看不到出了什么问题。 此登录Java类:
public class LoginActivity extends AppCompatActivity {
private EditText UsernameEt;
private EditText PasswordEt;
AlertDialog alertDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
UsernameEt=findViewById(R.id.username);
PasswordEt=findViewById(R.id.password);
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Login Status");
}
public void onLogin(View view) {
String username = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = "login";
//BackgroundWorker backgroundWorker = new BackgroundWorker(this);
//backgroundWorker.execute(type, username, password);
if(username=="123456" && password=="123456789"){
alertDialog.setMessage("Login success");
alertDialog.show();
} else {
alertDialog.setMessage("Login failed");
alertDialog.show();
}
}
}
这是登录xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/bg"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#FFFFFF"
android:drawableLeft="@drawable/ic_person_black_24dp"
android:ems="10"
android:hint="IPN"
android:inputType="textPersonName"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#FFFFFF"
android:drawableLeft="@drawable/ic_lock_black_24dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF" />
<Button
android:id="@+id/buttonLogin"
style="@style/DefaultButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginTop="16dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:background="@drawable/gradiant"
android:onClick="onLogin"
android:text="@string/login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
尝试
username.getText().toString().contentEquals("123456") && password.getText().toString().contentEquals("123456789")
答案 1 :(得分:0)
if(username.equals("123456") && password.equals("123456789"){
// login true
}