简体   繁体   中英

The phone's keyboard is hiding my Bottom Sheet Dialog Fragment. How can i solve this issue?

I'm making a small tasker for android with kotlin. To create a task i added a fragment called CreateTaskFragment which extends BottomSheetDialogFragment() . When the fragment pops up i need the keyboard to be focused on editText and located just under the hole CreateTaskFragment . I've already tried different solutions, but nothing passed to me, that's why i need your help. Here is some code:

class CreateTaskFragment : BottomSheetDialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        // Get a reference to the binding object and inflate the fragment views.
        val binding: FragmentBottomSheetBinding = DataBindingUtil.inflate(
            inflater, R.layout.fragment_bottom_sheet, container, false)

        val application = requireNotNull(this.activity).application

        // Create an instance of the ViewModel Factory.
        val dataSource = TaskDatabase.getInstance(application).taskDatabaseDao
        val viewModelFactory = CreateTaskViewModelFactory(dataSource)

        // Get a reference to the ViewModel associated with this fragment.
        val createTaskViewModel =
            ViewModelProviders.of(
                this, viewModelFactory).get(CreateTaskViewModel::class.java)

        var date: LocalDate? = null

        // To use the View Model with data binding, you have to explicitly
        // give the binding object a reference to it.
        binding.createTaskViewModel = createTaskViewModel
        binding.setLifecycleOwner(this)

        // Set the focus to the edit text.
        binding.editText.requestFocus()

        binding.datePicker.setOnClickListener {
            val datePickerDialog = DatePickerDialog(it.context, R.style.DatePickerTheme)
            datePickerDialog.setOnDateSetListener { view, year, month, dayOfMonth ->
                date = LocalDate.of(year, month + 1, dayOfMonth)
            }
            datePickerDialog.show()
        }

        binding.saveButton.setOnClickListener {
            val title = binding.editText.text.toString()
            createTaskViewModel.createTask(title, "", date)

            this.dismiss()
        }

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

    <data>
        <variable
            name="createTaskViewModel"
            type="com.oleksii.routinetracker.createtask.CreateTaskViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:background="@android:color/transparent"
            android:ems="17"
            android:fontFamily="@font/noto_sans"
            android:hint="@string/new_task"
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/details_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginBottom="24dp"
            android:tint="@color/colorBlue500"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText"
            app:srcCompat="@drawable/more_details" />

        <ImageView
            android:id="@+id/date_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:tint="@color/colorBlue500"
            app:layout_constraintBottom_toBottomOf="@+id/details_add"
            app:layout_constraintStart_toEndOf="@+id/details_add"
            app:layout_constraintTop_toTopOf="@+id/details_add"
            app:srcCompat="@drawable/date_ico" />

        <Button
            android:id="@+id/save_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:background="?android:attr/selectableItemBackground"
            android:fontFamily="@font/noto_sans"
            android:text="@string/save_button"
            android:textColor="@color/colorBlue500"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@+id/details_add"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/details_add" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Thank you in advance!

在此处输入图像描述 在此处输入图像描述

In manifest.xml file.

 <activity
        android:name=".ActivityName"
        android:windowSoftInputMode="stateHidden|adjustPan" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM