在没给大家介绍正文之前先给大家说下实现思路:
先分别实现闪屏、注册界面、登录界面的活动,再用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 views = new ArrayList();
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 views;
public GuideAdapter(List 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实现闪屏及注册和登录界面之间的切换效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!