Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 禁用NumberPicker上的软键盘_Android_Android Layout_Android Widget_Android Edittext_Android Softkeyboard - Fatal编程技术网

Android 禁用NumberPicker上的软键盘

Android 禁用NumberPicker上的软键盘,android,android-layout,android-widget,android-edittext,android-softkeyboard,Android,Android Layout,Android Widget,Android Edittext,Android Softkeyboard,我试图在使用NumberPicker输入数值时禁用软键盘(出于美观原因)。这是我的布局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

我试图在使用NumberPicker输入数值时禁用软键盘(出于美观原因)。这是我的布局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" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="30dp" >

        <NumberPicker
            android:id="@+id/repetitionPicker"
            android:layout_width="40dp"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/repetitions_short_divider"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <NumberPicker
            android:id="@+id/weightPicker"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/pounds"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>


    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/save" />

</LinearLayout>

不幸的是,当点击数字键盘时,软键盘仍然会出现。有什么想法吗?

在阅读com/android/internal/widget/NumberPicker.java源代码后,我得到了以下解决方案:

// Hide soft keyboard on NumberPickers by overwriting the OnFocusChangeListener
OnFocusChangeListener fcl = new OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        // Do nothing to suppress keyboard
    }
};

((EditText) numberPicker.getChildAt(1)).setOnFocusChangeListener(fcl);

// Suppress soft keyboard from the beginning
((EditText) numberPicker.getChildAt(1)).setInputType(InputType.TYPE_NULL);

刚刚增强了@MaxVogler的ans(因此,如果不想也投票给@MaxVogler),使其成为一个强大的黑客。另外,我们不需要调用
setonfocuschangeenner
setinputype
。只有
setFocusable
设置为false才行

下面是用于启用/禁用该功能的帮助器api

public static void enableNumberPickerManualEditing(NumberPicker numPicker,
        boolean enable) {
    int childCount = numPicker.getChildCount();

    for (int i = 0; i < childCount; i++) {
        View childView = numPicker.getChildAt(i);

        if (childView instanceof EditText) {
            EditText et = (EditText) childView;
            et.setFocusable(enable);
            return;
        }
    }
}
public static void enableNumberPickerManualEditing(NumberPicker-numPicker,
布尔值(启用){
int childCount=numPicker.getChildCount();
for(int i=0;i
刚找到这个,它就像一个符咒:

myNumberPicker.setDescendantFocusability(NumberPicker.FOCUS\u BLOCK\u后代)

您还可以在XML中设置:

android:descendantFocusability="blocksDescendants" 

这里有另一种方法,它允许用户仍然可以编辑一个数字,如果他们想-它只是抑制软键盘最初。根据上述答案,当界面第一次显示时,使用NumberPicker.SetDegenantFocusability(FOCUS\u BLOCK\u Subgenders)
抑制软键盘。然后让您的对话框或活动实现
View.OnTouchListener
,在
NumberPicker
上调用
setOnTouchListener(this)
,并在实现
onTouch(View v,MotionEvent e)
时将NumberPicker子体的焦点重置为其正常值,然后返回false

返回false意味着触摸仍然由NumberPicker处理,这意味着如果用户点击编辑框,软键盘就会出现。这正是我在面对同样的问题时想要的——让软键盘在对话框第一次显示时弹出对话框会让人不快,因为它会在对话框出现后将其向上移动

public class GetBufferDialog extends DialogFragment implements View.OnTouchListener {
onCreateDialog()
方法中创建对话框并找到NumberPicker后:

m_oldFocus = m_numberpicker.getDescendantFocusability();
m_numberpicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
m_numberpicker.setOnTouchListener(this);
以下是OnTouch方法:

public boolean onTouch(View v, MotionEvent event) {
    m_numberpicker.setDescendantFocusability(m_oldFocus);
    return false;
}

我发现最简单的工作方法是:

numberPicker               = (NumberPicker) myDialogView.findViewById(R.id.myViewId);
EditText numberPickerChild = (EditText) numberPicker.getChildAt(0);
numberPickerChild.setFocusable(false);
numberPickerChild.setInputType(InputType.TYPE_NULL);
*调用此命令:

  • 不会阻止子体聚焦(numberpicker将是可编辑的)

  • 将在创建时隐藏软输入

  • 在(处理输入)之前,getValue()将允许获取正确的walue



  • 安德鲁·韦伯答案的Xml版本

    android:descendantFocusability="blocksDescendants"
    
    范例

    <NumberPicker
            android:id="@+id/your_numberpicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:descendantFocusability="blocksDescendants"/>
    

    我不知道为什么它能工作,但设置OnClickListener却什么也不做,阻止了键盘显示(棒棒糖)


    如果您只想在使用数字选择器加载视图时隐藏软件键盘,但仍希望用户能够在加载视图后进行编辑,则不应阻止子体聚焦。相反,只需防止数字选择器成为视图中的第一个焦点项目

    有关详细信息,请参阅

    根据上述答案:

        <!-- Dummy item to prevent Number Picker from receiving focus -->
        <LinearLayout        
            android:focusable="true" 
            android:focusableInTouchMode="true"
            android:layout_width="0px" 
            android:layout_height="0px"/>
    
        <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component 
             to prevent the dummy from receiving focus again -->
        <NumberPicker 
            android:id="@+id/number_picker"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:nextFocusUp="@id/number_picker" 
            android:nextFocusLeft="@id/number_picker"/>
    

    工作代码 在程序上:

    mp.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    
    XML:


    这个扩展很好,因为它不会忘记如何做,并且有可读的代码。它有点隐藏了实现细节,但在这种情况下,我相信它是可以接受的:

    fun NumberPicker.disableTextEditing(disable: Boolean) {
        descendantFocusability = if (disable) FOCUS_BLOCK_DESCENDANTS else FOCUS_BEFORE_DESCENDANTS
    }
    

    是的,我必须直接编辑NumberPicker的子视图。Android API有时非常稀疏。除非我遗漏了什么,否则这会删除为NumberPicker键入值的功能。这并不一定是一件坏事,这取决于您如何使用选择器,但它确实似乎是解决方案的一个限制。您也可以在XML中设置这一点:android:DegenantFocusability=“blocksDescendants”啊,当然是blocksDescendants。非常感谢:)惊人的解决方案,救了我!只有androidx 1.2.0-rc2的编程版本对我有效,我将它与
    isClickable=true
    isFocusable=true
    (Kotlin)结合在一起,只有这个版本帮助我解决了问题,感谢你的方法如果我添加
    android:genderantfocusability=“blocksdessendants”
    ,单击到当前值后,它将隐藏。只有添加click listener帮助了我。谢谢你救了我的心。
        <!-- Dummy item to prevent Number Picker from receiving focus -->
        <LinearLayout        
            android:focusable="true" 
            android:focusableInTouchMode="true"
            android:layout_width="0px" 
            android:layout_height="0px"/>
    
        <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component 
             to prevent the dummy from receiving focus again -->
        <NumberPicker 
            android:id="@+id/number_picker"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:nextFocusUp="@id/number_picker" 
            android:nextFocusLeft="@id/number_picker"/>
    
    mp.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    
    android:descendantFocusability="blocksDescendants" 
    
    fun NumberPicker.disableTextEditing(disable: Boolean) {
        descendantFocusability = if (disable) FOCUS_BLOCK_DESCENDANTS else FOCUS_BEFORE_DESCENDANTS
    }