Intent类通常与按钮关联。单击按钮时,Intent类可以用于简单地唤醒/启动另一个活动,也可以将一些数据(String/int/etc)传递给该活动,如果您需要,它还可以获取一些数据。
我有一个地图,在android studio上使用谷歌地图api2,当我点击任何地方时,我想打开一个列表视图,那里会有单选按钮和其他类型的东西。我不知道如何做到这一点,如何使用意图,如何使用听众,有人能帮助我吗?这是我到目前为止得到的代码:
public class MainActivity extends FragmentActivity implements OnMapReadyCallback,LocationListener {
private MapView mapV;
private GoogleMap map;
private GoogleMapOptions options;
private CameraUpdate camUp;
private double lat,lon;
private LatLng koordinate;
private String provider;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map = fm.getMap();
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
koordinate = new LatLng(location.getLatitude(),location.getLongitude());
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(koordinate, 10);
map.animateCamera(cameraUpdate);
}
public void openList(View view)
{
Intent intent = new Intent(intent.ACTION_VIEW ,);
ListView list = (ListView) findViewById(R.id.idList);
}
XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/idList"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="openList"/>
要简单地唤醒另一个活动,可以执行以下操作:
Intent i = new Intent (*activity_action_name*);
startActivity(i);
对于听众来说,最好在有很多onClick的时候使用。因为这是减少数据冗余的好方法。如果你只有一个onClick。没有必要使用OnClickListener。
示例:如果您有两个按钮。您可以执行以下操作。
//in onCreate declare some buttons
Button bt1 = (Button) findViewById(R.id.*button1_id*);
bt1.setOnClickListener(new clsOnClickListener());
Button bt2 = (Button) findViewById(R.id.*button2_id*);
bt2.setOnClickListener(new clsOnClickListener());
//You can create a class that implements OnClickListener
private class clsOnClickListener implements View.OnClickListener{
public void onClick(View v){
if (v.getId()==R.id.button1_id{
//do what you want
}
else if (v.getId()==R.id.button2_id){
//do something else
}
}
}
为了在地图上做到这一点,您必须创建具有自定义布局的InfoWindow,其中将包含您提到的小部件。这是一个示范教程。