【问题标题】:Android hiding keyboard for autocompleteAndroid隐藏键盘自动完成
【发布时间】:2020-06-01 11:47:03
【问题描述】:

当我选择一个自动完成结果时,我试图隐藏键盘。 这是我的自动完成代码。

input_address.setFocusable(false);
input_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Place.Field> fieldList = Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.NAME);
                Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fieldList).build(MainActivity.this);
                startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
            }
        });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AUTOCOMPLETE_REQUEST_CODE && resultCode == RESULT_OK) {

            Place place = Autocomplete.getPlaceFromIntent(data);
            input_address.setText(place.getName());
            latitude = place.getLatLng().latitude;
            longitude = place.getLatLng().longitude;
            departure = "\"" + latitude + "\"" + ":" + "\"" + longitude + "\"";
        }
        if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            Status status = Autocomplete.getStatusFromIntent(data);
            Toast.makeText(getApplicationContext(), status.getStatusMessage(),
                    Toast.LENGTH_SHORT).show();
        }

}

这是我输入地址的 xml 代码

<EditText
        android:id="@+id/input_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:hint="departure"
        android:textSize="20sp"
        android:inputType="text"
        android:imeOptions="actionDone"
        android:onClick="startAutocompleteActivity"
/>

当我点击自动完成结果时,我应该如何将我的代码更改为有键盘? 我尝试使用 IMM 在 OnActivityResult 下添加,但没有成功。请帮忙谢谢。

【问题讨论】:

    标签: android android-softkeyboard googleplacesautocomplete


    【解决方案1】:
    input_address.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<Place.Field> fieldList = Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.NAME);
    
    //Code for hiding keyboard
     final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
    
    
                    Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fieldList).build(MainActivity.this);
                    startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
                }
            });
    

    【讨论】:

    • for (InputMethodManager) getActivity() 我只能找到方法getCallingActivity。
    • getActivity() 是您的活动上下文
    • 我使用 MainActivity.this 而不是 getActivity,但我找不到替代 getView 的东西。因为方法没有出现
    • 你可以使用 v.getParent()
    猜你喜欢
    • 1970-01-01
    • 2014-01-31
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 2023-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    相关资源
    最近更新 更多