2016-03-01 47 views
1

我有2個編輯文本輸入在我的app.One是從地方和另一個place.For這2輸入我已經實現谷歌自動完成活動,但用戶點擊時的問題這些輸入中的任何一個在用戶地點選擇完成意圖調用onActivityResult()後輸入google autocomplete意圖。 在onActivityResult()中如何知道哪個輸入被點擊。 這裏是我的代碼Android應用程序實現谷歌自動完成活動2輸入

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

        try { 
         AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
           .setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS).setTypeFilter(AutocompleteFilter.TYPE_FILTER_GEOCODE) 
           .build(); 
         Intent intent = 
           new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).setFilter(typeFilter) 
             .build(MainActivity.this); 
         startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); 

        } catch (GooglePlayServicesRepairableException e) { 
         // TODO: Handle the error. 
        } catch (GooglePlayServicesNotAvailableException e) { 
         // TODO: Handle the error. 
        } 
       } 
      }); 
toPlaceEdit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       try { 
        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
          .setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS).setTypeFilter(AutocompleteFilter.TYPE_FILTER_GEOCODE) 
          .build(); 
        Intent intent = 
          new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).setFilter(typeFilter) 
            .build(MainActivity.this); 
        startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); 

       } catch (GooglePlayServicesRepairableException e) { 
        // TODO: Handle the error. 
       } catch (GooglePlayServicesNotAvailableException e) { 
        // TODO: Handle the error. 
       } 
      } 
     }); 
@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 

       Place place = PlaceAutocomplete.getPlace(this, data); 

       String fullName= (String) place.getAddress(); 
       #// here I want to know which input clicked and set fullname in input text# 
       fromPlaceEdit.setText(fullName); 
      } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 
       Status status = PlaceAutocomplete.getStatus(this, data); 
       // TODO: Handle the error. 
       Log.i("fdfdf", status.getStatusMessage()); 

      } else if (resultCode == RESULT_CANCELED) { 
       // The user canceled the operation. 
      } 
     } 
    } 
+0

可能重複:http://stackoverflow.com/questions/7495909/how-to-know-which-intent-is -selected-in-intent-action-send – abielita

+1

調用startActivityForResult時使用不同的請求代碼。不要忘記也改變onActivityResult。 – Fraid

回答

1

我做了不同的方式。 我已經爲每個地方創建了兩個全局布爾值。 然後當用戶從地點點擊或放置更改布爾值相同的地方,然後onActivityResult檢查哪個布爾值爲true並將結果地點名稱設置爲相同的輸入框。 代碼如下:

public boolean fromplaceSlected =false ; 
    public boolean toplaceSlected =false; 
fromPlaceEdit.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        try { 
         AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
           .setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS).setTypeFilter(AutocompleteFilter.TYPE_FILTER_GEOCODE) 
           .build(); 
         Intent intent = 
           new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).setFilter(typeFilter) 
             .build(MainActivity.this); 
         startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); 

        } catch (GooglePlayServicesRepairableException e) { 
         // TODO: Handle the error. 
        } catch (GooglePlayServicesNotAvailableException e) { 
         // TODO: Handle the error. 
        } 
       } 
      }); 
toPlaceEdit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       try { 
        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
          .setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS).setTypeFilter(AutocompleteFilter.TYPE_FILTER_GEOCODE) 
          .build(); 
        Intent intent = 
          new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).setFilter(typeFilter) 
            .build(MainActivity.this); 
        startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); 

       } catch (GooglePlayServicesRepairableException e) { 
        // TODO: Handle the error. 
       } catch (GooglePlayServicesNotAvailableException e) { 
        // TODO: Handle the error. 
       } 
      } 
     }); 
@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Log.e("request", String.valueOf(requestCode)); 
     // 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(getContext(), data); 

       String fullName = (String) place.getAddress(); 

       if(fromplaceSlected){ 
        Log.e("from place",fullName); 
        fromPlaceEdit.setText(fullName); 
        fromplaceSlected=false; 
        toplaceSlected=false; 
       } 
       else if(toplaceSlected){ 
        Log.e("toplace" ,fullName); 
        toPlaceEdit.setText(fullName); 
        fromplaceSlected=false; 
        toplaceSlected=false; 
       } 

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

    } 
1

您的代碼可能會工作,但它有點hacky。這裏是個好辦法做到這一點:

public static final int PLACE_AUTOCOMPLETE_FROM_PLACE_REQUEST_CODE=1; 
    public static final int PLACE_AUTOCOMPLETE_TO_PLACE_REQUEST_CODE=2; 

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

        try { 
         //Do your stuff from place 
         startActivityForResult(intent, PLACE_AUTOCOMPLETE_FROM_PLACE_REQUEST_CODE); 

        } catch (GooglePlayServicesRepairableException e) { 
         // TODO: Handle the error. 
        } catch (GooglePlayServicesNotAvailableException e) { 
         // TODO: Handle the error. 
        } 
       } 
      }); 
toPlaceEdit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       try { 
        //Do your stuff to place 
        startActivityForResult(intent, PLACE_AUTOCOMPLETE_TO_PLACE_REQUEST_CODE); 

       } catch (GooglePlayServicesRepairableException e) { 
        // TODO: Handle the error. 
       } catch (GooglePlayServicesNotAvailableException e) { 
        // TODO: Handle the error. 
       } 
      } 
     }); 
@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == PLACE_AUTOCOMPLETE_FROM_PLACE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       //Do your ok >from place< stuff here 
      } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 
       //Handle your error >from place< 
      } else if (resultCode == RESULT_CANCELED) { 
       // The user canceled the operation. 
      } 
     } else if (requestCode == PLACE_AUTOCOMPLETE_TO_PLACE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       //Do your ok >to place< stuff here 
      } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 
       //Handle your error >to place< 
      } else if (resultCode == RESULT_CANCELED) { 
       // The user canceled the operation. 
      } 
     } 
    } 

從技術文檔: requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits.

+0

謝謝你的幫助 – Murali