android开发一个简单的记事本之登录注册页面

接下来几天我们会用android来写一个简单的记事本例子。记事本的基本功能包括:登录注册,最新记事,所有记事,添加记事,管理记事等。
几天我们先来介绍记事本的第一个模块--登录注册界面。
第一,我们新建一个工程MyNotepad。
第二,添加布局文件,在/res/layout文件夹下添加一个文件activity_login.xml,文件内容如下(在文件中会使用到/res/values文件加下的strings.xml文件,请参考下面strings.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical" >

    <RelativeLayout
        android:id="@+id/rlname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >

        <TextView
            android:id="@+id/tvname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="60dp"
            android:text="@string/name"
            android:textSize="20sp" />
       
        <EditText
            android:id="@+id/name"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:hint="@string/namehint"
            android:layout_toRightOf="@id/tvname"
            android:textSize="18sp"/>
    </RelativeLayout>
   
    <RelativeLayout
        android:id="@+id/rlpwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rlname"
        android:layout_margin="5dp" >

        <TextView
            android:id="@+id/tvpwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/pwd"
            android:layout_marginLeft="60dp"
            android:textSize="20sp" />
       
        <EditText
            android:id="@+id/pwd"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:hint="@string/pwdhint"
            android:layout_toRightOf="@id/tvpwd"
            android:textSize="18sp"/>
    </RelativeLayout>
   
    <RelativeLayout
        android:id="@+id/rlbtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rlpwd"
        android:layout_margin="5dp" >

        <Button
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/register"
            android:layout_marginLeft="60dp"
            android:textSize="22sp" />
       
        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/login"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/register"
            android:textSize="22sp"/>
    </RelativeLayout>

</RelativeLayout>
里面控件的文字说明在/res/values/strings.xml中,其内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">我的记事本</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
   
    <string name="name">用户名:</string>
    <string name="namehint">请输入用户名</string>
    <string name="pwd">密&#160;&#160;&#160;&#160;码:</string>
    <string name="pwdhint">请输入密码</string>
    <string name="register">注册</string>
    <string name="login">登录</string>
    <string name="repwd">确认密码:</string>
    <string name="repwdhint">请再次输入密码</string>

</resources>
接下来是注册界面,其实注册界面和登录界面很像。具体做法如下:在/res/layout文件夹下添加文件activity_register.xml文件,文件内容如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical" >

    <RelativeLayout
        android:id="@+id/rlname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" >

        <TextView
            android:id="@+id/tvname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="60dp"
            android:text="@string/name"
            android:textSize="20sp" />
       
        <EditText
            android:id="@+id/name"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:hint="@string/namehint"
            android:layout_toRightOf="@id/tvname"
            android:textSize="18sp"/>
    </RelativeLayout>
   
    <RelativeLayout
        android:id="@+id/rlpwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rlname"
        android:layout_margin="5dp" >

        <TextView
            android:id="@+id/tvpwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/pwd"
            android:layout_marginLeft="60dp"
            android:textSize="20sp" />
       
        <EditText
            android:id="@+id/pwd"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:hint="@string/pwdhint"
            android:layout_toRightOf="@id/tvpwd"
            android:textSize="18sp"/>
    </RelativeLayout>
   
    <RelativeLayout
        android:id="@+id/rlrepwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rlpwd"
        android:layout_margin="5dp" >

        <TextView
            android:id="@+id/tvrepwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/repwd"
            android:layout_marginLeft="60dp"
            android:textSize="20sp" />
       
        <EditText
            android:id="@+id/repwd"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:hint="@string/repwdhint"
            android:layout_toRightOf="@id/tvrepwd"
            android:textSize="18sp"/>
    </RelativeLayout>
   
    <RelativeLayout
        android:id="@+id/rlbtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rlrepwd"
        android:layout_margin="5dp" >

        <Button
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/register"
            android:layout_marginLeft="100dp"
            android:textSize="22sp" />
       
    </RelativeLayout>

</RelativeLayout>
布局文件已经完成,接下来是activity。
第三,添加acticity,我们在src下新建一个包com.exceptionhelp.activity,同时新建两个java文件,LoginActivity.java和RegisterActivity.java。
LoginActivity.java文件内容如下:
package com.exceptionhelp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class LoginActivity extends Activity {
private Button register = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

register = (Button) findViewById(R.id.register);
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
RegisterActivity.java文件内容如下:
package com.exceptionhelp.activity;

import android.app.Activity;
import android.os.Bundle;
public class RegisterActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
}
现在如果运行程序,是不行的,因为我们新添加了acticity,而且我们工程的入口应该是LoginActivity而不是MainActivity,所以我们需要改一下AndroidManifest.xml。其内容如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exceptionhelp.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.exceptionhelp.activity.LoginActivity"
            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
            android:name="com.exceptionhelp.activity.RegisterActivity"></activity>
       
    </application>

</manifest>
源码下载地址 http://www.exceptionhelp.com/posts/542

猜你喜欢

转载自exceptionhelp.iteye.com/blog/2045710