Android EditText не виден и не показывает клавиатуру - PullRequest
1 голос
/ 17 февраля 2020

Используя следующие макеты и представления ниже, я не могу заставить окно EditText et_messageText правильно отображаться на моем дисплее. Если я запускаю это в эмуляторе, я могу щелкнуть мышью в области и получить «нулевой» курсор выбора, поэтому я знаю, что EditText есть, но клавиатура никогда не отображается, и я не могу заставить работать ввод. Что я делаю не так?

Activity_main. xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="viewModel"
            type="com.example.project.model.MyViewModel" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.project.MainActivity">

        <com.example.project.view.ChannelHeaderView
            android:id="@+id/channelHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" />

        <com.example.project.view.MessageInputView
            android:id="@+id/message_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</layout>

message_input. xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="viewModel"
            type="com.example.project.model.MyViewMOdel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center_vertical"
        android:orientation="horizontal">


        <ImageView
            android:id="@+id/iv_send"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="13dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:srcCompat="@drawable/stream_ic_send" />

        <EditText
            android:id="@+id/et_messageText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="112dp"
            android:ems="10"
            android:inputType="text"
            android:text="@={viewModel.inputText}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/iv_send"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

MessageInputView. java

public class MessageInputView extends RelativeLayout {

    private MessageInputBinding binding;
    private MyViewModel viewModel;
    public MessageInputView(Context context) {
        super(context);
        binding = initBinding(context);
    }
    public MessageInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        binding = initBinding(context);
        // Focus Listening
        binding.etMessageText.setOnFocusChangeListener((v, hasFocus) -> {

            if (hasFocus) {
                binding.etMessageText.setTextIsSelectable(true);
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(v,InputMethodManager.SHOW_FORCED);
            }
        });
    }

    private MessageInputBinding initBinding(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        binding = MessageInputBinding.inflate(inflater, this, true);
        binding.etMessageText.clearFocus();
        return binding;
    }

    public void setViewModel(MyViewMOdel model, LifecycleOwner lifecycleOwner) {
        this.viewModel = model;
        binding.setLifecycleOwner(lifecycleOwner);
        binding.setViewModel(viewModel);
    }
}

и, наконец, onCreate() из MainActivity. java

binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setLifecycleOwner(this);

viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
viewModel.setPreferences(this);
binding.setViewModel(viewModel);
binding.channelHeader.setViewModel(viewModel, this);
binding.messageInput.setViewModel(viewModel, this);

1 Ответ

0 голосов
/ 17 февраля 2020

Почему вы расширяете свой MessageInputView класс на RelativeLayout? Я предполагаю, что ваш класс MessageInputView является Fragment, поэтому расширьте его на Fragment и используйте значения по умолчанию LayoutInflater inflater и ViewGroup container.

Следовательно, ваш:

private MessageInputBinding initBinding(Context context) {
    LayoutInflater inflater = LayoutInflater.from(context);
    binding = MessageInputBinding.inflate(inflater, this, true);
    binding.etMessageText.clearFocus();
    return binding;
}

Должно быть так:

private MessageInputBinding initBinding(LayoutInflater inflater, ViewGroup container) {
    binding = DataBindingUtil.inflate(inflater, R.layout.message_input, container, false);
    binding.etMessageText.clearFocus();
    return binding;
}
...