Android examples for User Interface:TextView
Adds the given listeners to all text input fields contained in the given view.
import java.util.ArrayList; import java.util.List; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.TextWatcher; import android.text.format.Time; import android.view.View; import android.view.ViewGroup; import android.view.View.OnFocusChangeListener; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Spinner; import android.widget.SpinnerAdapter; public class Main{ /**//ww w .ja v a2s . c o m * Adds the given listeners to all text input fields contained in the given view. * @param onFocusChangeListener * @param textChangedListener * @param rootView */ public static void addEditTextListener( final OnFocusChangeListener onFocusChangeListener, final TextWatcher textChangedListener, View rootView) { getChildren(rootView, new ArrayList<View>(), new ViewFilter() { @Override public boolean matches(View view) { if (view instanceof EditText) { ((EditText) view) .setOnFocusChangeListener(onFocusChangeListener); if (textChangedListener != null) { ((EditText) view) .addTextChangedListener(textChangedListener); } } return false; } }); } /** * Performs a depth-first search for Views contained in the given view, and returns * all views that match the given filter. Can be used to perform operations on the child * views directly by putting the operation in the ViewFilter's matches() method * @param v the root view. * @param viewList views that match the filter are added to this list. * @param filter specifies the search criteria for the search. * @return the given list, populated with matching views. */ public static List<View> getChildren(View v, List<View> viewList, ViewFilter filter) { if (v instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) v; for (int i = 0; i < viewGroup.getChildCount(); i++) { getChildren(viewGroup.getChildAt(i), viewList, filter); } } if (filter.matches(v)) { viewList.add(v); } return viewList; } }