如果AutoCompleteTextView包含文本,如何检查?

时间:2017-04-27 21:26:58

标签: java android autocomplete

我有两个AutoCompleteTextView片段,用户应填写。然后这两个字符串值必须发送到其他活动,否则(如果它没有填充)你应该看到一个祝酒词。我试图实现它,但它不起作用:

public void onFindPathClick(View view) {
    String originAddress = OriginPlaceFragment.mAutoPlaceSearch.getText().toString();
    String destinationAddress = DestinationPlaceFragment.mAutoPlaceSearch.getText().toString();

    Intent intent = new Intent(SimpleTabsActivity.this, MapsActivity.class);
    if ((originAddress != "") && (!originAddress.equals(null)) && (destinationAddress != "") && (!destinationAddress.equals(null))) {
        intent.putExtra(ORIGIN_ADDRESS, originAddress);
        intent.putExtra(DESTIN_ADDRESS, destinationAddress);
        startActivity(intent);
    } else {
        Toast.makeText(this, "Fill \"from\" and \"to\" fields", Toast.LENGTH_SHORT).show();
    }
}

1 个答案:

答案 0 :(得分:0)

而不是检查(originAddress!=“”),使用isEmpty()方法。

public void onFindPathClick(View view) {
String originAddress = OriginPlaceFragment.mAutoPlaceSearch.getText().toString();
String destinationAddress = DestinationPlaceFragment.mAutoPlaceSearch.getText().toString();

Intent intent = new Intent(SimpleTabsActivity.this, MapsActivity.class);
if (!originAddress.toString().isEmpty() && !destinationAddress.toString().isEmpty()) {
    intent.putExtra(ORIGIN_ADDRESS, originAddress);
    intent.putExtra(DESTIN_ADDRESS, destinationAddress);
    startActivity(intent);
} else {
    Toast.makeText(this, "Fill \"from\" and \"to\" fields", Toast.LENGTH_SHORT).show();
}
}