我用按钮创建了一个穷人的键盘,我已经使用三行这样将它们放在TableLayout中了:
<TableLayout android:id="@+id/TableLayout_Keyboard" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <Button android:id="@+id/Keyboard_Btn_Q" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/q" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_W" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/w" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_E" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/e" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_R" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/r" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_T" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/t" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_Y" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/y" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_U" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/u" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_I" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/i" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_O" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/o" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_P" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/p" tools:ignore="ButtonStyle" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <Button android:id="@+id/Keyboard_Btn_A" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/a" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_S" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/s" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_D" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/d" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_F" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/f" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_G" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/g" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_H" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/h" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_J" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/j" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_K" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/k" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_L" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/l" android:textSize="14sp" tools:ignore="ButtonStyle" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <Button android:id="@+id/Keyboard_Btn_Z" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/z" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_X" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/x" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_C" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/c" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_V" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/v" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_B" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/b" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_N" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/n" android:textSize="14sp" tools:ignore="ButtonStyle" /> <Button android:id="@+id/Keyboard_Btn_M" android:layout_width="35dp" android:layout_height="wrap_content" android:onClick="keyboardButtonPressed" android:text="@string/m" android:textSize="14sp" tools:ignore="ButtonStyle" /> </TableRow>
现在我想制作一个以编程方式禁用所有按钮的方法。我试过这样做:
private void disableKeyboard(){
TableLayout keyboard = (TableLayout)findViewById(R.id.TableLayout_Keyboard);
for (int i = 0; i < keyboard.getChildCount(); i++) {
View v = keyboard.getChildAt(i);
if (v instanceof Button) {
v.setEnabled(false);
}
}
}
但它不起作用。我以为.getChildAt()
会获得TableRows,所以我这样做了:
private void disableKeyboard(){
TableLayout keyboard = (TableLayout)findViewById(R.id.TableLayout_Keyboard);
for (int i = 0; i < keyboard.getChildCount(); i++) {
View v = keyboard.getChildAt(i);
if(v instanceof TableRow) {
for(int j=0; j < ((TableRow) v).getChildCount(); j++){
View vv = keyboard.getChildAt(i);
if (vv instanceof Button) {
vv.setEnabled(false);
}
}
}
}
}
但那也没有用。如何循环浏览所有按钮并禁用它们?
EDIT找到了解决方案:
private void disableKeyboard(){
TableLayout keyboard = (TableLayout)findViewById(R.id.TableLayout_Keyboard);
ArrayList<View> layoutButtons = keyboard.getTouchables();
for(View v : layoutButtons){
if( v instanceof Button ) {
v.setEnabled(false);
}
}
}
答案 0 :(得分:0)
我意识到禁用这些按钮会使他们无法接触&#34; - 意味着我们在被禁用后无法通过.getTouchables()
获取它们。我想创建一个切换功能(这样我可以在启用/禁用之间切换),并且必须这样做:
private ArrayList<View> keyboardButtons;
private void storeKeyboardButtons(){
TableLayout keyboard = (TableLayout)findViewById(R.id.TableLayout_Keyboard);
ArrayList<View> layoutButtons = keyboard.getTouchables();
for(View v : layoutButtons){
if( v instanceof Button ) {
keyboardButtons.add(v);
}
}
}
private void toggleKeyboard(boolean whichState){
//whichState == true enables ALL the buttons
//whichState == false disables ALL the buttons
for (View v: keyboardButtons) {
v.setEnabled(whichState);
}
}
我首先将键盘中的所有按钮存储在名为&#34; keyboardButtons&#34;的变量中,从onCreate -method调用此方法(storeKeyboardButtons)。稍后,当我想启用或禁用按钮时,我只需调用toggleKeyboard -method并为启用传递true,为禁用传递false。