gpt4 book ai didi

android - 在 NumberPicker 上禁用软键盘

转载 作者:IT老高 更新时间:2023-10-28 12:55:19 25 4
gpt4 key购买 nike

我试图在使用 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>

最后,这是我尝试在 onCreate() 方法中阻止键盘的代码:

// hide keyboard
View.OnClickListener disableKeyBoardListener = new View.OnClickListener() {
public void onClick(View v) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
};

((EditText) weightPicker.getChildAt(1)).setInputType(InputType.TYPE_NULL);
((EditText) repetitionPicker.getChildAt(1)).setInputType(InputType.TYPE_NULL);

((EditText) weightPicker.getChildAt(1)).setOnClickListener(disableKeyBoardListener);
//((EditText) repetitionPicker.getChildAt(1)).setOnClickListener(disableKeyBoardListener);
//weightPicker.setOnClickListener(disableKeyBoardListener);
//repetitionPicker.setOnClickListener(disableKeyBoardListener);

getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

遗憾的是,点击 NumberPicker 时仍会显示软键盘。有什么想法吗?

最佳答案

刚刚发现这个,它就像一个魅力:

myNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

您也可以在 XML 中设置:

android:descendantFocusability="blocksDescendants" 

关于android - 在 NumberPicker 上禁用软键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8854781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com