Fragment fully meet my needs | databaserのブログ

databaserのブログ

ブログの説明を入力します。

In a recent client application, I met a requirement to generate a specified number of edit box field according to the selected value so that the user can input character information. My initial idea was to put Fragment of these logical, just to increase the selected value based on changes to the linear layout container edit box number, but do make Fragment excessive expansion, and did not think too much in terms of reusability. This is a great opportunity, these interactive features can be encapsulated into a custom view. Custom views can be reused in the entire range of applications (so far there are at least two places), and let me test the package functions become more relaxed. What is a custom view Android framework provides a lot of views and layout, but in some cases developers need to create their own views. Sometimes extend the built-in class to add features, like support in the text box to customize the font and character spacing. In other cases, because the built-in views can not provide the needed functionality, like radial dial. Now we discuss a custom composite view. View composed of a plurality of other views, built-in or custom can be used to encapsulate complex interactions and functions. In a mature and complete Fragment fully meet my needs, I use a composite view, because I wanted a reusable component testable. The above example illustrates this situation well. Since the code belongs to a client project, so here I created a simple project to show how to create and use custom views. Custom views in this case, we want to customize the view to add an edit box, so the user can enter any number of data entries. In the custom view, by using a suitable number edit box contains a simple container view (linear layout) implementation, which can easily get a list of names. New Nike Free 5.0 V4 Shoes Black Red Code is as follows: / ** * A custom compound view that displays an arbitrary * number of text views to enter your friends names * / public class FriendNameView extends LinearLayout {private int mFriendCount; private int mEditTextResId; public FriendNameView (Context context) {this. (context, null);} public FriendNameView (Context context, AttributeSet attrs) {this (context, New Nike Free 5.0 V4 Shoes Black Red attrs, 0);} public FriendNameView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); setOrientation (VERTICAL);} public int getFriendCount () {return mFriendCount;} public void setFriendCount (int friendCount) {if SALOMON (! friendCount = mFriendCount) {mFriendCount = friendCount; removeAllViews (); for (int i = 0; i \u0026 lt; mFriendCount ; i ++) {addView (createEditText ());}}} private View createEditText () {View v; if (mEditTextResId \u0026 gt; 0) {LayoutInflater inflater = LayoutInflater.from (getContext ()); v = inflater.inflate (mEditTextResId , this, true);} else {EditText et = new EditText (getContext ()); et.setHint (R.string.friend_name); v = et;} return v;} public int getEditTextResId () {return mEditTextResId;} public void setEditTextResId (int editTextResId) {mEditTextResId = editTextResId;}. / ** * Returns a list of entered friend names * / public List \u0026 lt; String \u0026 gt; getFriendNames () {List \u0026 lt; String \u0026 gt; names = new ArrayList \u0026 lt; \u0026 gt; (); for (int i = 0; Air Max 2011 Womens Yellow Grey White i \u0026 lt; getChildCount (); i ++) {View v = getChildAt (i); if (v instanceof EditText) {EditText et = (EditText) v; names.add (et.getText (). toString ());}} return names;}} When a user Air Max 2012 Green Orange Black setFriendCount (int) method to set the number of friends, we reset the number of sub-fields based on the input edit box. As used herein, the completion of a custom layout, but will default to a simple edit box. When we want to obtain a list of user names that have been entered, you can not care about the internal structure of the custom view. Just call getFriendNames () method, the view will bring together a list of names. This is a simple example. This view is only by a linear layout (parent) view and Womens Nike Kobe VIII edit boxes composed as a special effects, because they already know how to save and restore the state, there Authentic Black Gold Nike KD 7 Mens Basketball Shoes is no need for state processing. Layout with a custom view when you want to use in Activity or Fragment custom layout view, you can 2015 Nike Free 5.0 view the same as using other, 616175-700 Black/Metallic Pine Green-Glow Dark Armory Slate Nike LeBron 11 Outlet add some simple XML. \u0026 Lt; com.ryanharter.android.compoundviews.app.views.FriendNameView android: id = \u0026 quot; @ + id / friend_names \u0026 quot; android: layout_width = \u0026 quot; match_parent \u0026 quot; android: layout_height = \u0026 New Nike Tr Fit Running Shoes Black quot; Air Jordan Outlet wrap_content \u0026 quot; / \u0026 gt; and other views Like, you can use findViewById (int) method to get it. mFriendNameView = (FriendNameView) findViewById (R.id.friend_names); in our MainActivity, when the pickup value Air Max 2012 Blue Silver White change data, we can very easily set the number of friends. When you want to get a list of names, calling getFriendNames () method. mFriendCountPicker.setOnValueChangedListener (new OnValueChangeListener () {Override public void onValueChange (NumberPicker picker, int oldVal, int newVal) {mFriendNameView.setFriendCount (newVal);}}); mCountFriendsButton.setOnClickListener (new OnClickListener () {Override public void onClick (View v) {List \u0026 lt; String \u0026 gt; names = mFriendNameView.getFriendNames (); Intent i = new Intent (MainActivity.this, FriendCountActivity.class); i.putStringArrayListExtra (\u0026 quot; names \u0026 quot ;, new ArrayList \u0026 Nike Air Max lt; String \u0026 gt; (names)) ; startActivity (i);}}); While this is an example of a deliberate design, but custom composite view is an excellent feature package. If you do not encapsulate the function code will be scattered throughout Air Max 2012 Blue Grey White the event and fragments. Custom composite view provides testable, reusable code, so the application more stable. I encourage everyone to think about where Womens Nike Kobe VIII your application can use a custom composite view. If you can, and share them with other developers, they are very useful. The sample project in this article may be available on Github.how to use Android to customize composite view