【发布时间】:2018-01-10 16:15:18
【问题描述】:
我有两个活动位置活动和家庭活动,并且有两个不同的按钮,两个启动位置活动 btnGPSStartPoint 和 btnGPSEndPoint。
我想在从 btnGPSStartPoint 启动位置活动时将 sendLocation 存储在 StartLatLong 变量中,在从 btnGPSEndPoint 启动位置活动时将 sendLocation 存储在 EndLatLong 变量中。
简而言之,我想使用活动作为方法,以 sendLocation 作为输出
请帮忙...
家庭活动:
btnGPSStartPoint.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
startActivityForResult((new Intent(getApplicationContext(), Location.class)),1);
StartLatLong = getLocation;
}
});
btnGPSEndPoint.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
startActivityForResult((new Intent(getApplicationContext(), Location.class)),1);
EndLatLong = getLocation;
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
LatLng result= data.getParcelableExtra("sendLocation");
getLocation = result;
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
位置活动:
FloatingActionButton SaveLocation = (FloatingActionButton) findViewById(R.id.location);
SaveLocation.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
if (MarkerPosition == null) {
Snackbar.make(view, "Please click on location first", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(Location.this);
builder.setCancelable(true);
builder.setTitle("Location");
builder.setMessage("Latitude : " + MarkerPosition.latitude + "\n" + "Longitude : " + MarkerPosition.longitude);
builder.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent returnIntent = new Intent();
returnIntent.putExtra("sendLocation",MarkerPosition);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
【问题讨论】: