Three Questions

264 Views Asked by At

First, I want set unchecked all the 8 checkboxes using a loop like this:

    for (int i=1;i<8;++i){
    CheckBox view1 = (CheckBox) findViewById(R.id.CheckBox0+String.valueOf(i));
    view1.setChecked(false);
    }

It Doesn't work, but you get the idea what I mean. How can solve it?

Second: With Eclipse I set a form list. What is making that when application starts, it immediately shows the keyboard and is focused in a editing field?. I want that the keyboard appears only after the user touches an editing field.

Third: How I set the properties of the editing field that when user touches enter, the focus doesn't pass to the next editing field. Thanks in advance.

3

There are 3 best solutions below

3
On

For the resources to have fixed ids after each build, you will have to declare the resources in public.xml, then you can access the ids sequentially. Check here

Also R.id.CheckBox0 is an int so do

findViewById(R.id.CheckBox0+ i)

after you have declared all checkboxes in public.xml

For the second question in the activity manifest add android:windowSoftInputMode="stateHidden"

Third: I think you will have to reference the same edittext in layout for android:nextFocusDown. Not sure if this will work give it a try

1
On

1 - Remember that this is Java not Javascript, you can't do this (R.id.CheckBox0+String.valueOf(i)). Normally I create an array of int with all the R.id inside and loop through them.

2 - You could use also android:windowSoftInputMode="stateUnchanged" to not hide the keyboard if it's already showing.

3 - That's the way it's supposed to be, but you can use on the edittext the property android:imeOptions="actionNone", so the enter button will do nothing.

10
On

Firstly, asking multiple questions together isn't good; it prevents people from answering when they know only one of the solutions.

1 - Relying on the IDs CheckBox0, CheckBox1, CheckBox2, ... to be in order is very risky and is bad practice. In this case, you should be using getIdentifier; this will fetch IDs CheckBox1, then CheckBox2, etc. reliably.

for (int i=1;i<8;++i){
    CheckBox view1 = (CheckBox) findViewById(getResources().getIdentifier("CheckBox" + i, "id", getPackageName()));
    view1.setChecked(false);
}

2 - You need to use a stateHidden modifier for this:

<activity
    android:name="com.example.NoKeyboardActivity"
    android:windowSoftInputMode="stateHidden" />

3 - Use imeOptions for this; actionNone is the one you're looking for, or (as per comments), actionDone to enable the "Done" button.

<TextView
    ...
    android:imeOptions="actionNone" />