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

接下来几天我们会用android来写一个简单的记事本例子。记事本的基本功能包括:登录注册,最新记事,所有记事,添加记事,管理记事等。

几天我们先来介绍记事本的第一个模块--登录注册界面。

第一,我们新建一个工程MyNotepad。

第二,添加布局文件,在/res/layout文件夹下添加一个文件activity_login.xml,文件内容如下(在文件中会使用到/res/values文件加下的strings.xml文件,请参考下面strings.xml):

<RelativeLayoutxmlns: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中,其内容如下:

<?xmlversion="1.0"encoding="utf-8"?>

<resources>

<stringname="app_name">我的记事本</string>

<stringname="action_settings">Settings</string>

<stringname="hello_world">Helloworld!</string>

<stringname="name">用户名:</string>

<stringname="namehint">请输入用户名</string>

<stringname="pwd">密&#160;&#160;&#160;&#160;码:</string>

<stringname="pwdhint">请输入密码</string>

<stringname="register">注册</string>

<stringname="login">登录</string>

<stringname="repwd">确认密码:</string>

<stringname="repwdhint">请再次输入密码</string>

</resources>

接下来是注册界面,其实注册界面和登录界面很像。具体做法如下:在/res/layout文件夹下添加文件activity_register.xml文件,文件内容如下:

<RelativeLayoutxmlns: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文件内容如下:

packagecom.exceptionhelp.activity;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.os.Bundle;

importandroid.view.Menu;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

publicclassLoginActivityextendsActivity{

privateButtonregister=null;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_login);

register=(Button)findViewById(R.id.register);

register.setOnClickListener(newOnClickListener(){

publicvoidonClick(Viewv){

Intentintent=newIntent(LoginActivity.this,RegisterActivity.class);

LoginActivity.this.startActivity(intent);

}

});

}

@Override

publicbooleanonCreateOptionsMenu(Menumenu){

//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.

getMenuInflater().inflate(R.menu.main,menu);

returntrue;

}

}

RegisterActivity.java文件内容如下:

packagecom.exceptionhelp.activity;

importandroid.app.Activity;

importandroid.os.Bundle;

publicclassRegisterActivityextendsActivity{

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_register);

}

}

现在如果运行程序,是不行的,因为我们新添加了acticity,而且我们工程的入口应该是LoginActivity而不是MainActivity,所以我们需要改一下AndroidManifest.xml。其内容如下:

<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns: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>

<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<activity

android:name="com.exceptionhelp.activity.RegisterActivity"></activity>

</application>

</manifest>

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

相关推荐