Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

android - Method to get all EditTexts in a View

can anyone help me with coding a method to get all EditTexts in a view? I would like to implement the solution htafoya posted here: How to hide soft keyboard on android after clicking outside EditText?

Unfortunately the getFields() method is missing and htafoya did not answer our request to share his getFields() method.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

EDIT

MByD pointed me to an error, thus making my answer almost identical to that of blackbelt. I have edited mine to the correct approach.


You could do a for-each loop and then check if each view is of the type EditText:

ArrayList<EditText> myEditTextList = new ArrayList<EditText>();

for( int i = 0; i < myLayout.getChildCount(); i++ )
  if( myLayout.getChildAt( i ) instanceof EditText )
    myEditTextList.add( (EditText) myLayout.getChildAt( i ) );

You could also, instead of having a list of EditTexts, have a list of ID's and then just add the id of the child to the list: myIdList.add( child.getId() );


To access your layout you need to get a reference for it. This means you need to provide an ID for your layout in your XML:

<LinearLayout android:id="@+id/myLinearLayout" >
   //Here is where your EditTexts would be declared
</LinearLayout>

Then when you inflate the layout in your activity you just make sure to save a reference to it:

LinearLayout myLinearLayout;

public void onCreate( Bundle savedInstanceState ) {
   super( savedInstanceState );
   setContentView( R.layout.myLayoutWithEditTexts );

   ...

   myLinearLayout = (LinearLayout) findViewById( R.id.myLinearLayout );
}

You then have a reference to your the holder of your EditTexts within the activity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...