如何关闭TimePicker的键盘图标?

问题描述 投票:0回答:6

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

我需要将此键盘图标隐藏在按钮下方。我该怎么做?它只是一个原型,所以这里只是它的 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>
android android-timepicker
6个回答
6
投票

我刚刚检查了布局检查器中的层次结构,发现了键盘图标。对于纵向和横向层次结构是不同的,因此我们对此进行了方向检查,并且还对 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)

3
投票

据我所知,您无法删除此图标,它是 Android O 上 UI 的一部分。

该图标的存在是为了使手动编辑时间更加明显,因为在以前的版本中,您可以单击时间并通过键盘更改它并不明显


1
投票

好吧,这是我的解决方案。检查布局后,我发现键盘是

AppCompatImageButton
。所以你只需对
TimePicker
进行递归,这是一个
Viewgroup
。所以你可以这样称呼它:

private fun initTimePicker() {
    timePicker.setIs24HourView(true)
    setImageButtonToGone(timePicker)
}

private fun setImageButtonToGone(viewGroup: ViewGroup) {
    for (i in 0 until viewGroup.childCount) {
        val child = viewGroup.getChildAt(i)
        if (child is LinearLayout) {
            setImageButtonToGone(child)
        } else if (child is AppCompatImageButton) {
            child.visibility = View.GONE
        }
    }
}

您将获得所需的布局。

警告!!! 这很大程度上取决于键盘按钮是唯一的

AppCompatImageButton
如果他们更改了 TimePicker 的 XML,这将不再正常工作。


0
投票

尝试使用:

timePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);

您还可以在 xml 中使用

android:descendantFocusability="blocksDescendants"


0
投票

安卓N:

日期/时间选择器带有滚动时钟,改善了与用户进行输入的交互,但这使得

minute selection
更加乏味。滚动时钟在 24 小时模式下会消耗大量时间,而使用拇指时无法看到准确的时间。

安卓O:

日期/时间选择器(日历、时钟等)在左下角有一个小图标(手动输入):键盘。点击它会切换到基于文本的输入,您可以在其中手动输入准确的时间。


0
投票

我觉得我已经迟到了,但是删除键盘图标是可以完成的。 这是我的 Java 代码。

(有点笨拙,但是嗯,它有效)

schedule_Clock_1 = findViewById(R.id.clock_1);
    
//Make the keyboard GONE
LinearLayout ct = (LinearLayout) schedule_Clock_1 .getChildAt(0); 
// retrieves the entire clock layout in LinearLayout form
LinearLayout cta= (LinearLayout) ct.getChildAt(4); 
// retrieves child 4 of the clock layout which is the keyboard
cta.setVisibility(View.GONE); 
// makes the keyboard GONE, INVISIBLE makes the item still there, just not seen. It can still be clicked
© www.soinside.com 2019 - 2024. All rights reserved.