2023年Android实验报告.pdf
创建一个名为“登录页面”的布局文献,该文献用于展示登录信息。程序界面如下图所示:p ack a g e co m.e x ample.mylayoutl;i mpo r t a n dro i d.a p p.Ac t ivi t y;impor t a n d ro i d.conten t.C o nte x t;1 mp o r t and r oid.c onten t.I ntent;im por t andro 1 d.co n t e nt.Shar e d P refere n ces;1 mport andro 1 d.o s.Bu n die;impo r t and r oid.v ie w.Menu;1 mport andro i d.v ie w.V i e w;impo r t an d roi d.widg e t.E ditT e x t;im p o r t and r oid.w i dget.Toast;p ublic cla s s Ac t n v ity O le x te n d s A c t iv 1 t y。E d it T e xt n ame;。Ed i tTex t p a s s ;pro t e c te d void o nCreate(Bund 1 e sa vedlns ta n ceS t ate)。s u p er.o n C r eate(s ave dl n stanc e St a te);3 Set C o ntentView(R.ayou t.mylin e);nam e 二(Edi t T e xt)fin d ViewByld(R.l d.edi t T ex t 1);b pass=(EditTe x t)findView Byld(R.id.ed itTe x t2);p ubl 1 c v oi d c 1 ic k(V i ew view)I nte n t i n te n t l=n ew In t ent(t his.MylayoutlMainAc t ivity.class);startActivity(i n t e n tl);pu b 1 ic vo i d c 1 ic k 1 (V ie w v i e w)Shared P refe fe n c e s sf1=getSharedPre f e r ences(acc o un t,C ontext.MODE_ PRIVATE);S t ring n am e 1=s fl.ge t S t r i n g(name ,);Str i n g pa s sl=s f l.getStrin g(pass,);3 S t rin g name2=n a me.g e t T ext().t oStri n g();S t ri n g pa s s2=p a ss.ge t T ex t().t oS t ring();。1 f(n a mel.e quals(n a me2)&p a s s i.eq uals(pa s s 2)Int e nt intent2=n e w Int e n t(t his,Tex t view 1 .c la ss);。i nte n t2.putEx t ra(n a me,n a mel);。inte n t 2.putE x t ra(pas s,pass 1 );。star t Ac t ivity(i ntent2);e lseStri ng c o=您的用户名或密码错误;6 Toa s t.makeText(t h is,co.t o S t ri n g(),10 00).show();private S haredPreferences g e t Share d P r eference()。return n u II;a 5554:AIDX3,M 2:19扁I ActivityOl OMD M M MD用户名密 码HMtfwai*KeyboardUM yvut ItvykMM to prov4 ta(MI登陆注册aa1创建一个名为“注册页面”的布局文献,该文献用于展示登录信息。程序界面如下图所示:pac k a g e com.exam p 1 e.mylayou t 1;im p ort android,o s.Bu n die;impo r t android.app.Ac t i vity;i mport a ndro i d.c onte n t.I n t ent;impo r t android.content.Sha r e dPr e fe r e n ces;import a n droid,c ontent.S haredPreferences.Ed i tor;impo r t androi d.view.Menu;impo r t a nd r oid.v iew.View;1 m po r t a n d ro id.widget.CheckBo x;impo r t and r oid.widge t.Ed itT ex t;i mport a n d r oid.widg e t.R a d i oButt o n;i mpo r t a n d ro id .w id get.R a dioGr o u p;impor t a nd r oid.wid g et.S pi n ner;import a ndroid.widget.TextVi ew;p u b 1 ic class M y layoutlM a in Act 1 v i t y exte n d s Ac t i vi t yE d i t T ext uname;EditT ext upass;R a dioB u tton ra d iol;Radi o Button r a d i o2;Ra d ioGr o u p gr o up 1 ;S t rin g sex;String name;S t ring pass;Che c k Box b oxi;C heckBo x box2;C hec k Box box3;Ch e ckBox bo x 4;S tring strl;S t ring str2;String s tr3;S trin g s t r 4;Spin n er s p;S tring sp 1 ;0 v e r r i d ee p r ote c t e d v o id o n C re ate(Bun d Ie sa v ed Instan c eS t ate)。super.o n Create(save d I n stan c eState);setC ontentV i ew(R.1 a y out.m y layoutl_main);gro u p 1=(RadioG r o up)findViewByld(R.i d.rad i oGroupl);。u nam e=(Ed 1 tText)f i n dV i e wBy I d(R.i d.editTe x;3 u pass=(Edi t Te x t)findV i ewBy I d(R.id.editTex t 3);o boxl=(C hee kBox)find ViewByld(R .id.c h ec k B ox 1);。bo x 2=(Chec k Box)fi n d Vi e wByld(R.id.ch e ckBo x 2);b ox3=(Ch ec k Bo x)find View By ld(R.id.c heckBox3);box4=(CheckBox)findViewBy I d(R.id.checkBox4);。sp=(Spinne r)fin d V ie w B y I d(R.id.spin n erl);。0ve r rid e。public b oole a n onC r eateOption s Me n u(M e nu menu)e /Inflate the men u;th is a d d s i t ems t o the ac t ion bar i f it is pres e n t.getM e nu I nfla t e r(),i nflat e(R.m e n u.mylayoutl_ ma i n,me n u);ret u rn t rue;pu b 1 i c void click(View v iew)f o r(in t i=0;i g roup 1 .getC h i 1 dCount();i+)。rad i ol=(Radio B u tton)g r oupl.getC hildAt(i);e i f(ra d i o 1.isC h ec k ed()。sex=r adiol.getText().toS t rin g();break;。n a me=u name.ge t Te x t().toStr n ng();pass二 up a s s.getText().t o String();。if(boxl.i s C hecked()strl=box 1 .g e tText(),toStri n g();。else strl=。i f(box2.i s Checked()s t r 2=box2.getTe x t().toSt r i n g();e ls e st r 2=;。if(bo x 3.I s Chee k e d()str3=b ox 3.getTe x t Q.toStri n g();else s t r 3=;。if(bo x 4.isC h e c k ed()str4=b o x 4.g e tT ext().to S t r ing();。else str4=,H;。s p 1=(St r in g)s p.g e t S e l ectedltem();I nte n t int e ntl=n ew Inte n t(this,Text v iew2.class);int e n tl.p u t Extra(name,nam e);i n tentl.putE x tra(pas s ,p a s s);i n t en tl.putExtra(s ex,sex);intentl.putExt r a(s t r,s t rl+str2+str3+st r 4);1 nte n t l.p utExt r a(s p 1,s p l);s tart Ac t i v ity(int e n t l);S ha r e d Prefer e n c es s f 1=g e tS h a redPr e ferences(ac c ount,MODE_PRI VATE);Editor editor=sfl.edi t();ed 1 to r.putStr 1 ng(name,name);edit o r.putSt r ing(pas s,p a s s)e d i tor.c ommi t();p u b lic v o l d c 1 i c kl(View view)I nten t intent2=new In t e nt(th i s.Act 1 v i t yOl.cl a s s);start Act i vi t y(i n te n t 2)?创 建“登录成功页面”的界面交互代码,该程序实现用户信息显示。关键代码如下:pa c kage co m.ex a mple.my 1 a y out 1 ;impo rt andr o id.os.Bundle;import and r oid.app.Activity;imp o rt android.co n te n t.I n t ent;im p o rt an d ro id.vi e w.Me n u;impor t a nd r o i d.widg e t.T ex t V iew;p u bl 1 c cl a ssT extvi ew l e xte n d s Activ i ty T extView resu 1 t;。prot e c ted void onC r eate(Bu ndie s avedln s ta nceSta t e)su p e r.onCr e ate(s avedln s tan c e S tate);。setConte n tView(R.l a y o ut.a c ti v i ty_ textvi e wl);。In t e n t 1 n t entl=g e tint e nt();。String str 1 =i n tentl.g e tStrin g E x tra(n a me);。3 Strin g s t r2=i n tentl.g e t Str i ng E xtra(p a s s);o。r e sult=(T e xt V i e w)thi s.fin d V i ewByld(R.id.textl);r esu 1 t.setText(“您已登录成功”+“n ”+“用户名:+s t rl+“n”+”密 码:+str2);public b o olean onCrea t e Op t-ion sMenu(Menu m e nu)。/Inf 1 ate t he men u;this adds i t ems t o the act i on ba r i f i t is p re sent.。g e tM e n u ln fl ater().i n flate(R.menu.t e xtviewl,m enu);。re t urn true;5554:AIDX%H 2:21ifii Textview 1您已登录成功用户名:11密码a匕目创 建“用户信息显示界面”的界面交互代码,该程序实现用户信息显示。关键代码如下:packa g e com.ex a m p I e.mylayoutl;i m po r t android.os.Bundl e;im p o rt a n dr o i d.app.A c tiv i ty;im port and r oid.content.lntent;import an d r oid.v i e w.Me n u;i m p o r ta n d r o i d.vi e w.Vi e w;impo r t a n dro id.w i dg e t.Tex t View;pu b 1 i c c lass Textv 1 ew2 ex t ends A c tiv 1 t y。T ext View v iewl;。T e x t View view2;。Tex tView view 3;。T e x t View v iew 4;。Text View view5;Ove r ride。pro t ect e d v oid onCrea t e(Bun d Ie savedlnst a n c eState)。super.onCreate(savedins ta n ceState);。se t Co nten t View(R.1 ayout.a ctivi t y_te x tvi e w 2);3 view 1=(TextView)f i n d Vi e wByld(R.i d,te x t V ie w l);。o vi ew2=(TextV i e w)f indV 1 ew B y ld(R.id.t e x t View 2);vi e w3=(Te x tView)f 1 n d ViewByld(R.i d.t ext V 1 e w 3);。v i ew4=(T extView)f indViewByld(R.i d.textVi e w4);。v iew5=(TextView)find ViewByld(R.i d.textVi ew5);Intent i n tent 1 =ge t In t e n t();String n a me=intentl.g e tStri n gEx t ra(name);。o String p a s s =intentl.getStri n gE x t r a(p a s s);3 S t ring s e x =inten t l.getSt r ingExtra(se x);St r ing s t r=i nten 1 1.g e t String Ext r a(st r);S t r ing spl=int e n tl.g e t Stri n g E xt r a(sp 1 );。vi e w l.se tT e x t(nam e);o。vi e w2.setTe x t(pass);view3.se t T e xt(s ex);view4.s e tTe x t(str);3 vi e w5.setT e xt(spl)?)p u blic void c li c k 1(View v ie w)。Inte n t i n te n tl=new I n t ent(t h is,ActivityOl.c la s s);s tart A c tivity(inte nt 1);)。Overr i de。public boole a n o n Cr e ateOptionsMe n u(Menu m e n u)6/In f 1 at e t h e menu;this ad d s i t ems to t h e a c t 1 on bar i f it isp rese n t.。g e tMenuInf 1 a ter().in f 1 a t e(R.men u.te x tview2,menu);。r eturn true;)5554:AIDX3 U 2:20Textview2(M icC M frok 怎Hftfwatv ButlwW M t wwMad m A VOHMtfwai*KeyboardUM youi hvykuY to prov4用 户 名:11密 码:11性 别:男喜 好:体育身 份:教师确定Jaa1