1. Function and usage of the search box (Searchview)Searchview is a search component that allows users to enter files within a text box and allows listeners to listen for user input. when the user input is complete, the search is submitted and the actual search is performed through the listener . common methods are as follows:Seticonfiedbydefault (Boolean iconified): Sets whether the search box is automatically reduced to an icon by default;setsubmitbuttonenabled (Boolean enabled): Sets whether the search button is displayed;setqueryhint (charsequence hint): Set the default display text in the search box;Setonquerytextlistener (Searchview.onquerytextlistener listener): Set up event listeners for this search;
Source code Combat:
function: Defines a Searchview and ListView component in the interface layout file that automatically populates the ListView component with the data that is searched when the user enters data in the search box.
(1) Src/mainactivity.java
Package Com.example.searchviewtest;import Android.app.activity;import Android.os.bundle;import Android.text.textutils;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.searchview;import Android.widget.searchview.onquerytextlistener;import Android.widget.Toast; public class Mainactivity extends Activity implements onquerytextlistener{private Searchview Searchview = null; private L Istview ListView = null; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Searchview = (Searchview) Findviewbyid (R.id.search); ListView = (ListView) Findviewbyid (R.id.list); /** * Add Adapter * */FINAL string[] data= {"China", "Pakistan", "middle", "Rice state", "middle", "Just test"} to the ListView; arrayadapter<string> adapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1,data); Listview.setadapter (adapter); ListviEw.settextfilterenabled (TRUE); Enable text filter/** * Set Searchview related properties * */Searchview.seticonifiedbydefault (FALSE);//Set Searchview Default whether to automatically zoom out to icon Searchview.setonquerytextlistener (this);//Set event listener for the Searchview component searchview.setsubmitbuttonenable D (true);//Set the Searchview Display Search button searchview.setqueryhint ("Please enter keywords"); }/** * Dynamically displays the results of the search * */@Override public boolean onquerytextchange (String newtext) {if (Textutils.isempty (n Ewtext) {//If the search box has no input, clear the filter listview.cleartextfilter () of the ListView; }else{Listview.setfiltertext (NewText);//causes the search box character to match the list data display} return true; /** * The method fires when the search button is clicked * */@Override public boolean onquerytextsubmit (String query) {toast.maketext (this, "Your choice is:" +que Ry, Toast.length_short). Show (); return false; }}
(2) res/layout/main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "match_parent" ><searchview Android:id= "@+id/search" android:layout_width= "fill_parent" android:layout_height= "Wrap_content"/> <listview android:id= "@+id/list" android:layout_width= "fill_parent" android:layout_height= " 0DP " android:layout_weight=" 1 "/></linearlayout>
(3) Effect demonstration
2. Scrolling view (ScrollView) How to useThe scrolling view ScrollView is derived from framelayout and is primarily used to add a scrollbar component to a normal component. The Scollview can contain a maximum of one component and a vertical scroll bar for that component. If the app needs to add a horizontal scroll bar, it needs to be implemented through the Horizontalscrollview component.
Source code Combat:
Features: Scrolling display of text in TextView components.
(1) Src/mainactivity.java
package com.example.srcollviewtest;import Java.io.ioexception;import Java.io.inputstream;import android.app.Activity; Import Android.content.res.assetmanager;import Android.os.bundle;import Android.widget.textview;public class Mainactivity extends Activity {private TextView mText = null; private String parseresult = ""; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); MText = (TextView) Findviewbyid (R.id.text); Assetmanager Assetmanager = Getassets ();//Get the Asset Management object try {InputStream InputStream = Assetmanager.open ("Info.txt" ); Gets the specified file input stream int length = inputstream.available (); Gets the total number of input streams bytes byte[] buffer = new Byte[length]; byte array cache inputstream.read (buffer); Reads a length byte in the input stream into the byte array mtext.settext (new String (buffer, "GB2312")); } catch (IOException e) {e.printstacktrace (); } }}
(2) Res/main.xml
<scrollview xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent ">
(3) Effect demonstration
function and usage of 3.Notificationnotification is a notification that appears on the phone's status bar, at the top of the phone's screen, and is typically used to display the current network status, battery status, and time of the phone. Notification represents a global effect notification that is typically sent notification through the Notificationmanager service. Notificationmanager is an important system service located at the application framework layer where applications can send global notifications to the system through Notificationmanager. to create a notification object from the Notification.builder class, use the following steps:Call the Getsystemservice (Notification_service) method to obtain the Notificationmanager service of the system;create a notification object from the Notification.builder class;call the notification method to set various properties(1) setdefaults (): Set notification LED lights, music, vibration, etc.(2) Setautocancel (): After setting the click Notification, the status bar automatically deletes the notification(3) Setcontenttitle (): Set notification title(4) Setcontenttext (): Set notification content(5) Setsmallcon (): Set icon for notifications(6) Setlargeicon (): Set large icons for notifications(7) Settick (): Sets the prompt text of the notification in the status bar(8) setcontentintent (): Sets the pendingintent of the program component that will be launched after the click Notification.send notification via Notificationmanager. Source code Combat:function: Implement send a notification notification, and accompany the LED light to blink, when click on the notification to jump to the information display activity. (1) Src/mainactivity.java Package Com.example.notificationtest;import Android.app.activity;import Android.app.notification;import Android.app.notification.builder;import Android.app.notificationmanager;import Android.app.PendingIntent;import Android.content.componentname;import Android.content.intent;import Android.net.uri;import Android.os.Bundle; Import Android.view.view;public class Mainactivity extends Activity {private Notificationmanager notificationmanager; Private final static int notification_id=0x123; Notification unique identifier ID @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (R.layout.main); Get the Notificationmanager service of the system Notificationmanager = (Notificationmanager) getsystemservice (Notification_service); }/** * Send notification notification */public void sendnotification (View v) {Intent Intent = new Intent (); Intent.setclassname ("Com.tencent.mobileqq", "com.tencent.mobileqq.activity.SplashActivity");//StartQQ client Pendingintent pendingintent = pendingintent.getactivity (mainactivity.this, 0, intent, 0); /** * Create notifications and specify related properties * */Notification notify = new Notification.builder (this). Setautocancel (TRUE)//Set The notification automatically disappears. Setticker ("new Message")//sets the notification prompt information displayed in the status bar. Setsmallicon (R.drawable.ic_launcher)//Setting the notification icon . Setcontenttext ("Congratulations on getting rich!" ")//Set the caption of the notification content. Setcontenttext (" You have a new QQ red envelope, poke to collect? ") ")//Set the notification content. SetDefaults (notification.default_lights| notification.default_vibrate)//led lamp, vibration set to default. Setsound (Uri.parse ("android.resource:// com.example.notificationtest/"+r.raw.ring)"//Set Custom sound. Setwhen (System.currenttimemillis ()). Setcontentinten T (pendingintent)//Set Click notification Event (boot). build (); Create notification/** * SEND notification * */notificationmanager.notify (notification_id, notify); }/** * Remove notification of NOTIFICATION_ID flag */public void cancelnotification (View v) {notificationmanager.cancel ( NOTIFICATION_ID); }} Note: In addition to setting sound, vibration, and flash cues for notification with the SetDefaults () method, we can also use the Setsound (URI), Setvibrate (long[] Long) method to set the effects of custom sounds. For example:Setsound (Uri.parse ("File:///sdcard/click.mp3"))--Set custom soundssetviberate (New long[]{0,50,100,150})--Setting a custom vibration(2) androidmainfest.xml add permission<span style= "Font-family:times New roman;font-size:18px;" > <!--Add LEDs, vibrate permissions--<uses-permission android:name= "Android.permission.FLASHLIGHT"/> < Uses-permission android:name= "Android.permission.VIBRATE"/></span>
(3) Effect demonstration
Android Search box/scrolling View/notification