Android Common Simple Code

Source: Internet
Author: User

1. Database SQLite operation

Import Android.database.Cursor; Import Android.database.sqlite.SQLiteCursorDriver; Import Android.database.sqlite.SQLiteDatabase; Import Android.database.sqlite.SQLiteQuery; Import android.database.sqlite.sqlitedatabase.cursorfactory;//Create a database, Openorcreatedatabase is the method sqlitedatabase mydatabase=this.openorcreatedatabase (" Mydata.db ", Mode_private, New Cursorfactory () {@Override public Cursor newcursor (Sqlitedatabase arg0, Sqlitecursordriver arg1, String arg2, Sqlitequery arg3) {return null;}}); Delete the database, Context.deltedatabase (String) this.deletedatabase (dbname); Execute the non-queried SQL instruction String sql= "INSERT INTO test (ID) VALUES (i)"; Db.execsql (SQL); Execute Query instruction Cursor cur=db.rawquery ("SELECT * from Test", null); The IF (cur!=null)//cursor initially points to the first row {//Gets the column int numcolumn=cur.getcolumnindex ("id") of the ID, if (cur.movetofirst ()) {do{int val= Cur.getint (numcolumn);//Get ID value}while (cur.movetonext ()); } }

2. Get screen information

Import Android.util.DisplayMetrics; Describes the structure of display information: density, height, width, font size displaymetrics displaymetrics=new displaymetrics (); Initialize Displaymetrics Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Displaymetrics); int width=displaymetrics.widthpixels; int height=displaymetrics.heightpixels;

3. Hint Information

Import Android.app.AlertDialog; Prompt dialog box Alertdialog.builder dialog=new alertdialog.builder (this); Dialog.settitle ("hint"); Dialog.setmessage ("Android hint"); Dialog.show (); Prompt short message, no interactive import android.widget.Toast; Maketext (Context,charsequence,int), the int parameter is displayed for how long/The system band has Toast.length_long,toast.length_short Toast.maketext ( Helloactivity.this, "Android Tips", Toast.length_long. Show ();

4.Activity jumps between

The starting activity is set Intent next_intent=new Intent (); Data transfer between the activity Bundle bundle=new Bundle (); Bundle.putstring ("username", Edittext.gettext (). toString ()); Next_intent.putextras (bundle); Specifies the jump Next_intent.setclass (Helloactivity.this, Nextpageactivity.class); StartActivity (next_intent); Objective activity to obtain data Bundle bundle=this.getintent (). Getextras (); String username=bundle.getstring ("username");

5. Jump to the specified Web site

Import Android.net.Uri; Open Web page Uri uri=uri.parse ("http://www.baidu.com"); Intent intent=new Intent (Intent.action_view,uri); StartActivity (Intent);

Cond

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.