Android 登陆窗口实现  

下面的这个例子主要讲的就是有三个Activity,一个是Login的Activity,一个是正确登陆后的Activity,最后一个是登陆错误后的Activity。在这里还有就是检测用户名和密码,用的是最最简陋的equals来判断的,主要还是因为这个是一个比较简单的登录窗口吗,谢谢大家!下面是全部代码。

Java代码:

  1. package org.clove.login;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9.  
  10. public class Login extends Activity {
  11. private static Button loginButton;
  12. private static Button cancelButton;
  13. private static Button registerButton;
  14. private static Button exitButton;
  15. private ButtonListener bl = new ButtonListener();
  16. private EditText et1;
  17. private EditText et2;
  18. private Intent intent;
  19.  
  20. @Override
  21. public void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.login);
  24. //添加登陆按钮监听
  25. loginButton = (Button)findViewById(R.id.login_ok);
  26. loginButton.setOnClickListener(bl);
  27. //添加取消按钮监听
  28. cancelButton = (Button)findViewById(R.id.login_reset);
  29. cancelButton.setOnClickListener(bl);
  30. //添加注册按钮监听
  31. registerButton = (Button)findViewById(R.id.register);
  32. registerButton.setOnClickListener(bl);
  33. //添加退出按钮监听
  34. exitButton = (Button)findViewById(R.id.exit);
  35. exitButton.setOnClickListener(bl);
  36. }
  37.  
  38. private class ButtonListener implements View.OnClickListener {
  39. public void onClick(View view) {
  40. // TODO Auto-generated method stub
  41. if(view == loginButton) {
  42. et1 = (EditText)findViewById(R.id.username_info);
  43. et2 = (EditText)findViewById(R.id.password_info);
  44. if((et1.getText().toString()).equals("heji") && (et2.getText().toString()).equals("heji")) {
  45. intent = new Intent();
  46. //用Bundle来传递当前Activity的内容
  47. Bundle bundle = new Bundle();
  48. bundle.putString("USERNAME", et1.getText().toString());
  49. intent.putExtras(bundle);
  50. intent.setClass(Login.this, Information.class);
  51. //启动Activity
  52. startActivity(intent);
  53. }else {
  54.  
  55. intent = new Intent();
  56. intent.setClass(Login.this, ErrorPage.class);
  57. //启动Activity
  58. startActivity(intent);
  59. }
  60. }else if(view == cancelButton) {
  61. intent = new Intent();
  62. //通过Login这个类来启动Login
  63. intent.setClass(Login.this, Login.class);
  64. //启动Activity
  65. startActivity(intent);
  66. }else if(view == registerButton) {
  67.  
  68. }else if(view == exitButton) {
  69. finish();
  70. }
  71. }
  72. }
  73. }

 Information.java

Java代码:
 

  1. package org.clove.login;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9.  
  10. public class Information extends Activity {
  11. protected void onCreate(Bundle savedInstanceState) {
  12. // TODO Auto-generated method stub
  13. super.onCreate(savedInstanceState);
  14. //显示布局
  15. this.setContentView(R.layout.information);
  16. TextView tv = (TextView)findViewById(R.id.first_page_info);
  17. //获得从上个页面传过来的数据
  18. Bundle bundle = this.getIntent().getExtras();
  19. String str = bundle.getString("USERNAME");
  20. tv.setText(str);
  21. Button button_back = (Button)findViewById(R.id.back);
  22. button_back.setOnClickListener(new View.OnClickListener() {
  23. public void onClick(View view) {
  24. // TODO Auto-generated method stub
  25. Intent intent = new Intent();
  26. //通过Information这个类来启动Login
  27. intent.setClass(Information.this, Login.class);
  28. //启动Activity
  29. startActivity(intent);
  30. }
  31. });
  32. }
  33. }


ErrorPage.java

Java代码:
 

  1. package org.clove.login;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class ErrorPage extends Activity {
  10. protected void onCreate(Bundle savedInstanceState) {
  11. // TODO Auto-generated method stub
  12. super.onCreate(savedInstanceState);
  13. //显示布局
  14. this.setContentView(R.layout.errorpage);
  15.  
  16. Button button_back = (Button)findViewById(R.id.errorback);
  17. button_back.setOnClickListener(new View.OnClickListener() {
  18. public void onClick(View view) {
  19. // TODO Auto-generated method stub
  20. Intent intent = new Intent();
  21.  
  22. //通过ErrorPage这个类来启动Login
  23. intent.setClass(ErrorPage.this, Login.class);
  24. //启动Activity
  25. startActivity(intent);
  26. }
  27. });
  28. }
  29. }

欢迎大佬支持本博客的发展 -- Donate --

本文链接:Android 登陆窗口实现

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:三十岁,谢谢!^^


分享到:          
  1. 不错,刚刚学习android开发。必须给力。

  1. 没有通告