實現登陸頁面並跳轉和簡單的注冊頁面
首先我們來看看布局的xml代碼
login.xml
<span style="font-family:Arial;font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bkcolor" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/topbg2" android:gravity="center_vertical" android:orientation="horizontal" > <Button android:id="@+id/cancel" android:layout_width="65.83dp" android:layout_height="30.5dp" android:layout_marginLeft="10dp" android:background="@drawable/normal_btn" android:text="@string/cancel" android:textColor="#ffffff" /> <TextView android:id="@+id/tv_friend_title" android:layout_width="wrap_content" android:layout_height="50dip" android:layout_weight="1" android:gravity="center" android:text="AppName" android:textColor="#ffffff" android:textSize="20dip" /> <Button android:id="@+id/login_in" android:layout_width="65.83dp" android:layout_height="30.5dp" android:layout_marginRight="10dp" android:background="@drawable/normal_btn" android:text="@string/login_in" android:textColor="#ffffff" /> </LinearLayout> <RelativeLayout android:id="@+id/editRel" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RelativeLayout android:id="@+id/accountRel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:background="@drawable/preference_first_item" android:clickable="true" android:gravity="center_vertical" > <TextView android:id="@+id/account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="@string/account" android:textColor="#000" android:textSize="17sp" /> <EditText android:id="@+id/accountEdittext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dip" android:layout_toRightOf="@id/account" android:background="@null" android:hint="@string/account" android:textSize="15sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/pwdRel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/accountRel" android:background="@drawable/preference_last_item" android:clickable="true" android:gravity="center_vertical" > <TextView android:id="@+id/pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="@string/pwd" android:textColor="#000" android:textSize="17sp" /> <EditText android:id="@+id/pwdEdittext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dip" android:layout_toRightOf="@id/pwd" android:background="@null" android:hint="@string/pwd" android:textSize="15sp" /> </RelativeLayout> </RelativeLayout> <TextView android:id="@+id/sound_help" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dip" android:layout_marginLeft="15dip" android:text="@string/forget_pwd" android:textColor="#29981A" /> </LinearLayout></span>
我們來看第一行,這里主要就是介紹xml的版本信息和編碼格式。
接下來就是重點了,代碼寫好了,我們可以看看寫好代碼這個布局究竟是什么樣子的也便於調試,那么這個時候需要點擊一下下方的Graphical Layout,這個是一個可視化的編輯窗口,我們寫的xml代碼是什么樣子的,這里都可以見到,所以這個非常的方便。基本上這里顯示是什么樣,到了手機實際運行就是什么樣子的,所以大家寫好一個布局樣式的時候都要來回的切換一下,調整細節布局都靠這個了。
首先說明一下常用的5種布局:
1.LinearLayout(線性布局)
2.RelativeLayout(相對布局)
3.AbsoluteLayout(絕對布局)
4.TableLayout(表格布局)
5.FrameLayout(單幀布局)
今天我只會用到其中兩種,那么我就只會講其中兩種啦,至少我現在做的那么多項目的話,用的最最多的其實就是前兩種,基本上可以完成大多數的布局了。
講到這里
我的弟弟又問:那么多布局,布局里面貌似還有啥子長寬高,還有好多屬性哦,怎么記得完記得住啊?這個布局不好搞啊,頁面圖不好畫呀。
我說:確實啊,但是誰要求你記那么多啊?現在開始,用到什么再去查什么,記住基礎的屬性就可以了,其他的還是需要用的時候再去查,這樣才能快速從零開始,每一個你都問為什么去深入,那可不行。很多東西現在講了也不明白,等時間到了,不講你也自己都明白了。不過弄完過后,下來的查資料和思考是必不可少的。並且最最重要的就是動手實驗,自己去敲代碼,才是王道。
好了散打回到我們的布局代碼上來,首先我們就來了一個LinearLayout作為整體的一個布局基調,接下來的東西都寫到這個大的LinearLayout上面。然后這里說明一下,布局是可以嵌套的,意思就是LinearLayout下面還可以嵌套其他的布局或者還是LinearLayout。具體怎么的呢,不急,我們慢慢一步一步的來。
弟弟立刻就問了:xmlns:android=
http://schemas.android.com/apk/res/android這句是什么意思呢?
我說:我都還不知道呢,於是乎,去查,哦原來這個是xml的命名空間,有了他,你就可以alt+/作為提示,提示你輸入什么,不該輸入什么,什么是對的,什么是錯的,也可以理解為語法文件。或者語法判斷器什么的。
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:background=
"@drawable/bkcolor"
android:orientation=
"vertical"
第一句其實就是設置這個寬度,那么值我們填的是填充這個寬度,大家還可以alt+/查看還有什么屬性,到時候會用到其他的
第二句其實也是同樣的是設置高度。
第三句就是整個的背景我們直接引用了一個顏色,同樣的你可以引用你放到drawable-hdpi下面的圖片,其實就相當於我們微信或者有些app啟動的時候的背景圖片。
第四句是設置在這個布局下面的空間采用什么排列方式,我們這里是采用垂直排序的方式,意思就是后面的組件都是從上到下排列,當然如果你選擇horizontal則是從左到右的方式排列。
好了大的背景我們就搭建好了,其實這個時候什么都木有,只是你會看到背景顏色變了。
接下來我們就要添加按鈕,輸入框,文字等內容了。
首先這個時候我們要有一個思維就是,布局都是分塊的,而不是所有的東西都是一個字一個字,一個按鈕一個按鈕來寫的。


