編輯:關於Android編程
在沒給大家介紹正文之前先給大家說下實現思路:
先分別實現閃屏、注冊界面、登錄界面的活動,再用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實現閃屏及注冊和登錄界面之間的切換效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!
今天我們一起來探討下安卓中BroadcastReceiver組件以及詳細分析下它的兩種注冊方式。BroadcastReceiver也就是“廣播接收者”的意思,顧名思義,它
我們在開發中常常會注意到一些Crash,這正是很頭疼的,而且Crash會帶來很多意想不到的狀態,很惡心,所以今天,我們來著重研究一下Crash,同時,我們也將使用第三方
一、前言 在現今App泛濫的市場上,各種App的功能都是你抄我的我抄你的時候,想做一個精品的App沒有自己的風格,沒有讓用戶眼
48.Android 標簽TextView的點擊技巧Android 標簽TextView的點擊技巧 前言 ClickableSpan源碼 自定義ClickableSpan