android - 如何关闭 TimePicker 的键盘图标?

标签 android android-timepicker

我有一个时间选择器,一切正常,但问题是我不能让它看起来像设计师想要的那样。现在它看起来像这样: screenshot

我需要在按钮下隐藏这个键盘图标。我该怎么做?它只是一个原型(prototype),所以这里只有它的 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:tag="ww">

  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Time"
    android:textAppearance="?android:attr/textAppearanceLarge" />

  <TimePicker
    android:id="@+id/timePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:amPmBackgroundColor="#6400AA"
    android:numbersSelectorColor="#6400AA" />

  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:orientation="horizontal">

    <Button
      android:id="@+id/button2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Cancel"
      android:textColor="#6400AA" />

    <Button
      android:id="@+id/button1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="OK"
      android:textColor="#6400AA" />

  </LinearLayout>

</LinearLayout>

最佳答案

我刚刚检查了布局检查器中的层次结构并找到了键盘图标。因为纵向和横向层次结构不同,所以我们对此进行了方向检查,还从 Oreo 进行了操作系统检查(从 Oreo 只有这个图标可用),我尝试了这段代码并且工作正常。 PS: Kotlin 代码

    fun hideKeyboardInputInTimePicker(orientation: Int, timePicker: TimePicker)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            try
            {
                if (orientation == Configuration.ORIENTATION_PORTRAIT)
                {
                    ((timePicker.getChildAt(0) as LinearLayout).getChildAt(4) as LinearLayout).getChildAt(0).visibility = View.GONE
                }
                else
                {
                    (((timePicker.getChildAt(0) as LinearLayout).getChildAt(2) as LinearLayout).getChildAt(2) as LinearLayout).getChildAt(0).visibility = View.GONE
                }
            }
            catch (ex: Exception)
            {
            }

        }
    }

这样调用

hideKeyboardInputInTimePicker(this.resources.configuration.orientation, startTimePicker)

关于android - 如何关闭 TimePicker 的键盘图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53645448/

相关文章:

android - 退出对话框 fragment 后如何更新 MainActivity 中的 TextView?

android - 不允许时间选择器选择过去的时间值

android - Room Rxjava Single 作为删除响应

android - 如何获取 ActionBar View ?

android - 有没有办法裁剪 Image/ImageProxy(在传递给 MLKit 的分析器之前)?

android - 具有 Sense UI 的 Android 手机上的日历 ContentProvider URL

android - 在android中从日期选择器和时间选择器中转换和获取值

android - 如何在关闭 android 中的弹出窗口之前在弹出窗口内设置动画 View ?

android - 时间选择器格式

android - 如何在第一次单击时出现edittext上的TimePickerDialog