0

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.

JoeCoolman
  • 512
  • 3
  • 8
  • 27

3 Answers3

3

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" />
Cat
  • 66,919
  • 24
  • 133
  • 141
  • ids can be reliably assigned using public.xml , it is not risky – nandeesh Aug 15 '12 at 18:18
  • @nandeesh But then they cannot be iterated through. – Cat Aug 15 '12 at 18:21
  • if you assign continous numbers from CheckBox0 to CheckBox8 in public.xml then you can iterate by just adding i to CheckBox0 – nandeesh Aug 15 '12 at 18:23
  • And if the AAPT tool is changed in a future revision to assign IDs backwards? Relying on the IDs to compile consecutively is a very bad idea. You should *always* assume that those IDs are arbitrary, just as you would with any [magic number](http://en.wikipedia.org/wiki/Magic_number_(programming)). – Cat Aug 15 '12 at 18:24
  • if you check the frameworks http://developer.android.com/reference/android/R.id.html#tabcontent . All ids are assigned arbitrarily. These ids will never change otherwise the api compatibility would break. this is achieved using public.xml. So all future revisions of aapt will support public.xml – nandeesh Aug 15 '12 at 18:28
  • Fair enough, but why go for hardcoding IDs to public.xml when you could just dynamically fetch the ones you have already assigned? – Cat Aug 15 '12 at 19:16
  • Yeah.. it boils down to individual preference of doing things – nandeesh Aug 15 '12 at 19:24
  • hahaha ok guys, nice discussion. Anyway, thanks for reply. I will try your suggestions @Eric and see – JoeCoolman Aug 16 '12 at 02:42
  • I was testing your suggestions Eric, about question three, I make a correction, it's not "actionNone", it's actionDone". Thanks, the other two helps worked perfectly. – JoeCoolman Aug 19 '12 at 06:14
  • Great--I'll correct my post. If it helped, don't forget to mark it correct! ;) – Cat Aug 19 '12 at 06:18
1

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

Community
  • 1
  • 1
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • 1
    You can't guarantee `R.id.CheckBox0+ i` will work. The 'ids' in R.java are generated as and when different resources are added. – Squonk Aug 15 '12 at 18:10
  • @squonk thats why the ids need to be added to public.xml. public.xml is present for the sole purpose to give fixed ids. Thats how framework guaratees compatibility for android.r.id. Check the link http://stackoverflow.com/questions/9348614/what-is-the-use-of-the-res-values-public-xml-file-on-android – nandeesh Aug 15 '12 at 18:12
  • OK, I missed the point about using public.xml but I don't personally see this as a viable approach. I'd be more inclined to use `getIdentifier(...)` as advised by Eric in his answer. – Squonk Aug 15 '12 at 18:20
0

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.

Marcio Covre
  • 4,556
  • 2
  • 22
  • 24