這里我們來看看 我們把最上面的titile欄作為一塊,中間的賬號密碼輸入框作為一塊,最下面的忘記密碼為一塊。那么我們就可以知道接下來應該有三塊了,然后首先完成第一塊,根據第一塊,對了這里說明一般的開發流程是,首先是美工會設計整個頁面的樣式,然后切圖,我們才會根據那個來作為參考來布局我們的內容。
最上面的一塊根據他的設計我們看到是三個內容,2個BUTTON,1個TextView。而且是水平排列的,那么就可以利用我們的只是嵌套一個LinearLayout 並且排列方式應該用什么呢?前面說了,當然就用horizontal啦,它就會自動從左到右的排列了
同樣的就不介紹了,不過也是設置按鈕的大小,這里的大小是根據美工切的圖片來計算的,有一個轉換工具就是像素轉換成dip的
下載地址
dip轉換工具
android:id=
"@+id/cancel"
這是為這個按鈕添加一個id號,便於在代碼中找到這個按鈕,為這個按鈕寫對應的功能,這里也就貼出在loginActivity中找到這個按鈕的代碼吧.
Button cancel;
cancel=(Button)findViewById(R.id.
cancel);
cancel.setOnClickListener(
new OnClickListener() {
@Override
public
void onClick(View v) {
//這里做你想你的這個按鈕想做的事情吧。
}
});
android:layout_marginLeft=
"10dp"
這句話的意思就是距離左邊距多遠,一般10-15dp是最好看的距離,反正所有統一就好了。
同樣還有就是按鈕的文字內容我們都寫到strings.xml里面,通過android:text=
"@string/cancel"
這種方式來調用,這樣做的好處是以后萬一有修改就不用一一的去改了。上一篇文章頁介紹了,這里就是實際使用了。大家照着寫吧。
android:textColor=
"#ffffff"
這句話就是文字顏色,這里是黑色,至於其他顏色大家可以去查。
好了其他的都大同小異,相信各位智商一定比我弟弟高,他都能做出來,各位小天才們一定是可以舉一反三的,不然我不是在侮辱各位么不是么。說太多會挨打的。
android:layout_weight=
"1"當然這個還是要講一下,這個是在這個LinearLayout中的一個權重,相當於把整個分成幾份,我這里設置1的目的其實就是居中顯示這個TextView,同時剩下的兩個按鈕分剩下的空間,類似於TextView占了兩份,其他的按鈕各自占一份。或者大家可以試一試其他的方式來達到這個效果,也許我也做錯了。
弟弟:你為什么這樣做呢?可以用其他的方式么?
我:我也不曉得,你記着我給你講的很有可能是錯的,所以你要有懷疑的精神,同時可以試驗其他方法。
預告:后面發生了一件事情證明了這個,所以我還是錯的凶。大家如果我有錯,請指正,我改,我是好孩子,不是那種你給我指出來了,我反正不改,你來打我呀那種人!
預告:后面發生了一件事情證明了這個,所以我還是錯的凶。大家如果我有錯,請指正,我改,我是好孩子,不是那種你給我指出來了,我反正不改,你來打我呀那種人!
接下來講一下RelativeLayout,也就是下面賬號密碼的實現中的關鍵點了
這種布局叫相對布局,那么必然有一個相對的對象
這時我的弟弟問:到底@+id/name和@id/name有什么區別呢?怎么使用呢?
我回答:其實有加號的那個是添加一個id,另外一個是引用那個id。
好了再相對布局中會類似這樣的代碼
android:layout_toRightOf=
"@id/account"
它的意思就是把該空間放到id為account的空間的右邊,當然還有很多參數,還可以放到左邊,放到下面,放到上面等等,大家可以在輸入完android:后按ctrl+/來查看。后面的文章我會做一個詳細的具體的有哪些參數的說明。等用到的時候繼續講解。基本的布局就差不多這樣完成了。再講解跳轉方法之前說一下引用的圖片,大家可能看到了drawable-hdpi中有的圖片的結尾格式是.9.png說明一下個圖片是利用android自帶的工具制作而成的,目的是讓圖片具有較小的體積同時拉伸的時候圖片不會變型,大家先了解有這么一回事吧。
接下來是跳轉相關的代碼了主要就是利用intent,什么時候跳轉呢?我們在點擊了登陸的時候跳轉,我的代碼如下:
Button loginBtn = (Button)findViewById(R.id.
login_in);
loginBtn.setOnClickListener(
new OnClickListener() {
@Override
public
void onClick(View v) {
//
TODO Auto-generated method stub
//這里就可以判斷賬號密碼是否正確了,這里讓大家自己試驗動手一下謝謝如果賬號密碼是admin 123456就成功
//否則就提示登陸失敗,大家試一試吧,我這里直接跳轉了,沒做驗證
//這個是直接跳轉到MainActivity
Intent intent =
new Intent();
intent.setClass(LoginActivity.
this, MainActivity.
class);
startActivity(intent);
}
});
同時留一個作業就是,大家去獲取編輯框的內容,然后以賬號為admin,密碼為123456作為正確賬號,來判斷輸入是否正確,成功了就跳轉,失敗了就彈出提示。
Intent負責對應用中一次操作的動作、動作涉及數據、附加數據進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。intent可以帶參數傳遞,還可以綁定bundle。這些后期會用到我們繼續講 ,這里我們就是單純的實現了跳轉,setClass就是告訴計算機從哪個類到哪個類去,然后調用startActiviy()方法就可以成功的跳轉了。