I have problem with edittext. I get text of Edit text and I want to say if text in edit text is equal to "a" do something but its not working
button.setOnClickListener(new View.OnClickListener() {
String adadrandom=editText.getText().toString();
@Override
public void onClick(View v) {
if (adadrandom=="a"){
System.out.println("yes");
}else {
System.out.println("no");
}
}
});
also i use .equals() but again i see the same answer
CodePudding user response:
Write getText
code inside the onClick method, and compare the values with equals
method
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String adadrandom=editText.getText().toString();
if (adadrandom.equals("a")){
System.out.println("yes");
}else {
System.out.println("no");
}
}
});
CodePudding user response:
Your IDE should be warning you not to used ==
but to use .equals
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String adadrandom = editText.getText().toString();
//Use equals and by doing it on the a string you get
// null protection.
if ("a".equals(adadradom)){
System.out.println("yes");
}else {
System.out.println("no");
}
}
});
CodePudding user response:
.equals()
method for content comparison==
operators for reference comparisonbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String adadrandom = editText.getText().toString(); String displayData = adadrandom.equals("a") ? "Yes" : "No"; System.out.println(displayData); } });
CodePudding user response:
Place your getting text code in onClick method and replace ==
with .equals
for ignore warning from IDE.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String adadrandom = editText.getText().toString();
if (adadradom.contentEquals("a")){
System.out.println("yes");
}else {
System.out.println("no");
}
}
});