`
yangzhizhen
  • 浏览: 74818 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

基于Android的简单登录系统

阅读更多

      刚开始接触Android,做了一个简单的登录系统,实现的主要功能有:输入正确的用户名和密码后,点击登录按钮,就会进入另一个界面;如果用户名或密码不正确,则会弹出一个消息框。这种可视化界面在Android中称作Activity。

     下面是具体的代码实现

  1. AndroidLoginActivity(初始时显示的Activity)
package cn.yzz.AndroidLogin;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
 * 程序启动时显示的第一个Activity
 */
public class AndroidLoginActivity extends Activity {
	//用户名文本编辑框
	private EditText username;
	//密码文本编辑框
	private EditText password;
	//登录按钮
	private Button login;
	//定义Intent对象,用来连接两个Activity
	private Intent intent;
	//重写的方法,启动一个Activity时,此方法会自动调用
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局
        setContentView(R.layout.main);
        //得到登录按钮对象
        login = (Button)findViewById(R.id.userlogin);
        //给登录按钮设置监听器
        login.setOnClickListener(ocl);
        login.setBackgroundColor(Color.MAGENTA);
    }
    //创建登录按钮监听器对象
    OnClickListener ocl = new OnClickListener(){
		public void onClick(View arg0) {
			//得到用户名和密码的编辑框
			username = (EditText)findViewById(R.id.username);
			password = (EditText)findViewById(R.id.password);
			//判断用户输入的用户名和密码是否与设置的值相同,必须要有toString()
			if("yangzhizhen".equals(username.getText().toString())&& 
					"123456".equals(password.getText().toString())){
				System.out.println("你点击了按钮");
				//创建Intent对象,传入源Activity和目的Activity的类对象
				intent = new Intent(AndroidLoginActivity.this, SencondActivity.class);
				//启动Activity
				startActivity(intent);
			}else{
				//登录信息错误,通过Toast显示提示信息
				Toast.makeText(AndroidLoginActivity.this,"用户登录信息错误" , Toast.LENGTH_SHORT).show();
			}
		}
    };
}

  

2.SencondActivity(登录成功后跳转到的Activity)

package cn.yzz.AndroidLogin;

import android.app.Activity;
import android.os.Bundle;

/**
 * 登录成功后,显示的Activity
 */
public class SencondActivity extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设置布局
		setContentView(R.layout.second);
	}
}

  

3.初始显示的Anctivity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<!-- 此Activity采用线性布局 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <!-- TextView相当于Java中的标签组件,下面是对其属性的设置 -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#00ff00"
        android:textSize="40dip"
        android:text="@string/title" />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:text="@string/user_name"/>
    <!-- 文本编辑框 -->
    <EditText
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:id="@+id/username"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:text="@string/user_password"/>
    <EditText
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:id="@+id/password"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:text="@string/login"
        android:id="@+id/userlogin"
        />
        
        

</LinearLayout>

 

 4.成功登录后跳转到的Activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff00ff"
        android:textSize="50dip"
        android:text="@string/second_title" />
</LinearLayout>

 

5.用到的一些String的值

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="title">简单登录系统</string>
    <string name="app_name">AndroidLogin</string>
    <string name="user_name">用户名:</string>
    <string name="user_password">密码:</string>
    <string name="login">登录</string>
    <string name="second_title">您已成功登录</string>

</resources>

 

6.Activity注册信息

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.yzz.AndroidLogin"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="3" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidLoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 每个Activity都要在此文件中注册 -->
        <activity 
            android:name=".SencondActivity">
        </activity>
    </application>

</manifest>

   

分享到:
评论
2 楼 zhzhhghd 2013-09-27  
1 楼 xiaoshan5634 2012-10-16  
初学Android,学习。。。

相关推荐

Global site tag (gtag.js) - Google Analytics