这是该按钮的XML文件
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_action_search"
app:fabSize="normal"
android:onClick="onSearch"/>
这是Java代码
public void onSearch(View view) {
to = (EditText) findViewById(R.id.to);
String locate = to.getText().toString();
List<Address> addressList = null;
if (locate != null || !locate.equals("")) {
Geocoder geocoder = new Geocoder(getApplicationContext());
try {
addressList = geocoder.getFromLocationName(locate, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
m = mMap.addMarker(new MarkerOptions().position(latLng));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
} else {
Toast.makeText(MapsActivity.this, "Please enter a valid location", Toast.LENGTH_LONG).show();
}
}
当TextField为空并且单击按钮时,它应该显示敬酒,但是单击按钮后,它不起作用
尝试在onCreate((
中建议的方法之外找到您的文本视图 to = (EditText) findViewById(R.id.to);
public void onSearch(View view) {
String locate = to.getText().toString();
List<Address> addressList = null;
if (locate != null || !locate.equals("")) {
Geocoder geocoder = new Geocoder(getApplicationContext());
try {
addressList = geocoder.getFromLocationName(locate, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
m = mMap.addMarker(new MarkerOptions().position(latLng));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
} else {
Toast.makeText(MapsActivity.this, "Please enter a valid location", Toast.LENGTH_LONG).show();
}
}