您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 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
本文标题:ANDROID_学习笔记
链接地址:https://www.777doc.com/doc-6219764 .html