如何更改 Activity 的 onReume 方法的编辑文本值?

How to change edit text value onReume method of an Activity?

我有一个应用程序,我在其中从一个 activity 获取地址字符串并将其发送到另一个 activity。我想设置用户的 To 和 From 位置,所以我有两个编辑文本视图。当用户在另一个 activity 上选择位置时,应该可以在 To 和 From 的编辑文本视图中看到它。我已经完成直到在编辑文本视图中获得值 To location 因为我添加了 activity 的 OnResume 方法并在编辑文本视图中设置了文本。但是当我 return 到我的 activity 时,To 和 From 编辑文本视图都获得相同的位置。当用户接下来选择“到”位置时,它应该在“到编辑文本”中获得另一个位置。

这是怎么回事??

这就是我现在得到的:

ChooseFromMapActivity

public class ChooseFromMapActivity extends AppCompatActivity implements
        LocationListener, GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    private LocationRequest mLocationRequest;
    GoogleMap mGoogleMap;

    private GoogleApiClient mGoogleApiClient;
    boolean mUpdatesRequested = false;
    private LatLng center;
    private LinearLayout markerLayout;
    private Geocoder geocoder;
    private List<Address> addresses;
    private TextView Address;
    double latitude;
    double longitude;
    private GPSTracker gps;
    private LatLng curentpoint;
    private LinearLayout useLocation;
    Intent intent;
    double x, y;
    StringBuilder str;
    String add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_from_map);
        Address = (TextView) findViewById(R.id.textShowAddress);
        markerLayout = (LinearLayout) findViewById(R.id.locationMarker);
        useLocation = (LinearLayout)findViewById(R.id.LinearUseLoc);

        int status = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getBaseContext());

        if (status != ConnectionResult.SUCCESS) { // Google Play Services are
            // not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
                    requestCode);
            dialog.show();

        } else { // Google Play Services are available

            // Getting reference to the SupportMapFragment
            // Create a new global location parameters object
            mLocationRequest = LocationRequest.create();

            /*
             * Set the update interval
             */
            mLocationRequest.setInterval(GData.UPDATE_INTERVAL_IN_MILLISECONDS);

            // Use high accuracy
            mLocationRequest
                    .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

            // Set the interval ceiling to one minute
            mLocationRequest
                    .setFastestInterval(GData.FAST_INTERVAL_CEILING_IN_MILLISECONDS);

            // Note that location updates are off until the user turns them on
            mUpdatesRequested = false;

            /*
             * Create a new location client, using the enclosing class to handle
             * callbacks.
             */
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API).addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).build();

            mGoogleApiClient.connect();
        }

        useLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                intent = new Intent(ChooseFromMapActivity.this,GoSend.class);
                intent.putExtra("address",add);
                startActivity(intent);

            }
        });
    }


    private void stupMap() {
        try {

            mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // Enabling MyLocation in Google Map
            mGoogleMap.setMyLocationEnabled(true);
            mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
            mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
            mGoogleMap.getUiSettings().setCompassEnabled(true);
            mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
            mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);

            gps = new GPSTracker(this);

            gps.canGetLocation();

            latitude = gps.getLatitude();
            longitude = gps.getLongitude();
            curentpoint = new LatLng(latitude, longitude);

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(curentpoint).zoom(19f).tilt(70).build();

            mGoogleMap.setMyLocationEnabled(true);
            mGoogleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));
            // Clears all the existing markers
            mGoogleMap.clear();

            mGoogleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

                @Override
                public void onCameraChange(CameraPosition arg0) {
                    // TODO Auto-generated method stub
                    center = mGoogleMap.getCameraPosition().target;

                    mGoogleMap.clear();
                    markerLayout.setVisibility(View.VISIBLE);

                    try {
                        new GetLocationAsync(center.latitude, center.longitude)
                                .execute();

                    } catch (Exception e) {
                    }
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub
        stupMap();

    }

    private class GetLocationAsync extends AsyncTask<String, Void, String> {

        // boolean duplicateResponse;


        public GetLocationAsync(double latitude, double longitude) {
            // TODO Auto-generated constructor stub

            x = latitude;
            y = longitude;
        }

        @Override
        protected String doInBackground(String... params) {

            try {
                geocoder = new Geocoder(ChooseFromMapActivity.this, Locale.ENGLISH);
                addresses = geocoder.getFromLocation(x, y, 1);
                str = new StringBuilder();
                if (Geocoder.isPresent()) {

                    if ((addresses != null) && (addresses.size() > 0)) {
                        Address returnAddress = addresses.get(0);

                        String localityString = returnAddress.getLocality();
                        String city = returnAddress.getCountryName();
                        String region_code = returnAddress.getCountryCode();
                        String zipcode = returnAddress.getPostalCode();

                        str.append(localityString + "");
                        str.append(city + "" + region_code + "");
                        str.append(zipcode + "");



                    }
                } else {
                }
            } catch (IOException e) {
                Log.e("tag", e.getMessage());
            }
            return null;

        }

        @Override
        protected void onPostExecute(String result) {
            try {
                Address.setText(addresses.get(0).getAddressLine(0)
                        + addresses.get(0).getAddressLine(1) + " ");
                add=addresses.get(0).getAddressLine(0)
                    + addresses.get(0).getAddressLine(1) + " ";

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onProgressUpdate(Void... values) {

        }
    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

}

去发送

public class GoSend extends AppCompatActivity {
    LatLng latLng;
    private GoogleMap mMap;
    MarkerOptions markerOptions;
    LinearLayout ll;
    Toolbar toolbar;
    EditText editTextLocation;
    EditText edtxt_from;
    EditText edtxt_to;
    String stringAddress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gosendlayout);

        setUI();

        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
        }

        }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void setUI() {

        ll = (LinearLayout) findViewById(R.id.LinearLayoutGoSend);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("GO-SEND");



        try {
            if (mMap == null) {
                mMap = ((MapFragment) getFragmentManager().
                        findFragmentById(R.id.map)).getMap();
            }
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            LatLng sydney = new LatLng(-34, 151);
            mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            mMap.setMyLocationEnabled(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

         edtxt_from=(EditText)findViewById(R.id.editText_from);
         edtxt_to=(EditText)findViewById(R.id.editText_to);


        edtxt_from.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i=new Intent(getApplicationContext(),PickLocationActivity.class);
                startActivity(i);
            }
        });

        edtxt_to.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent  i=new Intent(getApplicationContext(),PickLocationActivity.class);
                startActivity(i);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first

        Bundle bundle = getIntent().getExtras();
        if(bundle != null)
            stringAddress  = bundle.getString("address");
        edtxt_from.setText(stringAddress);
        edtxt_to.setText(stringAddress);

    }
}

谁能帮忙...谢谢....

您在 GoSend Activity 的 onResume() 中设置了相同的地址值。

@Override
public void onResume() {
    super.onResume();  // Always call the superclass method first

    Bundle bundle = getIntent().getExtras();
    if(bundle != null)
        stringAddress = bundle.getString("address");
    edtxt_from.setText(stringAddress);
    edtxt_to.setText(stringAddress);
}

也许你应该删除 edtxt_to.setText(stringAddress);