第二个活动不会以登录形式加载

时间:2017-10-04 10:13:54

标签: android-activity

我创建了一个登录表单的简单应用程序,但登录后没有进入第二个活动。代码中没有错误。你能帮我吗,这是代码:

public class MainActivity extends AppCompatActivity {
    private EditText Name;
    private EditText Password;
    private TextView Info;
    private Button Login;
    private int counter=5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Name = (EditText)findViewById(R.id.editText);
        Password = (EditText) findViewById(R.id.editText2);
        Info = (TextView)findViewById(R.id.textView);
        Login = (Button) findViewById(R.id.btn);
        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                validate(Name.getText().toString(), Password.getText().toString());
            }
        });
    }

    private void validate(String userName, String userPasswor) {
        if ((userName == "admin") && (userPasswor == "1234")) {
            Intent intent= new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        } else {
            counter--;
            Info.setText("No of Attempts Remaining: " + String.valueOf(counter));
            if (counter == 0) {
                Login.setEnabled(false);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你编写代码

if((userName =="admin")  && (userPasswor=="1234"))
    {
        Intent intent= new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }

将此代码更改为

if((userName.equals("admin"))  && (userPasswor.equals("1234")))
    {
        Intent intent= new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }

还在Android.mainfeast文件中输入第二个活动。