您的位置:首页 > 职场人生

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

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

效果图:



1.添加注册页面

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







点击完成,页面添加完毕. 在页面中添加控件,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文件夹中的包文件,添加后台类,如下图





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

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





点击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




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: