.Net程序员玩转Android开发---(4)注册页面布局

上一篇我们介绍了登陆页面的布局,这一节我们看看注册页面的布局,实际上页面布局大同小异,来一起熟悉下基本控件的用法。

            效果图:

                           

            1.添加注册页面

                            右键选中layout文件夹,添加注册页面.如下图

                           

                           

                          

                           

                           

                          点击完成,页面添加完毕. 在页面中添加控件,XML代码如下

                         

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.    
  8.   <LinearLayout android:layout_width="fill_parent"  android:layout_weight="0.9" android:layout_height="fill_parent">  
  9.     </LinearLayout>  
  10.   
  11.   
  12.   
  13.    <LinearLayout   
  14.   android:layout_width="fill_parent"  
  15.   android:layout_weight="0.1"  
  16.   android:orientation="vertical"  
  17.   android:layout_height="fill_parent">  
  18.   
  19.       
  20.       
  21.   <LinearLayout  
  22.     android:layout_marginLeft="10px"  
  23.     android:layout_marginRight="10px"  
  24.     android:gravity="center"  
  25.     android:layout_width="fill_parent"  
  26.     android:orientation="horizontal"  
  27.     android:layout_height="wrap_content">  
  28.       
  29.         <TextView android:textSize="8pt"  
  30.     android:text="用户名"  
  31.     android:layout_weight="0.75"  
  32.     android:layout_width="fill_parent"  
  33.     android:layout_height="wrap_content">  
  34.               
  35.         </TextView>  
  36.           
  37.         <EditText  
  38.      android:layout_weight="0.25"  
  39.      android:layout_width="fill_parent"  
  40.      android:text="请输入用户名"  
  41.      android:layout_height="wrap_content">  
  42.               
  43.         </EditText>  
  44.         
  45.       </LinearLayout>  
  46.   
  47.   
  48.   
  49.   <LinearLayout   
  50. android:layout_marginLeft="10px"  
  51. android:layout_marginRight="10px"  
  52. android:gravity="center"  
  53. android:layout_width="fill_parent"  
  54. android:orientation="horizontal"  
  55. android:layout_height="wrap_content">  
  56.       
  57.         <TextView android:textSize="8pt"   
  58.         android:text="密码"    
  59.          android:layout_weight="0.75"     
  60.           android:layout_width="fill_parent"    
  61.            android:layout_height="wrap_content">  
  62.               
  63.         </TextView>  
  64.         
  65.         <EditText  android:layout_weight="0.25"   android:layout_width="fill_parent"  android:password="true" android:text="请输入密码" android:layout_height="wrap_content">  
  66.         </EditText>  
  67.         
  68.       </LinearLayout>  
  69.   
  70.   
  71.   <LinearLayout   
  72. android:layout_marginLeft="10px"  
  73. android:layout_marginRight="10px"  
  74. android:gravity="center"  
  75. android:layout_width="fill_parent"  
  76. android:orientation="horizontal"  
  77. android:layout_height="wrap_content">  
  78.       
  79.         <TextView android:textSize="8pt"   
  80.         android:text="确认密码"    
  81.          android:layout_weight="0.75"     
  82.           android:layout_width="fill_parent"    
  83.            android:layout_height="wrap_content">  
  84.               
  85.         </TextView>  
  86.         
  87.         <EditText  android:layout_weight="0.25"   android:layout_width="fill_parent"  android:password="true" android:text="请输入密码" android:layout_height="wrap_content">  
  88.         </EditText>  
  89.         
  90.       </LinearLayout>  
  91.   
  92.       
  93.    <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">  
  94.      <Button android:text="注册 "    android:textSize="9pt"  android:layout_width="fill_parent"    android:layout_height="wrap_content">   </Button>  
  95.       </LinearLayout>  
  96.   
  97.   
  98.       <LinearLayout     
  99.    android:id="@+id/btnLogin"  
  100.    android:layout_marginLeft="10px"  android:layout_marginRight="10px" android:gravity="center"     
  101.     android:layout_width="fill_parent"  
  102.      android:orientation="horizontal" android:layout_height="wrap_content">  
  103.      <Button android:text="登录"    android:textSize="9pt"  android:layout_width="fill_parent"    android:layout_height="wrap_content">   </Button>  
  104.       </LinearLayout>  
  105.   
  106.     
  107.   
  108.   
  109.   
  110.   </LinearLayout>  
  111.       
  112. </LinearLayout>  


                       

                          页面与后台代码关联

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

     右键选中src文件夹中的包文件,添加后台类,如下图

                              

                      

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

                     重载步骤如下,在类文件空白处右键单击

                       


                          

                        点击OK,方法添加成功。

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

                         全部代码如下:

                          

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Register extends Activity {  
  2.   
  3.       
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState)   
  6.     {  
  7.         // TODO Auto-generated method stub  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.register);  
  10.           
  11.     }  
  12.   
  13. }  


            2.页面跳转

                         上一篇文章我们做了一个登陆页面,在登陆页面有一个注册按钮,我们来看看如果从登陆页面跳转到注册页面。登陆页面代码如下:

                         

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.helloword;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.       
  14.     private Button btnRegister;//声明注册按钮  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         btnRegister=(Button)findViewById(R.id.btnRegeister);//找到页面中的注册按钮  
  20.         btnRegister.setOnClickListener(new OnClickListener() //绑定注册按钮单击事件  
  21.         {  
  22.   
  23.             @Override  
  24.             public void onClick(View arg0) {  
  25.                 // 按钮跳转  
  26.                  Intent intent = new Intent(MainActivity.this,Register.class);  
  27.                  startActivity(intent);  
  28.             }  
  29.               
  30.               
  31.         });  
  32.     }  
  33.   
  34.     @Override  
  35.     public boolean onCreateOptionsMenu(Menu menu) {  
  36.         // Inflate the menu; this adds items to the action bar if it is present.  
  37.         getMenuInflater().inflate(R.menu.main, menu);  
  38.         return true;  
  39.     }  
  40.   
  41. }  

    

                 注:每次在android项目中添加一个页面,都要在AndroidMainfest.xml中添加相应activity,代码如下

                

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <application  
  2.        android:allowBackup="true"  
  3.        android:icon="@drawable/ic_launcher"  
  4.        android:label="@string/app_name"  
  5.        android:theme="@style/AppTheme" >  
  6.        <activity  
  7.            android:name="com.example.helloword.MainActivity"  
  8.            android:label="@string/app_name" >  
  9.            <intent-filter>  
  10.                <action android:name="android.intent.action.MAIN" />  
  11.   
  12.                <category android:name="android.intent.category.LAUNCHER" />  
  13.            </intent-filter>  
  14.        </activity>  
  15.          
  16.         <activity  
  17.            android:name="com.example.helloword.Register">  
  18.        </activity>  
  19.    </application>  

     DEMO下载:http://download.csdn.net/detail/zx13525079024/8118723
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值