ANDROID_学习笔记

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Activity初步Activity的实现:1.继承类Activity2.重写onCreate()方法3.每一个Activity都要在AndroidManifest.xml中进行配置4.在Activity上添加控件例:publicclassMyActivityextendsActivity{@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);TextViewmyTextView=(TextView)findViewById(R.id.myTextView);ButtonmyButton=(Button)findViewById(R.id.myButton);myTextView.setText(我的第一个TextView);myButton.setText(我的第一个Button);}}多个Activity之间的关系1.跳转方法:startActivity(Intentintent)一个Intent对象包含一组信息:1.Componentname2.Action3.Data4.Category5.Extras6.FlagsActivity之间的跳转publicclassActivity02extendsActivity{privateButtonmyButton=null;publicvoidonCreate(BundlesaveInstanceState){super.onCreate(saveInstanceState);setContentView(R.layout.main);myButton=(Button)findViewById(R.id.myButton);myButton.setOnClickListener(newMyButtonListener());}classMyButtonListenerimplementsOnClickListener{publicvoidonClick(Viewv){Intentintent=newIntent();intent.putExtra(testIntent,123);//传递数据intent.setClass(Activity02.this,OtherActivity.class);Activity02.this.startActivity(intent);}}}publicclassOtherActivityextendsActivity{privateTextViewmyTextView=null;protectedvoidonCreate(BundlesaveInstanceState){super.onCreate(saveInstanceState);setContentView(R.layout.main);Intentintent=getIntent();Stringvalue=intent.getStringExtra(testIntent);//取出数据myTextView=(TextView)findViewById(R.id.myTextView);//myTextView.setText(R.string.other);myTextView.setText(value);}}布局文件属性的说明相对布局文件属性说明:android:layout_above将该控件的底部置于给定的ID的控件之上android:layout_below将该控件的底部置于给定的ID的控件之下android:layout_toLeftOf将该控件的右边缘和给定ID的控件的左边对齐android:layout_toRightOf将该控件的左边缘和给定ID的控件的右边对齐android:layout_alignBaseline该控件的baseline和给定ID的控件的baseline对齐android:layout_alignBotton该控件的底部和给定ID的控件的底部对齐android:layout_alignLeft该控件的左边和给定ID的控件的左边对齐android:layout_alignRight该控件的右边和给定ID的控件的右边对齐android:layout_alignTop该控件的顶部和给定ID的控件的顶部对齐android:layout_alignParentBottom如为true则将该控件的底部父控件的底部对齐android:layout_alignParentLeft如为true则将该控件的底部父控件的左部对齐android:layout_alignParentRight如为true则将该控件的底部父控件的右部对齐android:layout_alignParentTop如为true则将该控件的底部父控件的顶部对齐android:layout_centerHorizontal如为true则该控件将被置于水平方向的中央android:layout_centerInParent如为true则该控件将被置于父控件的中央android:layout_centerVertical如为true则该控件将被置于垂直方向的中央SQLite的使用SQLite使用方法publicclassDatabaseHelperextendsSQLiteOpenHelper{privatestaticfinalintVERSION=1;publicDatabaseHelper(Contextcontext,Stringname,CursorFactoryfactory,intversion){super(context,name,factory,version);}publicDatabaseHelper(Contextcontext,Stringname){this(context,name,VERSION);}publicDatabaseHelper(Contextcontext,Stringname,intversion){this(context,name,null,version);}publicvoidonCreate(SQLiteDatabasedb){db.execSQL(createtableuser(idint,namevarchar(20)));System.out.println(createaDatabase);}publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){System.out.println(updateaDatabase);}}publicclassSQLiteActivityextendsActivity{classUpdateListenerimplementsOnClickListener{publicvoidonClick(Viewv){DatabaseHelperdbHelper=newDatabaseHelper(SQLiteActivity.this,test_mars_db,2);SQLiteDatabasedb=dbHelper.getReadableDatabase();}}classInsertListenerimplementsOnClickListener{publicvoidonClick(Viewv){ContentValuesvalues=newContentValues();values.put(id,1);values.put(name,zhangsan);DatabaseHelperdbHelper=newDatabaseHelper(SQLiteActivity.this,test_mars_db);SQLiteDatabasedb=dbHelper.getWriteableDatabase();db.insert(user,null,values);}}classUpdateRecordListenerimplementsOnClickListener{publicvoidonClick(Viewv){DatabaseHelperdbHelper=newDatabaseHelper(SQLiteActivity.this,test_mars_db);SQLiteDatabasedb=dbHelper.getWriteableDatabase();ContentValuesvalues=newContentValus();values.put(name,zhangsanfeng);db.update(user,values,id=?,newString[]{1});}}classQueryListenerimplementsOnClickListener{publicvoidonClick(){DatabaseHelperdbHelper=newDatabaseHelper(SQLiteActivity.this,test_mars_db);SQLiteDatabasedb=dbHelper.getWriteableDatabase();Cursorcursor=db.query(user,newString[]{id,name},id=?);while(cursor.moveToNext()){Stringname=cursor.getString(cursor.getColumnIndex(name));System.out.println(query-----+name);}}}}Service的使用Service的使用publiccalssFirstServiceextendsService{publicIBinderonBind(Intentintent){System.out.println(ServiceonBind);returnnull;}publicvoidonCreate(){super.onCreate();System.out.prinltn(ServiceonCreate);}publicintonStartCommand(Intentintent,intflags,intstartId){System.out.println(flags---+flags);System.out.println(startId---+startId);System.out.println(ServiceonStartCommand);returnSTART_NOT_STICKY;}publicvoidonDestroy(){System.out.println(ServiceonDestroy);super.onDestroy();}}在Manifest.xml中注册:servicename=.FirstService/publicclassTestServiceActivityextendsActivity{privateButtonstartServiceButton=null;privateButtonendServiceButton=null;publicvoiconCreate(BundlesaveInstanceState){super.onCreate(saveInstanceState);setContentView(R.layout.main);startServiceButton=(Button)findViewById(R.id.startServiceButton);endServiceButton=(Button)fidnViewById(R.id.endServiceButton);startServiceButton.setOnClickListener(newStart

1 / 22
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功