【问题标题】:Android NumberPicker deselect numberAndroid NumberPicker 取消选择号码
【发布时间】:2015-01-18 23:17:04
【问题描述】:

我使用了一个稍微修改过的 NumberPicker,当活动开始时,默认值已经被选中。这很烦人,因为它会调出键盘或调出对话框来剪切/复制/粘贴。我确实指示键盘保持向下,但它仍然被选中,并且会弹出剪切/复制/粘贴对话框。一旦我滚动浏览这些值,该数字就不再被选中并且它正常运行。有谁知道如何将 NumberPicker 设置为在启动时不选择任何内容?

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class NumPicker extends NumberPicker
{
public NumPicker(Context context)
{
    super(context);
}

public NumPicker(Context context, AttributeSet attrs)
{
    super(context, attrs);
    processAttributeSet(attrs);
}

public NumPicker(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    processAttributeSet(attrs);
}

/**
 * Reads the xml, sets the properties
 */
private void processAttributeSet(AttributeSet attrs)
{
    setMinValue(attrs.getAttributeIntValue(null, "min", 0));
    setMaxValue(attrs.getAttributeIntValue(null, "max", 0));
    setValue(4);
}
}

这是它在xml中的使用方式:

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="50dp"
    android:layout_marginEnd="50dp"
    android:layout_marginTop="40dp"
    style="@style/CustomText"
    max="100"
    min="2"

【问题讨论】:

    标签: android numberpicker


    【解决方案1】:

    发生这种情况的原因是 NumberPicker 是您的活动中的第一个(或唯一一个?)可聚焦元素。您可以通过将此属性添加到根视图来将焦点捕获到主布局视图:

    android:focusableInTouchMode="true"
    

    【讨论】:

    • 我在 NumPicker 类和我的活动中尝试了 clearFocus()。两者都没有改变。
    • 是的,它成功了,谢谢。我尝试将焦点转移到文本视图或按钮上,但我猜这做不到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2011-10-25
    • 2018-07-20
    • 1970-01-01
    • 2015-08-09
    • 2020-05-20
    相关资源
    最近更新 更多