【问题标题】:How to reference a timerPicker in android studio using kotlin如何使用 kotlin 在 android studio 中引用 timerPicker
【发布时间】:2019-08-16 16:39:06
【问题描述】:

当这个程序在模拟器上运行时,它可以工作,但是,当我在手机上运行它时,timePicker 被引用为空。

运行 API 26 的模拟器可以正常运行,但是在 API 28 上它无法运行。所以我认为这可能与 API 28 有关(这也是我的手机运行的)

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val picker = findViewById<TimePicker>(R.id.timePicker)

        val t = picker == null // Is true on phone and is false on emulator

        Toast.makeText(this@MainActivity, t.toString(), Toast.LENGTH_LONG).show()
        //picker.setIs24HourView(true)

        val activity = findViewById<EditText>(R.id.activityName)
        val mCallApiButton = findViewById<Button>(R.id.submit)
        mCallApiButton?.setOnClickListener{
            val time = GetTime(picker)

            PostActivity(time, activity.text.toString()) // Function that does other stuff
        }
    }
}

我需要引用 timePicker 以便将其设置为 24H。

下面是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent">
        <TimePicker android:id="@+id/timePicker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:timePickerMode="clock"/>
        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:id="@+id/activityName"
                android:singleLine="true" android:hint="Activity" android:textSize="18sp"
                android:textAlignment="center" android:autofillHints="Activity"/>
        <Button
                android:text="Submit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" android:id="@+id/submit"/>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

模拟器的屏幕截图:(应该是这样的)

手机截图:

【问题讨论】:

  • 请检查您的设备设置是否与模拟器相同
  • 尝试清理重建您的项目并再次检查,手机和模拟器之间的情况必须没有区别
  • @Shalu T D 你的意思是操作系统版本,还是android中的实际设置?
  • @Dmitriy Pavlukhin 这没什么区别
  • 你有什么手机?时间选择器在活动布局中是否可见?

标签: android android-studio kotlin emulation release


【解决方案1】:

这是因为我使用 API 28 而不是 27 及以下。答案可以在这里找到: https://github.com/xamarin/Xamarin.Forms/issues/5159

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多