码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-11-04 13:17:52      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:android开发   页面布局   布局   

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

            效果图:

                           bubuko.com,布布扣

            1.添加注册页面

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

                           bubuko.com,布布扣

                           

                          bubuko.com,布布扣

                           

                           bubuko.com,布布扣

                          点击完成,页面添加完毕. 在页面中添加控件,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="用户名"
    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="请输入用户名"
     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="密码"  
         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="请输入密码" 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="确认密码"  
         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="请输入密码" 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文件夹中的包文件,添加后台类,如下图

                              bubuko.com,布布扣

                      bubuko.com,布布扣

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

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

                       bubuko.com,布布扣


                          bubuko.com,布布扣

                        点击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.页面跳转

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

                         

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) {
		// 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





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

标签:android开发   页面布局   布局   

原文地址:http://blog.csdn.net/zx13525079024/article/details/40781613

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!