Android 登陆窗口实现
下面的这个例子主要讲的就是有三个Activity,一个是Login的Activity,一个是正确登陆后的Activity,最后一个是登陆错误后的Activity。在这里还有就是检测用户名和密码,用的是最最简陋的equals来判断的,主要还是因为这个是一个比较简单的登录窗口吗,谢谢大家!下面是全部代码。
Java代码:
- package org.clove.login;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class Login extends Activity {
- private static Button loginButton;
- private static Button cancelButton;
- private static Button registerButton;
- private static Button exitButton;
- private ButtonListener bl = new ButtonListener();
- private EditText et1;
- private EditText et2;
- private Intent intent;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login);
- //添加登陆按钮监听
- loginButton = (Button)findViewById(R.id.login_ok);
- loginButton.setOnClickListener(bl);
- //添加取消按钮监听
- cancelButton = (Button)findViewById(R.id.login_reset);
- cancelButton.setOnClickListener(bl);
- //添加注册按钮监听
- registerButton = (Button)findViewById(R.id.register);
- registerButton.setOnClickListener(bl);
- //添加退出按钮监听
- exitButton = (Button)findViewById(R.id.exit);
- exitButton.setOnClickListener(bl);
- }
- private class ButtonListener implements View.OnClickListener {
- public void onClick(View view) {
- // TODO Auto-generated method stub
- if(view == loginButton) {
- et1 = (EditText)findViewById(R.id.username_info);
- et2 = (EditText)findViewById(R.id.password_info);
- if((et1.getText().toString()).equals("heji") && (et2.getText().toString()).equals("heji")) {
- intent = new Intent();
- //用Bundle来传递当前Activity的内容
- Bundle bundle = new Bundle();
- bundle.putString("USERNAME", et1.getText().toString());
- intent.putExtras(bundle);
- intent.setClass(Login.this, Information.class);
- //启动Activity
- startActivity(intent);
- }else {
- intent = new Intent();
- intent.setClass(Login.this, ErrorPage.class);
- //启动Activity
- startActivity(intent);
- }
- }else if(view == cancelButton) {
- intent = new Intent();
- //通过Login这个类来启动Login
- intent.setClass(Login.this, Login.class);
- //启动Activity
- startActivity(intent);
- }else if(view == registerButton) {
- }else if(view == exitButton) {
- finish();
- }
- }
- }
- }
Information.java
Java代码:
- package org.clove.login;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class Information extends Activity {
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- //显示布局
- this.setContentView(R.layout.information);
- TextView tv = (TextView)findViewById(R.id.first_page_info);
- //获得从上个页面传过来的数据
- Bundle bundle = this.getIntent().getExtras();
- String str = bundle.getString("USERNAME");
- tv.setText(str);
- Button button_back = (Button)findViewById(R.id.back);
- button_back.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- //通过Information这个类来启动Login
- intent.setClass(Information.this, Login.class);
- //启动Activity
- startActivity(intent);
- }
- });
- }
- }
ErrorPage.java
Java代码:
- package org.clove.login;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class ErrorPage extends Activity {
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- //显示布局
- this.setContentView(R.layout.errorpage);
- Button button_back = (Button)findViewById(R.id.errorback);
- button_back.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- //通过ErrorPage这个类来启动Login
- intent.setClass(ErrorPage.this, Login.class);
- //启动Activity
- startActivity(intent);
- }
- });
- }
- }
分享到: | |
不错,刚刚学习android开发。必须给力。
起步较晚,需要努力了