我创建了简单的android表单,其中我获得了用户名和密码 如果我输入用户名和密码是deepak,那么它也会转到其他条件。 但是我的if条件在任何情况下都是假的,我不知道为什么。
package com.example.dac.simple_intent_form;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button;
EditText email, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
email = (EditText) findViewById(R.id.eemail);
password = (EditText) findViewById(R.id.epassword);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String ema = email.getText().toString();
String pass = password.getText().toString();
if (ema == "deepak" && pass == "deepak")
{
Bundle b = new Bundle();
b.putString("email", ema);
b.putString("password", pass);
Intent i = new Intent(getApplicationContext(),Activity2.class);
i.putExtras(b);
startActivity(i);
}
else
{
Toast.makeText(getApplicationContext(),"Plase Enter Right Username and Password",Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
使用此
您的ema.equals("deepak") && pass.equals("deepak")
声明中的 if
答案 1 :(得分:0)
尝试使用此
String ema = email.getText().toString();
String pass = password.getText().toString();
if (ema.toLowerCase().equals("deepak") && pass.toLowerCase().equals("deepak"))
{
}
答案 2 :(得分:0)
检查这样的情况,它会正常工作
if (ema.equals("deepak") && pass.equals("deepak")){}
答案 3 :(得分:0)
if(" deepak" .equals(ema)&&" deepak" .equals(pass))将完成这项工作。这也将确保对象ema和通过不为空并且可以避免空检查
答案 4 :(得分:-1)
使用.equals()
if (ema.equals("deepak") && pass.equals("deepak"))
{
Bundle b = new Bundle();
b.putString("email", ema);
b.putString("password", pass);
Intent i = new Intent(getApplicationContext(),Activity2.class);
i.putExtras(b);
startActivity(i);
}