只有一个 onClick 侦听器在初始启动时响应 android studio
Only one onClick listener responds on initial startup android studio
我有 3 个按钮,我正在使用 mapsactivity。只有第一个按钮响应,然后按时间顺序响应第二个按钮。我在这些按钮中存储了标记,因此我希望它们在我单击其中任何一个时独立响应
这是我的 .java 文件
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
final Button button1 = (Button) findViewById(R.id.buttonMaps);
final Button button2 = (Button)findViewById(R.id.buttonMaps2);
final Button button3 = (Button)findViewById(R.id.buttonMaps3);
@Override
protected void onCreate(Bundle savedinstancestate) {
super.onCreate(savedinstancestate);
setContentView(R.layout.activity_maps);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMap.clear();
if (button1.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng(, )).title("").snippet("My Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.clear();
if (button2.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_hospital)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(), ));
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.clear();
if (button3.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng(`enter code here`)).title(" ").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_busstop)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(), ));
}
}
});
}
}
});
}
}
});
}
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng ( ),));
}
}
这是我的activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
tools:layout="@android:layout/simple_spinner_dropdown_item" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="text1"
android:padding="8dp"
android:background="#53fed0"
android:id="@+id/buttonMaps"
android:layout_marginLeft="90dp"
android:layout_marginStart="90dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"
android:id="@+id/buttonMaps2"
android:layout_marginTop="35dp"
android:layout_below="@+id/buttonMaps"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bus Stops"
android:id="@+id/buttonMaps3"
android:layout_below="@+id/buttonMaps2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp"
android:clickable="true"/>
</RelativeLayout>
您正在其他 onclicklisteners 中设置 onclicklisteners。在您的实现中,当单击 button1 时,将设置 button2 的 onclicklistener,当单击按钮 2(在单击 button1 之后)时,将设置 button3 的 onclicklistnere。直接在 oncreate 中设置所有 3 个 onclicklisteners。
你定义的按钮2-3只在你按下按钮1的时候才按下,需要单独标识。
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMap.clear();
if (button1.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng(, )).title("").snippet("My Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.clear();
if (button2.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_hospital)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(), ));
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.clear();
if (button3.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng(`enter code here`)).title(" ").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_busstop)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(), ));
}
}
});
我有 3 个按钮,我正在使用 mapsactivity。只有第一个按钮响应,然后按时间顺序响应第二个按钮。我在这些按钮中存储了标记,因此我希望它们在我单击其中任何一个时独立响应
这是我的 .java 文件
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
final Button button1 = (Button) findViewById(R.id.buttonMaps);
final Button button2 = (Button)findViewById(R.id.buttonMaps2);
final Button button3 = (Button)findViewById(R.id.buttonMaps3);
@Override
protected void onCreate(Bundle savedinstancestate) {
super.onCreate(savedinstancestate);
setContentView(R.layout.activity_maps);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMap.clear();
if (button1.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng(, )).title("").snippet("My Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.clear();
if (button2.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_hospital)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(), ));
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.clear();
if (button3.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng(`enter code here`)).title(" ").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_busstop)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(), ));
}
}
});
}
}
});
}
}
});
}
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng ( ),));
}
}
这是我的activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
tools:layout="@android:layout/simple_spinner_dropdown_item" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="text1"
android:padding="8dp"
android:background="#53fed0"
android:id="@+id/buttonMaps"
android:layout_marginLeft="90dp"
android:layout_marginStart="90dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"
android:id="@+id/buttonMaps2"
android:layout_marginTop="35dp"
android:layout_below="@+id/buttonMaps"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bus Stops"
android:id="@+id/buttonMaps3"
android:layout_below="@+id/buttonMaps2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp"
android:clickable="true"/>
</RelativeLayout>
您正在其他 onclicklisteners 中设置 onclicklisteners。在您的实现中,当单击 button1 时,将设置 button2 的 onclicklistener,当单击按钮 2(在单击 button1 之后)时,将设置 button3 的 onclicklistnere。直接在 oncreate 中设置所有 3 个 onclicklisteners。
你定义的按钮2-3只在你按下按钮1的时候才按下,需要单独标识。
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMap.clear();
if (button1.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng(, )).title("").snippet("My Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.clear();
if (button2.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_hospital)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(), ));
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.clear();
if (button3.isShown()) {
mMap.addMarker(new MarkerOptions().position(new LatLng(`enter code here`)).title(" ").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_busstop)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(), ));
}
}
});