.Net程序猿乐Android开发---(4)注册页面布局

时间:2021-11-13 21:09:03

接下来我们介绍的登陆页面布局,在本节中,我们看一下注册页面布局,页面布局大同小异,来一起熟悉下基本控件的使用方法。

效果图:

.Net程序猿乐Android开发---(4)注册页面布局

1.加入注冊页面

右键选中layout目录,加入注冊页面.例如以下图

.Net程序猿乐Android开发---(4)注册页面布局

.Net程序猿乐Android开发---(4)注册页面布局

.Net程序猿乐Android开发---(4)注册页面布局

点击完毕,页面加入完毕. 在页面中加入控件。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"> <LinearLayout android:layout_width="fill_parent" android:layout_weight="0.9" android:layout_height="fill_parent">
   </LinearLayout>    <LinearLayout
android:layout_width="fill_parent"
android:layout_weight="0.1"
android:orientation="vertical"
android:layout_height="fill_parent">   <LinearLayout
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:gravity="center"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">    <TextView android:textSize="8pt"
android:text="username"
android:layout_weight="0.75"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
  
   </TextView>
  
   <EditText
android:layout_weight="0.25"
android:layout_width="fill_parent"
android:text="请输入username"
android:layout_height="wrap_content">
  
   </EditText>    </LinearLayout>   <LinearLayout
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:gravity="center"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">    <TextView android:textSize="8pt"
android:text="password"
android:layout_weight="0.75"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
  
   </TextView>    <EditText android:layout_weight="0.25" android:layout_width="fill_parent" android:password="true" android:text="请输入password" android:layout_height="wrap_content">
   </EditText>    </LinearLayout>   <LinearLayout
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:gravity="center"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">    <TextView android:textSize="8pt"
android:text="确认password"
android:layout_weight="0.75"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
  
   </TextView>    <EditText android:layout_weight="0.25" android:layout_width="fill_parent" android:password="true" android:text="请输入password" android:layout_height="wrap_content">
   </EditText>    </LinearLayout> <LinearLayout android:layout_marginLeft="10px" android:layout_marginRight="10px" android:gravity="center" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">
   <Button android:text="注冊 " android:textSize="9pt" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button>
   </LinearLayout>    <LinearLayout
android:id="@+id/btnLogin"
android:layout_marginLeft="10px" android:layout_marginRight="10px" android:gravity="center"
android:layout_width="fill_parent"
android:orientation="horizontal" android:layout_height="wrap_content">
   <Button android:text="登录" android:textSize="9pt" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button>
   </LinearLayout>     </LinearLayout> </LinearLayout>

页面与后台代码关联

                         
熟悉MVC的朋友都知道。MVC中页面与后台代码是分离的。Android中相同也是这种,这样做的优点是前台UI设计和后台程序开发彻底分离。

右键选中src目录中的包文件。加入后台类,例如以下图

.Net程序猿乐Android开发---(4)注册页面布局

.Net程序猿乐Android开发---(4)注册页面布局

点击完毕button后,我们成功的创建一个后台类。但此时后台类还没有与前台页面关联,我们须要重载类中的OnCreate方法,实现与页面关联

重载过程例如以下,在类文件空白处右键单击

.Net程序猿乐Android开发---(4)注册页面布局

.Net程序猿乐Android开发---(4)注册页面布局

点击OK,方法加入成功。

我们在OnCreate方法中加入setContentView(R.layout.register);来关联前台页面,

所有代码例如以下:

public class Register extends Activity {

	@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register); } }

2.页面跳转

上一篇文章我们做了一个登陆页面,在登陆页面有一个注冊button,我们来看看假设从登陆页面跳转到注冊页面。

登陆页面代码例如以下:

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;//声明注冊button
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRegister=(Button)findViewById(R.id.btnRegeister);//找到页面中的注冊button
btnRegister.setOnClickListener(new OnClickListener() //绑定注冊button单击事件
{ @Override
public void onClick(View arg0) {
// button跳转
Intent intent = new Intent(MainActivity.this,Register.class);
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;
} }

注:每次在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