[英]I am not able to toast messages in Android Studio
I have created an add
button. 我创建了一个
add
按钮。 No data is entered in the EditText
field and when the user press the button I am not able to Toast
the message. 在
EditText
字段中未输入任何数据,并且当用户按下按钮时,我无法Toast
该消息。
If(text1.getText( ).toString( ).matches(" ") || text2.getText( ).toString( ).matches(" "))
{
Toast.makeText(MainActivity.this,"input values",Toast.LENGTH_SHORT.show( );
}
You are matching wrong string. 您匹配了错误的字符串。 Instead of
.match(" ")
use .match("")
or it is better to use text1.getText().toString().isEmpty()
. 代替
.match(" ")
使用.match("")
或最好使用text1.getText().toString().isEmpty()
。 In fact your if block never reached. 实际上,您的if块从未达到。
you can try this 你可以试试这个
EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_SHORT).show();
}
Try changing your code and IF condition like this: 尝试像这样更改代码和IF条件:
If (String.valueOf(text1.getText()).equals("") || String.valueOf(text2.getText()).equals(""))
{
Toast.makeText(MainActivity.this,"input values",Toast.LENGTH_SHORT).show();
}
That's all, 就这样,
Hope it helps :) 希望能帮助到你 :)
Use trim()
this function will remove spaces. 使用
trim()
此函数将删除空格。
If(text1.getText( ).toString( ).trim().equals("") || text2.getText( ).toString.trim().equals(""))
{
Toast.makeText(MainActivity.this,"You did not enter a username and password",Toast.LENGTH_SHORT.show( );
}
I hope this will helpful. 我希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.