.Net程式員玩轉Android開發---(4)註冊頁面配置,.netandroid
上一篇我們介紹了登陸頁面的布局,這一節我們看看註冊頁面的布局,實際上頁面配置大同小異,來一起熟悉下基本控制項的用法。
:
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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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;//聲明註冊按鈕@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnRegister=(Button)findViewById(R.id.btnRegeister);//找到頁面中的註冊按鈕btnRegister.setOnClickListener(new OnClickListener() //綁定註冊按鈕單擊事件{@Overridepublic void onClick(View arg0) {// 按鈕跳轉 Intent intent = new Intent(MainActivity.this,Register.class); startActivity(intent);}});}@Overridepublic 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
C#語言 開發網站程式 程式員的困惑(net程式員回答)
程式員不是一份工作,在中國寫程式要成為興趣和追求才能堅持到最後。如果不是這樣,你還是不要涉足。
JAVA程式員與C#/NET程式員那個可以更快的開發Android應用程式?
如果源碼java會快一些,如果用像完美e端這樣的工具,.net會快一點