【发布时间】:2018-10-10 11:45:44
【问题描述】:
我正在尝试在我的 Fragment 中覆盖 Spinner 的 OnItemSelected 方法,方法相同我在 Button 上使用了 OnClick,但它不再响应了
在 OnCreate 内部,注释块工作得很好,但我想清理它,因为我在这个布局中有多个微调器,这样它就会变得非常污染。
另一个问题:如果可能的话,这个 OnNothingSelected 方法是如何工作的以及如何更有效地使用它?
表达式中“if(itemSelected!="...")”是因为我的spinner的第一个元素是一个带“....”的字符串,以免一开始就选中任何item,我正在使用这种奇妙的技术...我该如何改进呢?
我的代码如下:
Vetarano2.kt
com.mtsa.escudeiro_rpghelper.fragments
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.Button
import android.widget.Spinner
import android.widget.Toast
import com.mtsa.escudeiro_rpghelper.R
class Veterano2 : Fragment(), View.OnClickListener, AdapterView.OnItemSelectedListener {
private lateinit var spinner: Spinner
private lateinit var button: Button
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val fragView = inflater.inflate(R.layout.fragment_veterano2, container, false)
initViews(fragView)
initListeners()
// SPINNER RAÇA
/*
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val selecionado = parent.getItemAtPosition(position) as String
Toast.makeText(context, "Opção escolhida: $selecionado", Toast.LENGTH_SHORT).show()
}
override fun onNothingSelected(parent: AdapterView<*>) {}
}
*/
return fragView
}
private fun initViews(v: View) {
spinner = v.findViewById(R.id.spinner2)
button = v.findViewById(R.id.button2)
}
private fun initListeners() {
spinner.onItemSelectedListener = this
button.setOnClickListener(this)
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
when (view?.id) {
R.id.spinner2 -> {
val selecionado = parent?.getItemAtPosition(position) as String
Toast.makeText(context, "Opção escolhida: $selecionado", Toast.LENGTH_SHORT).show()
}
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.button2 -> {
Toast.makeText(context, "SALVAR", Toast.LENGTH_SHORT).show()
}
}
}
}
fragment_veterano2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="64dp">
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/array_example"
android:spinnerMode="dropdown" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="Button" />
</LinearLayout>
arrays_diversos.xml
<resources>
<string-array name="array_example">
<item>AAAAA</item>
<item>BBBBB</item>
<item>CCCCC</item>
<item>DDDDD</item>
<item>EEEEE</item>
</string-array>
</resources>
非常感谢!
编辑:重新格式化代码以获得更好的可视化,但问题仍然存在
【问题讨论】:
-
在
onItemSelected()内部尝试匹配R.id.spinner,但微调器的ID 是R.id.fragVet2_spnRaca- 这就是问题 -
对不起,打错了,这里的代码只是我的一个例子,原文有点大,贴在这里。我会正确编辑我的问题。
标签: android android-fragments kotlin