Common controls and properties in Android

Source: Internet
Author: User

In the previous blog for everyone to bring a lot of Android and JSP introduction, this article will bring you, about andriod commonly used controls and properties of the use of the purpose to facilitate everyone to forget, timely review reference. Okay crap don't talk about it now start our introduction to this article.

1, control the direction of application display:

Setrequestedorientation (activityinfo.screen_orientation_portrait); // displays the effect vertically. setrequestedorientation (Activityinfo.screen_orientation_landscape); // horizontal display effect. 

Add the previous sentence to Setcontentview (R.layout.activity_one);

2. Text line display and text line input:

<!--     Set text to display on one line--       android:singleline= "true";

3, Tabactivity the implementation method:

A, layout file:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Activitytwo "> <tabhost Android:id= "@+id/booktabhost"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" > <LinearLayout Android:id= "@+id/donebook"android:orientation= "Vertical"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content" > <TextView Android:text= "Border Town"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <TextView Android:text= "Fortress Besieged"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <TextView Android:text= "The man Chasing the Kite"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </LinearLayout> <LinearLayout Android:id= "@+id/doingbook"android:orientation= "Vertical"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content" > <TextView Android:text= "Love of the Allure"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <TextView Android:text= "Splendid Qianyang"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <TextView Android:text= "Alive"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </LinearLayout> <LinearLayout Android:id= "@+id/willbook"android:orientation= "Vertical"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content" > <TextView Android:text= "Hundred Years of Loneliness"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <TextView Android:text= "The elephant in the house"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <TextView Android:text= "Confession"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </LinearLayout> </TabHost></RelativeLayout>

B, Activity code:

 Public classActivitytwoextendstabactivity{ PublicTabhost bookth =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Bookth=Gettabhost (); Layoutinflater.from ( This). Inflate (R.layout.activity_two, Bookth.gettabcontentview (),true); Bookth.addtab (Bookth.newtabspec ("Done"). Setindicator ("read"). SetContent (R.id.donebook)); Bookth.addtab (Bookth.newtabspec ("doing"). Setindicator ("positive read"). SetContent (R.id.doingbook)); Bookth.addtab (Bookth.newtabspec ("would"). Setindicator ("unread"). SetContent (R.id.willbook)); }  }

C:

  

It is worth reminding that the way to be more respected at the moment is viewpager+fragement

4. Text INPUT hint:

A, layout file code:

<Autocompletetextview Android:id= "@+id/actextview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter name:"Android:textcolor= "#000"Android:maxlength= "Ten"/><Multiautocompletetextview Android:id= "@+id/mactextview"Android:layout_below= "@id/actextview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter City:"Android:textcolor= "#000"Android:maxlength= "5"/>

B, Activity code:

 Public classActivityfiveextendsactivity{PrivateAutocompletetextview Actextview; PrivateMultiautocompletetextview Mactextview; PrivateString [] arr = {"abc", "ABX", "Abo", "BDC", "BDF"}; PrivateString [] brr = {"AB Beijing", "AB Nanjing", "Ab Tokyo", "BB Moscow", "BB uk", "BB USA"}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_five); Actextview=(Autocompletetextview) Findviewbyid (R.id.actextview); Mactextview=(Multiautocompletetextview) Findviewbyid (R.id.mactextview); Arrayadapter<String> arradapt =NewArrayadapter<string> ( This, Android.         R.layout.simple_dropdown_item_1line, arr);        Actextview.setadapter (ARRADAPT); Arrayadapter<String> brradapt =NewArrayadapter<string> ( This, Android.        R.layout.simple_dropdown_item_1line, BRR);        Mactextview.setadapter (BRRADAPT); Mactextview.setthreshold (1);//set how many characters to enter to start auto-matchMactextview.settokenizer (NewMultiautocompletetextview.commatokenizer ());//Set delimiter    }    }

5. Send the broadcast:

A. Create broadcast content:

 Public classActivityoneextendsActivity {FinalString intent_action = "Com.android.BroadcastReceiverDemo";//the address of the broadcast; custom Settings@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_one); Intent Intent=NewIntent (intent_action); Intent.putextra ("Name", "Millet");//content of the broadcastActivityone. This. Sendbroadcast (Intent);//Send broadcast    }}

B. Declare the broadcast in Androidmanifest.xml:

<!--set up a broadcast receiver        --<receiver             android:name= "Cn.edu.hpu.android.activity_broadcast. Mybroadcastreceiver "            android:enabled=" true "            >            <intent-filter>                <action Android:name= "Com.android.BroadcastReceiverDemo"/>            </intent-filter>                    </receiver>

C. Receiving broadcasts:

 Public class extends broadcastreceiver{    @Override    publicvoid  onreceive (context context, Intent Intent) {             = Intent.getstringextra ("name");   receive broadcast data sent out by            toast.maketext (Context, "" +Name, Toast.length_short). Show ();            }}

6, access to the user network status:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Get network Connection ObjectsConnectivitymanager NW = (Connectivitymanager) This. Getsystemservice (Context.connectivity_service); Networkinfo NetInfo=Nw.getactivenetworkinfo (); Toast.maketext (mainactivity. This, "Current Network" +add (Netinfo.isavailable ()) + "," + "Network" +app (netinfo.isconnected ()) + "," + "network connection" +ADP (netinfo.isconnected ()), Toast.length_long). Show (); } string Add (Boolean bl) {string S= "Not Available"; if(bl==true) {s= "Available"; }        returns; } string App (Boolean bl) {string S= "Not Connected"; if(bl==true) {s= "Connected"; }        returns; } string ADP (Boolean bl) {string S= "does not exist!" "; if(bl==true) {s= "Exists! "; }        returns; }    }

Note: Add permission to get the network in the Androidmanifest.xml file:<uses-permission android:name= "android.permission.ACCESS_ Network_state "/>

7, Sharedpreferences Storage:

Sharedpreferences sp = getsharedpreferences (file_name, context.mode_append);                 = Sp.edit (); // get Edit Object                 editor.clear ();                Editor.putstring ("name", name);                editor.putstring ("password", number);                Editor.commit (); // Submit Content Save

Note: file_name is a unique identifier that we set up to make it easier for us to query and modify.

8, sharedpreferences content obtained:

Sharedpreferences sp = getsharedpreferences (file_name, context.mode_private);                 = sp.getstring ("name", "");                 = sp.getstring ("Password", "");

Note: file_name: To be consistent with what we have set above so that we can get the content we saved above. sp.getstring ("name", "" "); Briefly, the first parameter is the label we need to get the data stored, and the second parameter is what is returned when we get the data that does not exist.

  

Common controls and properties in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.