Android实现闪屏及注册和登录界面之间的切换效果
程序员文章站
2024-03-01 20:12:40
在没给大家介绍正文之前先给大家说下实现思路:
先分别实现闪屏、注册界面、登录界面的活动,再用intent将相关的活动连接起来,实现不同活动之间的跳转。此次试验代码较多,我...
在没给大家介绍正文之前先给大家说下实现思路:
先分别实现闪屏、注册界面、登录界面的活动,再用intent将相关的活动连接起来,实现不同活动之间的跳转。此次试验代码较多,我只列出主要代码,详细的代码可用底部的下载链接下载。
一、实验效果图:
二、主要代码:
(1)welcomeactivity.java(这部分代码实现的是第一页的欢迎页面)
package com.example.flashscreendemo; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.animation.alphaanimation; import android.view.animation.animation; import android.view.animation.animation.animationlistener; import android.view.animation.rotateanimation; import android.widget.imageview; import android.widget.relativelayout; public class weclomeactivity extends activity { private relativelayout layoutwecome; private imageview ivmonkey; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /* * requestwindowfeature(window.feature_no_title); //取消电量状态栏 * this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, * windowmanager.layoutparams.flag_fullscreen); */ setcontentview(r.layout.activity_weclome); layoutwecome = (relativelayout) findviewbyid(r.id.layoutwelcome); alphaanimation alphaanimation = new alphaanimation(0.0f, 1.0f); alphaanimation.setduration(1000); layoutwecome.startanimation(alphaanimation); ivmonkey = (imageview) findviewbyid(r.id.ivmonkey); rotateanimation rotateanimation = new rotateanimation(0, 360); rotateanimation.setduration(2000); rotateanimation.setrepeatcount(2); ivmonkey.startanimation(rotateanimation); rotateanimation.setanimationlistener(new animationlistener() { @override public void onanimationstart(animation animation) { } @override public void onanimationrepeat(animation animation) { } @override public void onanimationend(animation animation) { intent intent = new intent(weclomeactivity.this, guideactivity.class); startactivity(intent); finish(); } }); } }
(2)guideactivity.java(这部分代码实现的是四个引导界面的功能)
package com.example.flashscreendemo; import java.util.arraylist; import java.util.list; import com.example.flashscreendemo.adapter.guideadapter; import android.app.activity; import android.content.intent; import android.os.bundle; import android.support.v4.view.viewpager; import android.support.v4.view.viewpager.onpagechangelistener; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.widget.imageview; public class guideactivity extends activity { private viewpager vpguide; private imageview ivdots[]; private int[] dots_id; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_guide); initguideviews(); initdots(); vpguide.setonpagechangelistener(new onpagechangelistener() { @override public void onpageselected(int position) { for (int i = 0; i < ivdots.length; i++) { if (position == i) { ivdots[i] .setimageresource(r.drawable.bg_point_selected); } else { ivdots[i].setimageresource(r.drawable.bg_point_normal); } } } @override public void onpagescrolled(int position, float arg1, int arg2) { } @override public void onpagescrollstatechanged(int postion) { } }); } public void jumpmain(view view) { intent intent = new intent(this, com.example.minitwittersimulate.loginactivity.class); startactivity(intent); finish(); } private void initdots() { dots_id = new int[] { r.id.ivdots1, r.id.ivdots2, r.id.ivdots3, r.id.ivdots4 }; ivdots = new imageview[dots_id.length]; for (int i = 0; i < ivdots.length; i++) { ivdots[i] = (imageview) findviewbyid(dots_id[i]); } } private void initguideviews() { vpguide = (viewpager) findviewbyid(r.id.vpguide); list<view> views = new arraylist<view>(); layoutinflater layoutinflater = getlayoutinflater(); view view1 = layoutinflater.inflate(r.layout.view1ofpager, null); views.add(view1); view view2 = layoutinflater.inflate(r.layout.view2ofpager, null); views.add(view2); view view3 = layoutinflater.inflate(r.layout.view3ofpager, null); views.add(view3); view view4 = layoutinflater.inflate(r.layout.view4ofpager, null); views.add(view4); guideadapter guideadapter = new guideadapter(views); vpguide.setadapter(guideadapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.guide, menu); return true; } }
(3)guideadapter.java(这部分代码实现的是数据适配的功能)
package com.example.flashscreendemo.adapter; import java.util.list; import android.support.v4.view.pageradapter; import android.view.view; import android.view.viewgroup; public class guideadapter extends pageradapter { private list<view> views; public guideadapter(list<view> views) { super(); this.views = views; } @override public int getcount() { return views.size(); } @override public boolean isviewfromobject(view view, object object) { return view == object; } @override public object instantiateitem(viewgroup container, int position) { container.addview(views.get(position)); return views.get(position); } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview(views.get(position)); } }
(4)loginactivity.java(这部分代码是登录界面的实现)
package com.example.minitwittersimulate; import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.preference.preferencemanager; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.checkbox; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import com.example.flashscreendemo.r; public class loginactivity extends activity { private sharedpreferences pref; private sharedpreferences.editor editor; private edittext etusername; private edittext etpassword; private button btnlogin; private checkbox cbrememberpass; private textview tvre; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); pref = preferencemanager.getdefaultsharedpreferences(this); etusername = (edittext) findviewbyid(r.id.etusername); etpassword = (edittext) findviewbyid(r.id.etpassword); cbrememberpass = (checkbox) findviewbyid(r.id.cbrememberpass); btnlogin = (button) findviewbyid(r.id.btnlogin); tvre = (textview) findviewbyid(r.id.tvre); boolean isremember = pref.getboolean("remember_password", false); if (isremember) {// 将账号和密码都设置到文本框中 string account = pref.getstring("account", ""); string password = pref.getstring("password", ""); etusername.settext(account); etpassword.settext(password); cbrememberpass.setchecked(true); } btnlogin.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { string account = etusername.gettext().tostring(); string password = etpassword.gettext().tostring(); if (account.trim().equals("")) { toast.maketext(loginactivity.this, "请您输入用户名!", toast.length_short).show(); return; } if (password.trim().equals("")) { toast.maketext(loginactivity.this, "请您输入密码!", toast.length_short).show(); return; } if (account.equals("admin") && password.equals("123456")) { editor = pref.edit(); if (cbrememberpass.ischecked()) {// 检查复选框是否被选中 editor.putboolean("remember_password", true); editor.putstring("account", account); editor.putstring("password", password); } else { editor.clear(); } editor.commit(); intent intent = new intent(loginactivity.this, mainactivity.class); startactivity(intent); finish(); } else { toast.maketext(loginactivity.this, "account or password is invalid", toast.length_short).show(); } } }); tvre.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(loginactivity.this, register.class); startactivity(intent); finish(); } }); } }
(5)user.java(这部分代码实现向注册界面传入用户输入的数据)
package com.example.minitwittersimulate; public class user { private string username; private string password; private char sex; private string city; private string hobby; public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public char getsex() { return sex; } public void setsex(char sex) { this.sex = sex; } public string getcity() { return city; } public void setcity(string city) { this.city = city; } public string gethobby() { return hobby; } public void sethobby(string hobby) { this.hobby = hobby; } public user(string username, string password, char sex, string city, string hobby) { super(); this.username = username; this.password = password; this.sex = sex; this.city = city; this.hobby = hobby; } @override public string tostring() { // todo auto-generated method stub return this.username + "," + this.password + "," + this.sex + "," + this.city + "," + this.hobby; } }
(6)register.java(这部分代码是注册界面的实现)
package com.example.minitwittersimulate; import com.example.flashscreendemo.r; import android.app.activity; import android.content.intent; import android.os.bundle; import android.text.textutils; import android.view.view; import android.widget.checkbox; import android.widget.edittext; import android.widget.radiobutton; import android.widget.spinner; import android.widget.toast; public class register extends activity { edittext etuser, etpassw, etrepassw; radiobutton rbmale, rbfamale; checkbox cbread, cbtourist, cbplaygame; spinner spcity; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_register); init(); } private void init() { // todo auto-generated method stub etuser = (edittext) findviewbyid(r.id.etuser); etpassw = (edittext) findviewbyid(r.id.etpassw); etrepassw = (edittext) findviewbyid(r.id.etrepassw); rbmale = (radiobutton) findviewbyid(r.id.rbmale); rbfamale = (radiobutton) findviewbyid(r.id.rbfamale); cbread = (checkbox) findviewbyid(r.id.cbread); cbtourist = (checkbox) findviewbyid(r.id.cbtourist); cbplaygame = (checkbox) findviewbyid(r.id.cbplaygame); spcity = (spinner) findviewbyid(r.id.spcity); } public void onclick(view view) { switch (view.getid()) { case r.id.btregister: string username = etuser.gettext().tostring(); if (textutils.isempty(username)) { toast.maketext(this, "用户名不能为空", 2000).show(); return; } string passw = etpassw.gettext().tostring(); if (textutils.isempty(passw)) { etpassw.seterror("密码不能为空"); return; } string repassw = etrepassw.gettext().tostring(); if (textutils.isempty(repassw)) { etrepassw.seterror("确认密码不能为空"); return; } if (!passw.equals(repassw)) { toast.maketext(this, "两次密码不一致", 2000).show(); return; } char sex; if (rbmale.ischecked()) { sex = rbmale.gettext().charat(0); } else { sex = rbfamale.gettext().charat(0); } stringbuffer s = new stringbuffer(); if (cbread.ischecked()) { s.append(cbread.gettext().tostring() + ","); } if (cbtourist.ischecked()) { s.append(cbtourist.gettext().tostring() + ","); } if (cbplaygame.ischecked()) { s.append(cbplaygame.gettext().tostring() + ","); } string city = spcity.getselecteditem().tostring(); user user = new user(username, passw, sex, s.tostring().equals("") ? "" : s.tostring().substring(0, s.length()), city); toast.maketext(this, "用户注册信息:" + user.tostring(), 100000).show(); intent intent = new intent(register.this, loginactivity.class); startactivity(intent); finish(); break; case r.id.bteixt: finish(); break; case r.id.btreturn: intent intent1 = new intent(register.this, loginactivity.class); startactivity(intent1); finish(); break; } } }
(7)mainactivity.java(这部分代码实现的是点击“登录”按钮后跳转到的界面)
package com.example.minitwittersimulate; import android.app.activity; import android.os.bundle; import android.view.menu; import com.example.flashscreendemo.r; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } }
以上所述是小编给大家介绍的android实现闪屏及注册和登录界面之间的切换效果,希望对大家有所帮助