@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtUserName=(EditText)this.findViewById(R.id.txtUname);
txtPassword=(EditText)this.findViewById(R.id.txtPwd);
btnLogin=(Button)this.findViewById(R.id.btnLogin);
Button btnLogin=(Button)this.findViewById(R.id.Button01);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(LoginappActivity.this, "Invalid Login",Toast.LENGTH_LONG).show();
}
}
}
);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), AddName.class);
startActivityForResult(myIntent, 0);
}});
}
}
当应用程序在模拟器中运行时,它已成功编译,但我不知道是否成功登录或登录无效。
答案 0 :(得分:1)
您需要将此代码放在Login Button的onClickListener中。修改后的代码应为
Button btnLogin=(Button)this.findViewById(R.id.Button01);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
} else{
Toast.makeText(LoginappActivity.this, "Invalid Login",Toast.LENGTH_LONG).show();
}
}
}
});
答案 1 :(得分:0)
在按钮单击事件中写入If ... Else。
Button btnLogin=(Button)this.findViewById(R.id.Button01);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(LoginappActivity.this, "Invalid Login",Toast.LENGTH_LONG).show();
}
}
}
});