GooglePlacesAutocomplete将同一地址返回到两个文字视图 - 您的起点和目的地

时间:2016-06-17 20:09:08

标签: java android

我是StackOverflow的新手,所以对任何错误道歉。我坚持的问题是,每当我在我的Android应用程序中使用GooglePlacesAutocomplete Activity时,当我输入地址时,它会填充textviews.While我希望它以某种方式工作,每当我点击"你的起点&# 34;这会将我带到placesautocomplete活动,而输入的地址应该只在"你的起点" textview然后与"你的目的地"。

类似

那么怎么做呢?还有如何获得"今天"或者"明天"而不是" date-month-year"在Datetextview中。我还附上截图以便更好地理解。

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

    eet1 = (TextView) findViewById(R.id.eet1);
    eet2 = (TextView) findViewById(R.id.eet2);

    eet1.setOnClickListener(this);
    eet2.setOnClickListener(this);

    // Open the autocomplete activity when the TextView is clicked.
    TextView openTextview = (TextView) findViewById(R.id.eet1);
    assert openTextview != null;
    openTextview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == eet1)
            openAutocompleteActivity();
        }

    });


    // Open the autocomplete activity when the TextView is clicked.
    TextView openTextView = (TextView) findViewById(R.id.eet2);
    assert openTextView != null;
    openTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == eet2)
            openAutocompleteActivity();
        }

    });

    timetext = (TextView) findViewById(R.id.timetext);
    datetext = (TextView) findViewById(R.id.datetext);

    timetext.setOnClickListener(this);
    datetext.setOnClickListener(this);

}

protected void openAutocompleteActivity() {
    try {
        // The autocomplete activity requires Google Play Services to be available. The intent
        // builder checks this and throws an exception if it is not the case.
        Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                .build(this);
        startActivityForResult(intent, 1);
    } catch (GooglePlayServicesRepairableException e) {
        // Indicates that Google Play Services is either not installed or not up to date. Prompt
        // the user to correct the issue.
        GoogleApiAvailability.getInstance().getErrorDialog(this, e.getConnectionStatusCode(),
                0 /* requestCode */).show();
    } catch (GooglePlayServicesNotAvailableException e) {
        // Indicates that Google Play Services is not available and the problem is not easily
        // resolvable.
        String message = "Google Play Services is not available: " +
                GoogleApiAvailability.getInstance().getErrorString(e.errorCode);

        Log.e(TAG, message);
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

/**
 * Called after the autocomplete activity has finished to return its result.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Check that the result was from the autocomplete widget.
    if (requestCode == REQUEST_CODE_AUTOCOMPLETE) {
        if (resultCode == RESULT_OK) {
            // Get the user's selected place from the Intent.
            Place place = PlaceAutocomplete.getPlace(this, data);
            Log.e(TAG, "Place: " + place.getAddress());

            assert findViewById(R.id.eet1) != null;
            ((TextView) findViewById(R.id.eet1))
                    .setText(place.getName() + ",\n" +
                            place.getAddress());


        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
            Status status = PlaceAutocomplete.getStatus(this, data);
            Log.e(TAG, "Error: Status = " + status.toString());
        } else if (resultCode == RESULT_CANCELED) {
            // Indicates that the activity closed before a selection was made. For example if
            // the user pressed the back button.
        }
    }

    // Check that the result was from the autocomplete widget.
    if (requestCode == REQUEST_CODE_AUTOCOMPLETE) {
        if (resultCode == RESULT_OK) {
            // Get the user's selected place from the Intent.
            Place place = PlaceAutocomplete.getPlace(this, data);
            Log.e(TAG, "Place: " + place.getAddress());

            assert findViewById(R.id.eet2) != null;
            ((TextView) findViewById(R.id.eet2))
                    .setText(place.getName() + ",\n" +
                            place.getAddress());


        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
            Status status = PlaceAutocomplete.getStatus(this, data);
            Log.e(TAG, "Error: Status = " + status.toString());
        } else if (resultCode == RESULT_CANCELED) {
            // Indicates that the activity closed before a selection was made. For example if
            // the user pressed the back button.
        }
    }
}

@Override
public void onClick(View v) {
    if (v == datetext) {

        // Process to get Current Date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        // Launch Date Picker Dialog
        DatePickerDialog dpd = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        // Display Selected date in textbox
                        datetext.setText(dayOfMonth + "-"
                                + (monthOfYear + 1) + "-" + year);

                    }
                }, mYear, mMonth, mDay);
        dpd.show();
    }
    if (v == timetext) {

        // Process to get Current Time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // Launch Time Picker Dialog
        TimePickerDialog tpd = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                                          int minute) {
                        // Display Selected time in textbox
                        timetext.setText(hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        tpd.show();
    }
}

1 个答案:

答案 0 :(得分:0)

由于你的 onActivityResult 方法,你遇到了问题,你为两个TextView编写了代码。因此,每当用户选择该位置时,两个TextView都会同时填充。