首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GooglePlacesAutocomplete将相同的地址返回给文本视图--您的起点和目的地。

GooglePlacesAutocomplete将相同的地址返回给文本视图--您的起点和目的地。
EN

Stack Overflow用户
提问于 2016-06-17 20:09:08
回答 1查看 78关注 0票数 0

我是StackOverflow的新手,所以对任何错误都表示歉意。我遇到的问题是,每当我在我的安卓应用程序中使用GooglePlacesAutocomplete活动时,当我键入地址时,它都填充了我希望它工作的两个textviews.While,每当我单击"your“(它带我去click自动完成的活动)时,输入的地址应该只在”您的起始点“文本视图中填写,然后与”您的目的地“类似。

那么如何做呢?以及如何在Datetextview.I中获得“今天”或“明天”,而不是“日期-月-年”,我还附上了屏幕截图,以便更好地理解。

代码语言:javascript
复制
@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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-08 05:03:10

由于在onActivityResult方法中出现问题,您为两个TextView编写了代码。因此,每当用户选择位置时,两个TextViews都同时被填充。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37889639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档