如何关闭 TimePicker 的键盘图标?
How to turn off keyboard icon at TimePicker?
我有一个时间选择器,一切正常,但问题是我不能让它看起来像设计师想要的那样。现在它看起来像这样:
我需要把这个键盘图标隐藏在按钮下面。我该怎么做?它只是一个原型,所以这里只有它的 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>
尝试使用:
timePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
您也可以在 xml 中使用 android:descendantFocusability="blocksDescendants"
。
据我所知,您无法删除此图标,它是 Android O 上 UI 的一部分。
该图标是为了让手动编辑时间更加明显,因为在以前的版本中,您可以点击时间并通过键盘更改它并不明显
Android N :
Date/Time 选择器带有滚动时钟,改进了与用户的交互以进行输入,但这使 minute selection
更加乏味。滚动时钟在 24 小时模式下占用了大量时间,而使用拇指时这是不可能的查看确切时间。
Android 哦:
Date/Time 选择器(在日历、时钟等中)在左下角有一个小图标(手动输入):键盘。点击它会切换到基于文本的条目,您可以在其中手动输入准确的精确时间。
我刚刚在布局检查器中检查了层次结构并找到了那个键盘图标。因为纵向和横向层次结构不同,所以我们对此进行了方向检查,并且还从 Oreo 进行了 OS 检查(从 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)
好的,这就是我的解决方案。检查布局后,我发现键盘是 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 这将无法正常工作没有了。
我有一个时间选择器,一切正常,但问题是我不能让它看起来像设计师想要的那样。现在它看起来像这样:
我需要把这个键盘图标隐藏在按钮下面。我该怎么做?它只是一个原型,所以这里只有它的 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>
尝试使用:
timePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
您也可以在 xml 中使用 android:descendantFocusability="blocksDescendants"
。
据我所知,您无法删除此图标,它是 Android O 上 UI 的一部分。
该图标是为了让手动编辑时间更加明显,因为在以前的版本中,您可以点击时间并通过键盘更改它并不明显
Android N :
Date/Time 选择器带有滚动时钟,改进了与用户的交互以进行输入,但这使 minute selection
更加乏味。滚动时钟在 24 小时模式下占用了大量时间,而使用拇指时这是不可能的查看确切时间。
Android 哦:
Date/Time 选择器(在日历、时钟等中)在左下角有一个小图标(手动输入):键盘。点击它会切换到基于文本的条目,您可以在其中手动输入准确的精确时间。
我刚刚在布局检查器中检查了层次结构并找到了那个键盘图标。因为纵向和横向层次结构不同,所以我们对此进行了方向检查,并且还从 Oreo 进行了 OS 检查(从 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)
好的,这就是我的解决方案。检查布局后,我发现键盘是 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 这将无法正常工作没有了。