heji 发表于 2013-1-15 03:00:14

一个Android登陆的简单实现

在公司实习半个月了,上个星期开始接触Android,做了一个很简单的登陆实现。有三个Activity,一个是Login的Activity,一个是正确登陆后的Activity,最后一个是登陆错误后的Activity。其中registerButton没写,因为我还没看android.database这个包,以后我会慢慢的完善它。还有就是检测用户名和密码,用的是最最简陋的equals来判断的,还是因为我没看android.database,呵呵。我现在还是新手,如果哪里有写的不好的,或者是不完善的,请大家多多批评。谢谢大家!下面是全部代码。代码我上传到附件,欢迎下载。

Login.java
package com.heji.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 {/** Called when the activity is first created. */    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 stubif(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);//启动ActivitystartActivity(intent);}else {                  intent = new Intent();intent.setClass(Login.this, ErrorPage.class);//启动ActivitystartActivity(intent);}}else if(view == cancelButton) {intent = new Intent();//通过Login这个类来启动Loginintent.setClass(Login.this, Login.class);//启动ActivitystartActivity(intent);}else if(view == registerButton) {}else if(view == exitButton) {    finish();}}   }}

Information.java(名字取得很不好,见谅)
package com.heji.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 stubsuper.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 stubIntent intent = new Intent();//通过Information这个类来启动Loginintent.setClass(Information.this, Login.class);//启动ActivitystartActivity(intent);}});}}

ErrorPage.java
package com.heji.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 stubsuper.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 stubIntent intent = new Intent();//通过ErrorPage这个类来启动Loginintent.setClass(ErrorPage.this, Login.class);//启动ActivitystartActivity(intent);}});}}
下面是布局文件:
login.xml
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:text="@string/welcome_hello"    />      <TableRow android:orientation="horizontal"                  android:layout_width="fill_parent"                   android:layout_height="wrap_content">                           <TextView                android:layout_width="fill_parent"            android:layout_height="wrap_content"             android:text="@string/username"         />                  <EditText android:id="@+id/username_info"             android:maxLength="8"             android:maxLines="1"             android:layout_weight="1.0"             android:layout_width="fill_parent"            android:layout_height="wrap_content"             android:text=""         />    </TableRow>      <TableRow android:orientation="horizontal"                  android:layout_width="fill_parent"                   android:layout_height="wrap_content">                           <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"             android:text="@string/password"         />                  <EditText android:id="@+id/password_info"             android:password="true"             android:maxLength="10"             android:maxLines="1"             android:layout_weight="1.0"             android:layout_width="fill_parent"            android:layout_height="wrap_content"             android:text=""         />    </TableRow>         <LinearLayout android:orientation="horizontal"                   android:layout_width="fill_parent"                  android:layout_height="wrap_content">                           <Button android:id="@+id/login_ok"                   android:layout_width="fill_parent"                  android:layout_height="wrap_content"                   android:layout_weight="1.0"                   android:text="@string/login"         />                      <Button android:id="@+id/login_reset"                   android:layout_width="fill_parent"                  android:layout_height="wrap_content"                   android:layout_weight="1.0"                   android:text="@string/reset"         />    </LinearLayout>         <LinearLayout android:orientation="horizontal"                   android:layout_width="fill_parent"                  android:layout_height="wrap_content">                           <Button android:id="@+id/register"                   android:layout_width="fill_parent"                  android:layout_height="wrap_content"                   android:layout_weight="1.0"                   android:text="@string/register"         />         <Button android:id="@+id/exit"                   android:layout_width="fill_parent"                  android:layout_height="wrap_content"                   android:layout_weight="1.0"                   android:text="@string/exit"         />    </LinearLayout>    </TableLayout>

information.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TextView      android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:text="@string/success_info"    />    <TextViewandroid:id="@+id/first_page_info"    android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:text=""    />    <Button android:id="@+id/back"          android:layout_width="fill_parent"         android:layout_height="wrap_content"          android:text="@string/back"/></LinearLayout>

errorpage.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TextView      android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:text="@string/errorpage"    /><Button android:id="@+id/errorback"            android:layout_width="60dip"               android:layout_height="wrap_content"            android:text="@string/error_back"      /></LinearLayout>

strings.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="welcome_hello">Hello,Welcome To Login</string>    <string name="app_name">Login</string>    <string name="username">username</string>    <string name="password">password</string>    <string name="login">login</string>    <string name="reset">reset</string>    <string name="register">register</string>    <string name="information">congratulation!</string>    <string name="success_info">Hello Android Developer,You ID Is</string>    <string name="errorpage">Sorry,You Don't Register,Please By The "BACK" Button To Return The FirstPage,Thank You!!!</string>    <string name="error_back">BACK</string>    <string name="back">BACK</string>    <string name="exit">Exit</string></resources>
页: [1]
查看完整版本: 一个Android登陆的简单实现