Android的Kotlin:OnItem选定的侦听器

时间:2019-06-14 20:40:19

标签: android kotlin spinner

我正在用Kotlin开发一个android项目

我想在侦听器外部获取x和y的值,并将乘法值打印在textview gpa1上,但它们没有改变

class sem1gpaactivity : AppCompatActivity(){
var x:Int=0
var y:Double=0.0
var z:Double=0.0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.sem1gpa)
val firstterm01 = arrayOf("0", "1", "2", "3", "4", "5", "6", "7", "8")


val spinner = findViewById<View>(R.id.spinnerfirsttermh1) as Spinner
val spinnerArrayAdapter = ArrayAdapter(this, 
android.R.layout.simple_spinner_item, firstterm01)

spinnerArrayAdapter.setDropDownViewResource
  (android.R.layout.simple_spinner_dropdown_item) // The drop down view
spinner.adapter = spinnerArrayAdapter
spinner.spinnerfirsttermh1!!.onItemSelectedListener = object : 
AdapterView.OnItemSelectedListener {
   override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: 
Long) {

      when (p2){

          0->  x=0
          1->  x=1
          2->  x=2
          3->  x=3
          4->  x=4
          5->  x=5
          6->  x=6
          7->  x=7
          8->  x=8

      }

   }

    override fun onNothingSelected(p0: AdapterView<*>?) {}
}

val firstterm02 = arrayOf("A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "F")
val spinner1 = findViewById<View>(R.id.spinnerfirsttermh11) as Spinner
val spinnerArrayAdapter1 = ArrayAdapter(this, android.R.layout.simple_spinner_item, firstterm02)
spinnerArrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) // The drop down view
spinner1.adapter = spinnerArrayAdapter1
spinner1.spinnerfirsttermh11!!.onItemSelectedListener = object : 
AdapterView.OnItemSelectedListener {
    override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, 
p3: Long) {
        val spinnerLanguage2 = spinner1.getSelectedItem().toString()
        when (p2){
            0->  y=4.0
            1->  y=3.7
            2->  y=3.3
            3->  y=3.0
            4->  y=2.7
            5->  y=2.3
            6->  y=2.0
            7->  y=1.7
            8->  y=1.3
            9->  y=1.0
            10-> y=0.0
        }
                          }
    override fun onNothingSelected(p0: AdapterView<*>?) {}
}
z=x*y
gpa1.text = z.toString()
}}

z结果为零,因为x和y不变,并且文本视图显示000 我试图将textiew代码放在第一个微调器的侦听器中的乐趣中,并且只显示x起作用,但是当我将textview代码放在侦听器之外时,它什么也不显示

1 个答案:

答案 0 :(得分:0)

文本不会像这样更改,因为除非您销毁该活动并再次创建它,否则该应用只会在第一次创建该活动时才执行一次oncreate方法,因此您可以通过在两个微调监听器中更改gpa1文本来执行此操作,像这样:

在拳头监听器中

spinner.spinnerfirsttermh1!!.onItemSelectedListener = object :
        AdapterView.OnItemSelectedListener {
        override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3:
        Long) {

            when (p2){

                0->  x=0
                1->  x=1
                2->  x=2
                3->  x=3
                4->  x=4
                5->  x=5
                6->  x=6
                7->  x=7
                8->  x=8

            }
            z=x*y
            tv.text = z.toString()

        }

        override fun onNothingSelected(p0: AdapterView<*>?) {}
    }

在第二个微调监听器中

spinner1.spinnerfirsttermh11!!.onItemSelectedListener = object :
        AdapterView.OnItemSelectedListener {
        override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int,
                                    p3: Long) {
            val spinnerLanguage2 = spinner1.getSelectedItem().toString()
            when (p2){
                0->  y=4.0
                1->  y=3.7
                2->  y=3.3
                3->  y=3.0
                4->  y=2.7
                5->  y=2.3
                6->  y=2.0
                7->  y=1.7
                8->  y=1.3
                9->  y=1.0
                10-> y=0.0
            }
            z=x*y
            tv.text = z.toString()
        }
        override fun onNothingSelected(p0: AdapterView<*>?) {}
    }