手机的键盘隐藏了我的“底端对话框”片段。我该如何解决这个问题?

问题描述 投票:0回答:1

我正在用Kotlin为Android制作一个小型任务程序。为了创建任务,我添加了一个名为CreateTaskFragment的片段,该片段扩展了BottomSheetDialogFragment()。当片段弹出时,我需要将键盘聚焦在editText上并位于孔CreateTaskFragment的正下方。我已经尝试过不同的解决方案,但是没有任何帮助,这就是为什么我需要您的帮助。这是一些代码:

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>

谢谢您!

enter image description hereenter image description here

android kotlin android-keypad
1个回答
0
投票

在manifest.xml文件中。

 <activity
        android:name=".ActivityName"
        android:windowSoftInputMode="stateHidden|adjustPan" />
© www.soinside.com 2019 - 2024. All rights reserved.