上一篇我们介绍了登陆页面的布局,这一节我们看看注册页面的布局,实际上页面布局大同小异,来一起熟悉下基本控件的用法。
效果图:
![](https://img-my.csdn.net/uploads/201411/04/1415072023_4935.jpg)
1.添加注册页面
右键选中layout文件夹,添加注册页面.如下图
![](https://img-my.csdn.net/uploads/201411/04/1415072024_8602.png)
![](https://img-my.csdn.net/uploads/201411/04/1415072007_3007.jpg)
![](https://img-my.csdn.net/uploads/201411/04/1415072007_1359.jpg)
点击完成,页面添加完毕. 在页面中添加控件,XML代码如下
页面与后台代码关联
熟悉MVC的朋友都知道,MVC中页面与后台代码是分离的,Android中同样也是这样的,这样做的好处是前台UI设计和后台程序开发彻底分离。
右键选中src文件夹中的包文件,添加后台类,如下图
![](https://img-my.csdn.net/uploads/201411/04/1415072006_3490.png)
![](https://img-my.csdn.net/uploads/201411/04/1415072008_4965.jpg)
点击完成按钮后,我们成功的创建一个后台类,但此时后台类还没有与前台页面关联,我们需要重载类中的OnCreate方法,实现与页面关联
重载步骤如下,在类文件空白处右键单击
![](https://img-my.csdn.net/uploads/201411/04/1415072006_6058.png)
![](https://img-my.csdn.net/uploads/201411/04/1415072023_5111.png)
点击OK,方法添加成功。
我们在OnCreate方法中添加setContentView(R.layout.register);来关联前台页面,
全部代码如下:
- public class Register extends Activity {
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.register);
-
- }
-
- }
2.页面跳转
上一篇文章我们做了一个登陆页面,在登陆页面有一个注册按钮,我们来看看如果从登陆页面跳转到注册页面。登陆页面代码如下:
- package com.example.helloword;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class MainActivity extends Activity {
-
-
- private Button btnRegister;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnRegister=(Button)findViewById(R.id.btnRegeister);
- btnRegister.setOnClickListener(new OnClickListener()
- {
-
- @Override
- public void onClick(View arg0) {
-
- Intent intent = new Intent(MainActivity.this,Register.class);
- startActivity(intent);
- }
-
-
- });
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- }
注:每次在android项目中添加一个页面,都要在AndroidMainfest.xml中添加相应activity,代码如下
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.helloword.MainActivity"
- 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.example.helloword.Register">
- </activity>
- </application>
DEMO下载:http://download.csdn.net/detail/zx13525079024/8118723