Fragment, AutoCompleteTextView, animation, notification bar,
Fragment
Fragment (Fragment) is a small Activity that appears in Android3.0.
You can think of fragment as a modular area of an activity with its own lifecycle, receive input events of its own, and get started with adding and deleting Fargment while the activity is running. Define a layout for Fragment
The definition class inherits the onCreateView method in the Fragment override class and returns a View object as the root layout of the current Fragment. If fragment does not provide the UI, null can be returned.
// Define Fragment as part of the Activity's public class Fragment1 extends Fragment {// call this method when the system draws the ui for the first time so that Fragment can display its layout content @ Override public View onCreateView (LayoutInflater inflater, viewGroup container, Bundle savedInstanceState) {// converts a layout into a View object View = inflater. inflate (R. layout. fragment1, null); return view ;}}Demonstration: Layout configuration and dynamic code Loading
Configuration
Dynamic Loading
Public class MainActivity extends Activity {@ SuppressWarnings ("deprecation") @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // [1] Get the resolution of the mobile phone WindowManager wm = (WindowManager) getSystemService (WINDOW_SERVICE); int width = wm. getdefadisplay display (). getWidth (); int height = wm. getdefadisplay display (). getHeight (); // [2] judge the portrait screen. // [3] Get the Fragment manager and directly obtain the FragmentManager fragmentManager = getFragmentManager () through context; FragmentTransaction beginTransaction = fragmentManager. beginTransaction (); // enable the event if (height> width) {// it indicates that a Fragment android is loaded on the portrait screen. r. id. content // represents the beginTransaction form of the current mobile phone. replace (android. r. id. content, new Fragment1 ();} else {// indicates that a Fragment beginTransaction is loaded on the horizontal screen. replace (android. r. id. content, new Fragment2 ();} // [4] The last step remembers comment beginTransaction. commit ();}}Example: Use Fragment to simulate the interface Layout
Public class MainActivity extends Activity implements OnClickListener {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // [1] find the Button btn_wx = (Button) findViewById (R. id. btn_wx); Button btn_contact = (Button) findViewById (R. id. btn_contact); Button btn_disconver = (Button) findViewById (R. id. btn_disconver); Button btn_me = (Button) findViewById (R. id. btn_me); // [2] set the Click Event btn_wx.setOnClickListener (this); btn_disconver.setOnClickListener (this); btn_me.setOnClickListener (this); listener (this );} @ Override public void onClick (View v) {// [4] Get Fragment manager FragmentManager fragmentManager = getFragmentManager (); FragmentTransaction beginTransaction = fragmentManager. beginTransaction (); // determines which button is clicked switch (v. getId () {case R. id. btn_wx: // click beginTransaction. replace (R. id. ll_layout, new WxFragment (); break; case R. id. btn_contact: // click the contact beginTransaction. replace (R. id. ll_layout, new ContactFragment (); break; case R. id. btn_disconver: // click beginTransaction. replace (R. id. ll_layout, new DiscoverFragment (); break; case R. id. btn_me: // click my beginTransaction. replace (R. id. ll_layout, new MeFragment (); break;} // remember comment beginTransaction. commit ();}}Fragment Lifecycle
OnAttach: bind to activity
OnCreate: Create fragment
OnCreateView: Create a fragment Layout
OnActivityCreated: after the activity is created
OnStart: visible, not interactive
OnResume: visible and interactive
OnPause: Partially visible and interactive
OnStop: invisible
OnDestroyView: destroys the view object of fragment.
OnDestroy: fragment destroyed
OnDetach: unbound from activity
Fragment backward compatibility
Fragment was launched in Android 3.0.
Change all Fragment and FragmentManager to the class under the support-v4 package change the inheritance of Activity to the communication case between FragmentActivity (under the support-v4 package) Fragment
Fragment has a public bridge Activity
Fragment1
// Define Fragment as part of the Activity's public class Fragment1 extends Fragment {// this method is called when the system draws the ui for the first time so that Fragment can display its layout content @ Overridepublic view onCreateView (LayoutInflater inflater, viewGroup container, Bundle savedInstanceState) {// converts a layout into a View object View = inflater. inflate (R. layout. fragment1, null); // [1] click the button to set the event view. findViewById (R. id. btn_update ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// [2] modify the content in fragment2 through the public bridge of fragment ---> activity Fragment2 fragment2 = (Fragment2) getActivity (). getFragmentManager (). findFragmentByTag ("f2"); fragment2.updateText ("HAHAHA") ;}}); return view ;}}
Fragment2
Public class Fragment2 extends Fragment {private TextView TV _content; @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view View = inflater. inflate (R. layout. fragment2, null); // [1] Find the TV TV _content = (TextView) view. findViewById (R. id. TV _content); return view;} // modify the textview content public void updateText (String content) {TV _content.setText (content );}}
AutoCompleteTextView
Example
Code
Public class MainActivity extends Activity {// simulate the private String [] COUNTRIES = new String [] {"laofang", "laoli", "laozhang ", "laobi", "laowang", "aa", "abb", "ccc"}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // [1] Find the control AutoCompleteTextView actv = (AutoCompleteTextView) findViewById (R. id. actv); // [2] The actv control displays data in the same way as listview requires a data adapter ArrayAdapter.
Adapter = new ArrayAdapter
(This, android. R. layout. simple_dropdown_item_1line, COUNTRIES); // [3] display data actv. setAdapter (adapter );}}
Animation
Before Android 3.0, Android supported two animation modes: tween animation and frame animation.
A new animation system is introduced in android3.0: property animation
Frame Animation
Frame Animation: Creates a Drawable sequence, which can be displayed one by one at a specified time interval, that is, playing the prepared images in sequence.
Put the prepared image file in the res/drawable-hdpi directory. Create the folder drawable In the res directory of the project, and define the animation XML file under the folder. The file name can be customized, for example, frame_anim.xml. Open the created xml file and add the root node in it. You can set the attribute "android: oneshot" in this root node to control the animation only once. Otherwise, the system will play continuously by default. Add a node for each frame animation image under the root node. The "android: drawable" attribute of the node is the resource id of the image, "android: the duration Attribute specifies the time when the image is displayed. (generally, the animation effect can be felt when 5-8 images are displayed every second ).
Stop () before the animation start (). Otherwise, the animation stops at the last frame after the first animation, so that the animation will be triggered only once. The last point is mentioned in the SDK. Do not call start in onCreate, because AnimationDrawable is not completely associated with Window. If you want to start animation when the interface is displayed, you can go to onWindowFoucsChanged () start ().
Code
Public class MainActivity extends Activity {private ImageView iv; private AnimationDrawable animationDrawable; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); iv = (ImageView) findViewById (R. id. iv); iv. setImageResource (R. drawable. frame_anim); animationDrawable = (AnimationDrawable) iv. getDrawable ();} public void startAnimation (View view) {// click the button to call this method if (animationDrawable. isRunning () animationDrawable. stop (); animationDrawable. start ();}}TweenAnimation
View Animation.
Gradient includes: translation, scaling, rotation, and transparency change
Features:
The animation effect does not change the actual coordinates of the control.
It can only be applied to View objects, and only some attributes are supported. For example, it supports scaling and rotation, but does not support background color changes.
Use code to implement the animation
Common Methods
Animation:
SetDuration: Set the animation execution time
SetRepeatCount
SetRepeatMode specifies the repeated mode (for example, reverse)
SetFillAfter indicates whether the control state stays when the animation is stopped after the animation is specified.
SetAnimationListener: Set the animation event listener
ImageView:
StartAnimation (Animation a) allows ImageView to execute an Animation
Sample Code
Public class MainActivity extends Activity {private ImageView iv; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // This control is used to execute the animation iv = (ImageView) findViewById (R. id. iv); iv. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Toast. makeText (getApplicationContext (), "You cannot click me", 1 ). show () ;}}) ;}// click the button to achieve transparent effect. public void click1 (View v) {// creating transparent animation 1.0 means completely opaque. 0.0 unexpected users are completely transparent. AlphaAnimation aa = new AlphaAnimation (1.0f, 0.0f); aa. setDuration (2000); // sets the animation execution time aa. setRepeatCount (1); // set the number of animation repetitions aa. setRepeatMode (Animation. REVERSE); // set the repeated mode // start executing the animation iv. startAnimation (aa);} // click the button to achieve the rotation effect. public void click2 (View v) {// fromDegrees start angle toDegrees end angle // RotateAnimation ra = new RotateAnimation (0,360); RotateAnimation ra = new RotateAnimation (0,360, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); ra. setDuration (2000); // sets the animation execution time ra. setRepeatCount (1); // sets the number of animation repetitions (ra. setRepeatMode (Animation. REVERSE); // set the repeated mode // start executing the animation iv. startAnimation (ra);} // click the button to implement the zoom effect. public void click3 (View v) {ScaleAnimation sa = new ScaleAnimation (1.0f, 2.0f, 1.0f, 2.0f, and Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); sa. setDuration (2000); // set the animation execution time sa. setRepeatCount (1); // sets the number of animation repetitions sa. setRepeatMode (Animation. REVERSE); // set the repeated mode // start executing the animation iv. startAnimation (sa);} // click the button to implement the translation effect. public void click4 (View v) {TranslateAnimation ta = new TranslateAnimation (Animation. RELATIVE_TO_PARENT, 0, Animation. RELATIVE_TO_PARENT, 0, Animation. RELATIVE_TO_PARENT, 0, Animation. RELATIVE_TO_PARENT, 0.2f); ta. setDuration (2000); // set the animation execution time ta. setFillAfter (true); // when the animation ends, it stays at the end position. // start executing the animation iv. startAnimation (ta);} // click the button to run the public void click5 (View v) together with the animation. {// create the animation set AnimationSet = new AnimationSet (true ); alphaAnimation aa = new AlphaAnimation (1.0f, 0.0f); aa. setDuration (2000); // sets the animation execution time aa. setRepeatCount (1); // set the number of animation repetitions aa. setRepeatMode (Animation. REVERSE); // set the repeated mode RotateAnimation ra = new RotateAnimation (0,360, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); ra. setDuration (2000); // sets the animation execution time ra. setRepeatCount (1); // sets the number of animation repetitions (ra. setRepeatMode (Animation. REVERSE); // set the repeated mode ScaleAnimation sa = new ScaleAnimation (1.0f, 2.0f, 1.0f, 2.0f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); sa. setDuration (2000); // set the animation execution time sa. setRepeatCount (1); // sets the number of animation repetitions sa. setRepeatMode (Animation. REVERSE); // set the repeated mode // Add an animation set. addAnimation (aa); set. addAnimation (ra); set. addAnimation (sa); // executes the animation iv. startAnimation (set );}}Use XML to implement interlace Animation
If you want to use xml to define a replenishment animation, create an anim directory under res.
Alpha transparency
Rotate Rotation
Scale
Translate displacement
Set
Call
Animation aa = AnimationUtils. loadAnimation (getApplicationContext (), R. anim. alpha); // start executing the Animation iv. startAnimation (aa );
Property Animation
Property animation changes the actual coordinates of the control.
If you use xml to define an attribute animation, you need to create an animator directory under res.
Code
Public class MainActivity extends Activity {private ImageView iv; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // load the layout setContentView (R. layout. activity_main); // action to execute the animation iv = (ImageView) findViewById (R. id. iv); // sets a listening event for iv. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Toast. makeText (MainActivity. this, "You cannot click me", 0 ). show () ;}}); // iv. setTranslationX (translationX) // iv. setScaleX (scaleX) // iv. setAlpha (alpha) // iv. setro} // displacement animation public void translate (View v) {// create an attribute animation/*** target: The target who executes the animation * propertyName attribute name The name of the property being animated. * float... values Variable Parameter */ObjectAnimator oa = ObjectAnimator. ofFloat (iv, "translationX", 10, 50, 20, 150); oa. setDuration (1, 2000); oa. start (); // start animation} // scale the animation public void scale (View v) {ObjectAnimator oa = ObjectAnimator. ofFloat (iv, "scaleY", 0.1f, 2, 1, 2); oa. setDuration (1, 2000); oa. start () ;}// implement transparent effect public void alpha (View v) {ObjectAnimator oa = ObjectAnimator. ofFloat (iv, "alpha", 0, 0.5f, 0, 1, 0); oa. setDuration (1, 2000); oa. start () ;}// public void rotate (View v) {// ObjectAnimator oa = ObjectAnimator. ofFloat (iv, "rotation", 0,180, 90,360); ObjectAnimator oa = ObjectAnimator. ofFloat (iv, "rotationY", 0,180, 90,360); oa. setDuration (1, 2000); oa. start () ;}// fly together public void fly (View v) {AnimatorSet as = new AnimatorSet (); ObjectAnimator oa = ObjectAnimator. ofFloat (iv, "translationX", 10, 50, 20,100); ObjectAnimator oa2 = ObjectAnimator. ofFloat (iv, "scaleY", 0.1f, 2, 1, 2); ObjectAnimator oa3 = ObjectAnimator. ofFloat (iv, "alpha", 0, 0.5f, 0, 1); ObjectAnimator oa4 = ObjectAnimator. ofFloat (iv, "rotationY", 0,180, 90,360);. setDuration (2000);. setTarget (iv); // Add an animation to the set // fly as one by one. playSequentially (oa, oa2, oa3, oa4); // fly together //. playTogether (oa, oa2, oa3, oa4);. start () ;}// use xml to create an attribute animation public void playxml (View v) {ObjectAnimator oa = (ObjectAnimator) AnimatorInflater. loadAnimator (this, R. animator. oanimator); // sets the execution target oa. setTarget (iv); oa. start (); // start execution }}
Xml implementation
Notification bar
Notifications are used to display messages in the status bar. When a message arrives, they are represented by icons,
To view a message, drag the status bar to the bottom of the screen to view the message,
In Android, the notification manager NotificationManager is used to send or disable a notification.
If the pop-up notification is set to sound/vibrate/light-on, pay attention to add the corresponding permissions. Otherwise, an error will be thrown. For example, you need to grant the android permission to set the vibration. permission. when VIBRATE obtains the PendingIntent of the deferred intent, the encapsulated intent object must use the implicit startForeground (id, notification) method, which can improve the process priority.
Public class MainActivity extends Activity {private NotificationManager nm; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // [1] Get icationicationmanager instance nm = (icationicationmanager) getSystemService (NOTIFICATION_SERVICE);} // click the button to send a notification public void click1 (View v) {// chained call/* Notification noti = new Notification. builder (this ). setContent ("I am a big title "). setContentText ("I am the title content "). setSmallIcon (R. drawable. ic_launcher ). setLargeIcon (BitmapFactory. decodeResource (getResources (), R. drawable. ic_launcher )). build (); * // compatible with earlier versions. Use the outdated method: Notification notification = new Notification (R. drawable. ic_launcher, "I have received a notification", System. currentTimeMillis (); // you can set a notification to vibrate the animation so that the breathing light can be highlighted. defaults = Notification. DEFAULT_ALL; // set notification not to clear notification. flags = Notification. FLAG_NO_CLEAR; // create the Intent object intent Intent = new intent (); // implement the call function Intent. setAction (Intent. ACTION_CALL); intent. setData (Uri. parse ("tel:" + 119); PendingIntent pendingIntent = PendingIntent. getActivity (this, 1, intent, Intent. FLAG_ACTIVITY_NEW_TASK); // click the business logic notification corresponding to the notification. setLatestEventInfo (this, "Xiao Fang", "", pendingIntent); // send a notification nm. Y (10, notification);} // click the button to cancel sending a notification public void click2 (View v) {// cancel the notification nm. cancel (10 );}}