当我触摸AppCompatEditText时键盘不可见

时间:2019-01-19 14:06:14

标签: java android android-studio keyboard

我创建了类ComplexEditText,该类是从AppCompatEditText扩展的。我确定了背景变体:

 setBackground(getResources().getDrawable(R.drawable.backgroundedittext));

我选择了填充:

setPadding((int) (16 * scale), (int) (10 * scale), (int) (17 * scale), (int) (13 * scale));

我创建了清除输入文字的按钮

mClearButtonImage = ResourcesCompat.getDrawable(getResources(), R.mipmap.delete_button, null);

但是问题是,当我触摸自定义视图(从AppCompatEditText扩展)时,键盘无法打开。我尝试过:

setClickable(true)
setFocusable(true)
setShowSoftInputOnFocus(true);
nputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);

我住进AndroidManifes.xml:

android:windowSoftInputMode="adjustResize" 

它也不起作用。键盘没有出现。

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.royallogistics.yegor.royallogistics">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <application
        android:name=".service.AppChannel"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".LoginActivity" />
        <activity android:name=".SubordersDinamicsFields"
            android:windowSoftInputMode="stateVisible">

        </activity>

ComplexEditText.java

package com.royallogistics.yegor.royallogistics.RestyleView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.royallogistics.yegor.royallogistics.R;
import com.royallogistics.yegor.royallogistics.service.DependentFields;

import java.util.List;

@SuppressLint("ViewConstructor")
public class ComplexEditText extends AppCompatEditText {

    // { НЕОБХОДИМЫЕ ПОЛЯ ДЛЯ УПРАВЛЕНИЯ }
    private Integer mIndex_on_the_view;
    private Integer mField_id;
    @Nullable
    private Integer mContainer_id;
    @Nullable
    private List<DependentFields> mActions;

    Drawable mClearButtonImage;
    EditText text;
    float scale = getResources().getDisplayMetrics().density;

    void init() {
        mClearButtonImage = ResourcesCompat.getDrawable(getResources(), R.mipmap.delete_button, null);
        setBackground(getResources().getDrawable(R.drawable.backgroundedittext));
        setPadding((int) (16 * scale), (int) (10 * scale), (int) (17 * scale), (int) (13 * scale));
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
        setShowSoftInputOnFocus(true);


        // TODO: if the clear (X) button is tapped, clear the text
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.e("focus", getFullName() + " _ " + String.valueOf(getShowSoftInputOnFocus()));

                if ((getCompoundDrawablesRelative()[2] != null)) {
                    float clearButtonStart; // Used for LTR languages
                    float clearButtonEnd;  // Used for RTL languages
                    boolean isClearButtonClicked = false;
                    // TODO: Detect the touch in RTL or LTR layout direction.
                    if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
                        // If RTL, get the end of the button on the left side.
                        clearButtonEnd = mClearButtonImage
                                .getIntrinsicWidth() + getPaddingStart();
                        // If the touch occurred before the end of the button,
                        // set isClearButtonClicked to true.
                        if (event.getX() < clearButtonEnd) {
                            isClearButtonClicked = true;
                        }
                    } else {
                        // Layout is LTR.
                        // Get the start of the button on the right side.
                        clearButtonStart = (getWidth() - getPaddingEnd()
                                - mClearButtonImage.getIntrinsicWidth());
                        // If the touch occurred after the start of the button,
                        // set isClearButtonClicked to true.
                        if (event.getX() > clearButtonStart) {
                            isClearButtonClicked = true;
                        }
                    }
                    // TODO: Check for actions if the button is tapped.

                    if (isClearButtonClicked) {
                        // Check for ACTION_DOWN (always occurs before ACTION_UP).
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            // Switch to the black version of clear button.
                            mClearButtonImage =
                                    ResourcesCompat.getDrawable(getResources(),
                                            R.mipmap.delete_button, null);
                            showClearButton();
                        }
                        // Check for ACTION_UP.
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            // Switch to the opaque version of clear button.
                            mClearButtonImage =
                                    ResourcesCompat.getDrawable(getResources(),
                                            R.mipmap.delete_button, null);
                            // Clear the text and hide the clear button.
                            getText().clear();
                            hideClearButton();
                            return true;
                        }
                    } else {
                        return false;
                    }
                }
                return false;
            }
        });
        // TODO: if the changes, show or hide the clear (X) button
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                showClearButton();
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return super.onCreateInputConnection(outAttrs);
    }

    public ComplexEditText(Context context, Integer mIndex_on_the_view, Integer mField_id, @Nullable Integer mContainer_id, @Nullable List<DependentFields> mActions) {
        super(context);
        this.mIndex_on_the_view = mIndex_on_the_view;
        this.mField_id = mField_id;
        this.mContainer_id = mContainer_id;
        this.mActions = mActions;
        init();

    }

    public ComplexEditText(Context context, @Nullable AttributeSet attrs, Integer mIndex_on_the_view, Integer mField_id, @Nullable Integer mContainer_id, @Nullable List<DependentFields> mActions) {
        super(context, attrs);
        this.mIndex_on_the_view = mIndex_on_the_view;
        this.mField_id = mField_id;
        this.mContainer_id = mContainer_id;
        this.mActions = mActions;
        init();


    }

    public ComplexEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr, Integer mIndex_on_the_view, Integer mField_id, @Nullable Integer mContainer_id, @Nullable List<DependentFields> mActions) {
        super(context, attrs, defStyleAttr);
        this.mIndex_on_the_view = mIndex_on_the_view;
        this.mField_id = mField_id;
        this.mContainer_id = mContainer_id;
        this.mActions = mActions;
        init();

    }


    private void setupAttributes(AttributeSet attributeSet) {
        @SuppressLint("CustomViewStyleable") TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.ComplexEditTextattr);
        typedArray.recycle();
    }

    String getFullName() {
        if (mContainer_id != null) {
            Log.e("Полное название", "field_" + String.valueOf(mField_id) + "_" + String.valueOf(mContainer_id));
            return "field_" + String.valueOf(mField_id) + "_" + String.valueOf(mContainer_id);

        } else {
            Log.e("Полное название", "field_" + String.valueOf(mField_id));
            return "field_" + String.valueOf(mField_id);

        }
    }

    void clearField() {
        setText("");
    }

    Integer getValueInteger() {
        return Integer.valueOf(getText().toString());
    }

    String getValueString() {
        return String.valueOf(getText().toString());
    }

    private void showClearButton() {

        setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, mClearButtonImage, null);
    }

    private void hideClearButton() {
        setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, null, null);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

}

screenshot with ComplexEditText

1 个答案:

答案 0 :(得分:0)

我在构造函数中添加以下行:

clearFocus();

,然后在onTouch方法中添加以下代码:

                requestFocus();
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);