无法解决 Android 中的 makeText 方法。

huangapple go评论61阅读模式
英文:

Cannot resolve method makeText in Android

问题

尝试 1:

if ((fieldvalue.getText().toString()).matches("")) {
    Toast.makeText(this, "您未输入用户名", 
    Toast.LENGTH_SHORT).show();
    return;
}

尝试 2:

if ((fieldvalue.getText().toString()).matches("")) {
    public static Toast makeText (this, "您未输入用户名", 
    Toast.LENGTH_SHORT);
    return;
}

注意:尝试 2 中的代码似乎存在错误,无法正确执行。

英文:

I was trying to play with an android toast. But encountered a problem saying Cannot resolve method makeText. I searched and found many answers but not a single worked for me.

Try 1:

if ((fieldvalue.getText().toString()).matches("")) {
    Toast.makeText(this, "You did not enter a username", 
    Toast.LENGTH_SHORT).show();
    return;
}

Try 2:

if ((fieldvalue.getText().toString()).matches("")) {
    public static Toast makeText (this, "You did not enter a username", 
    Toast.LENGTH_SHORT);
    return;
}

答案1

得分: 1

尝试使用isEmpty()。同时确保使用.this来定义activityclass,例如yourclass.this

if (fieldvalue.getText().toString().isEmpty()) {

    Toast.makeText(youractivity.this, "您未输入用户名!", Toast.LENGTH_SHORT).show();

} else {

    Toast.makeText(youractivity.this, "成功! :)", Toast.LENGTH_SHORT).show();
}
英文:

Try using isEmpty(). Also make sure to define the activityclass with .this like yourclass.this .

if(fieldvalue.getText().toString().isEmpty()) {

Toast.makeText(youractivity.this, "You did not enter a username!", Toast.LENGTH_SHORT).show();

}
else{
                    
Toast.makeText(youractivity.this, "Success! :)", Toast.LENGTH_SHORT).show();
}

答案2

得分: -2

Sure, here's the translated code:

if (fieldvalue.getText().toString().trim().isEmpty()) {
    Toast.makeText(this, "您未输入用户名", Toast.LENGTH_SHORT).show();
    return;
}
英文:
if (fieldvalue.getText().toString().trim().isEmpty()) {
    Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
    return;
}

huangapple
  • 本文由 发表于 2020年7月25日 20:55:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63088559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定