我想在我的应用程序中实现Spinner小部件。文本不显示在Spinner的文本框中。当我选择一个项目时,文本也不显示。
我在我的android工作室运行这个项目。创建片段时,文本将显示在微调器小部件的文本框中。当我选择一个项目时,选中的文本会显示在同一个文本框中。
我的项目和这个项目有什么不同?如何解决这个问题?
SleepRecordFragment.kt
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.example.sleeprecorder.R
import com.example.sleeprecorder.databinding.FragmentSleepRecordBinding
import java.util.*
class SleepRecordFragment: Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentSleepRecordBinding.inflate(inflater)
val daysSize = 7
val dayList = mutableListOf<String>()
val calendar = Calendar.getInstance()
dayList.add(
calendar.get(Calendar.YEAR).toString() +
'/' +
calendar.get(Calendar.MONTH).toString() +
'/' +
calendar.get(Calendar.DAY_OF_MONTH).toString()
)
for (i in 0 ..(daysSize - 1) ) {
calendar.add(Calendar.DAY_OF_YEAR, -1)
Log.i("SleepRecordFragment", i.toString())
dayList.add(
calendar.get(Calendar.YEAR).toString() +
'/' +
calendar.get(Calendar.MONTH).toString() +
'/' +
calendar.get(Calendar.DAY_OF_MONTH).toString()
)
}
val adapter = ArrayAdapter(
this.requireContext(),
android.R.layout.simple_spinner_item,
dayList
)
binding.dateSpinner.adapter = adapter
return binding.root
}
}
fragment_sleep_record.xml
...
<Spinner
android:id="@+id/date_spinner"
android:layout_width="413dp"
android:layout_height="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
...
截图试试这个
它为我工作
class SleepRecordFragment : Fragment() {
private var _binding: FragmentSleepRecordBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentSleepRecordBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val daysSize = 7
val dayList = mutableListOf<String>()
val calendar = Calendar.getInstance()
dayList.add(
calendar.get(Calendar.YEAR).toString() +
'/' +
calendar.get(Calendar.MONTH).toString() +
'/' +
calendar.get(Calendar.DAY_OF_MONTH).toString()
)
for (i in 0 until daysSize) {
calendar.add(Calendar.DAY_OF_YEAR, -1)
Log.i("SleepRecordFragment", i.toString())
dayList.add(
calendar.get(Calendar.YEAR).toString() +
'/' +
calendar.get(Calendar.MONTH).toString() +
'/' +
calendar.get(Calendar.DAY_OF_MONTH).toString()
)
}
val adapter = activity?.let {
ArrayAdapter(
it,
android.R.layout.simple_spinner_item,
dayList
)
}
binding.dateSpinner.adapter = adapter
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}