【问题标题】:How to check AutoCompleteTextView if it contains text?如果 AutoCompleteTextView 包含文本,如何检查它?
【发布时间】:2017-04-28 05:35:32
【问题描述】:

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

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();
    }
}

【问题讨论】:

  • 如果它给出异常会发生什么。把它放在问题上,我也希望 onFindPathClick 绑定到一个事件?

标签: java android autocomplete


【解决方案1】:

使用 isEmpty() 方法代替检查 (originAddress !="")。

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();
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-14
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2017-11-13
    • 2020-04-16
    相关资源
    最近更新 更多