Java 如果条件不起作用
我正试图做出一个Java 如果条件不起作用,java,android,Java,Android,我正试图做出一个如果条件,但效果不好。我希望当EditText为null时,用户不转到下一个活动 当它被填满时,按下按钮后进入下一个活动 Name = is my EditText assignment one_next_diaspora_bt = is the Button. 下面是我的代码: final String Name = name_diaspora_edt.getText().toString(); one_next_diaspora_bt.setOnClickListene
如果条件
,但效果不好。我希望当EditText
为null
时,用户不转到下一个活动
当它被填满时,按下按钮后进入下一个活动
Name = is my EditText assignment
one_next_diaspora_bt = is the Button.
下面是我的代码:
final String Name = name_diaspora_edt.getText().toString();
one_next_diaspora_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Name.matches("")){
Toast.makeText(getApplicationContext(),"Make sure that you have filled your name please !!!",Toast.LENGTH_LONG).show();
}
else {
Intent i = new Intent(Diaspora.this,DiasporaTwo.class);
i.putExtra("Name",Name);
i.putExtra("Age",Age);
i.putExtra("Gender",Gender);
i.putExtra("MaritalStatus",MaritalStatus);
startActivity(i);
}
}
});
使用:
并遵循Java命名约定。变量名称应以小写字符开头您可以使用此方法直接检查EditText,equals()用于字符串 试试这个
String data = Name.getText().toString();
然后
将您的条件更改为:
if(edt.getText().toString().isEmpty()){
}
或
尝试
TextUtils.isEmpty
,它也将检查null和empty。TextUtils是安卓软件包android.text中的内置类
if(android.text.TextUtils.isEmpty(Name)) {
首先我想说,你真的应该遵循命名惯例。
但是您需要抓住编辑文本中的字符串
,并将其与进行比较
if(name.getText().toString().equals("")){
//do something
}
使用android默认验证,TextUtils类使用以下方法:
one_next_diaspora_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String Name = name_diaspora_edt.getText().toString().trim();
if(Name.matches("")){
Toast.makeText(getApplicationContext(),"Make sure that you have filled your name please !!!",Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(Diaspora.this,DiasporaTwo.class);
i.putExtra("Name",Name);
i.putExtra("Age",Age);
i.putExtra("Gender",Gender);
i.putExtra("MaritalStatus",MaritalStatus);
startActivity(i);
}
}
});
}
试试这个:
if (Name != null && Name.equalIgnoreCase("null") && Name.trim().equalIgnoreCase("")){
}else
{
}
使用TextUtils.isEmpty(Name)
而不是Name.matches(“”
)
试试这个:
final String Name = name_diaspora_edt.getText().toString();
one_next_diaspora_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(TextUtils.isEmpty(Name)) {
Toast.makeText(getApplicationContext(),"Make sure that you have filled your name please !!!",Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(Diaspora.this, DiasporaTwo.class);
i.putExtra("Name", Name);
i.putExtra("Age", Age);
i.putExtra("Gender", Gender);
i.putExtra("MaritalStatus", MaritalStatus);
startActivity(i);
}
}
});
希望这将有助于~final String Name=Name\u diaspora\u edt.getText().toString();写onclick functionsSorry的indide,但在您的原始问题Name
中是一个文本字段!现在是字符串了?@jhildstack感谢您的改进
if (TextUtils.isEmpty(Name)){
Toast.makeText(getApplicationContext(),"Make sure that you have filled your name please !!!",Toast.LENGTH_LONG).show();
}
one_next_diaspora_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String Name = name_diaspora_edt.getText().toString().trim();
if(Name.matches("")){
Toast.makeText(getApplicationContext(),"Make sure that you have filled your name please !!!",Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(Diaspora.this,DiasporaTwo.class);
i.putExtra("Name",Name);
i.putExtra("Age",Age);
i.putExtra("Gender",Gender);
i.putExtra("MaritalStatus",MaritalStatus);
startActivity(i);
}
}
});
}
if (Name != null && Name.equalIgnoreCase("null") && Name.trim().equalIgnoreCase("")){
}else
{
}
TextUtils.isEmpty(CharSequence str) >> Returns true if the string is null or 0-length.
final String Name = name_diaspora_edt.getText().toString();
one_next_diaspora_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(TextUtils.isEmpty(Name)) {
Toast.makeText(getApplicationContext(),"Make sure that you have filled your name please !!!",Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(Diaspora.this, DiasporaTwo.class);
i.putExtra("Name", Name);
i.putExtra("Age", Age);
i.putExtra("Gender", Gender);
i.putExtra("MaritalStatus", MaritalStatus);
startActivity(i);
}
}
});