.Net程式員安卓學習之路1:登陸介面

來源:互聯網
上載者:User

標籤:

 

任何編程學習起步均是HelloWorld,作為稍有>net編程經驗的我們來說就跳過這步吧,咱們且從簡單登入介面開始。先看看效果:

一、準備知識:

1. 安卓環境:安裝好JDK,直接去官網下載ADT-bundle整合套件後更新即可使用。

2. 項目目錄:一張圖說明一切

二、頁面配置:

還是一幅圖說明一切

那麼這個介面的布局如何呢?

<LinearLayout >最外邊的DIV,用的是線性布局,方向是垂直    <TextView/>就是“初始使用者名稱。。。”這幾個字所在Lable    <LinearLayout >裡面的DIV,水平布局        <TextView/>使用者名稱Lable        <EditText/>使用者名稱TextBox    </LinearLayout>    <LinearLayout>裡面的DIV,水平布局        <TextView/>密碼Lable        <EditText/>密碼TextBox    </LinearLayout>    <Button/>登入按鈕</LinearLayout>

一看,就會一半,下來一個一個看:

 

1. 最外層DIV:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" android:layout_margin="5dip" >

定義寬高的停靠方式,有

fill_parent、match_parent:是一樣的,為了相容低版本,建議使用fill_parent

設定布局/控制項為fill_parent將強制性讓它布滿整個螢幕或填滿父控制項的空白

wrap_content:被內容撐大,剛好能顯示下內容為止

Orientation:相片順序,vertical垂直,預設是HORIZONTAL水平

 

2. 文字框:

<TextViewandroid:id="@+id/lbl_LoginPass"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/lbl_LoginPass_Text"android:textSize="15.0sp" />

這裡出現了兩個未知屬性:

@+id/lbl_LoginPass和@string/lbl_LoginPass_Text

其實完全可以直接寫:

android:text="使用者名稱"

系統會提示這是寫入程式碼,建議改寫(汗,寫了這麼多年寫入程式碼),他意思是這裡僅僅引用字典中一個變數的名稱,具體的值在字典中去維護,那麼字典在哪裡呢?

Res/Values/String.xml中維護了這個字典:

<resources>    <string name="app_name">登入DEMO</string>    <string name="action_settings">Settings</string>    <string name="lbl_LoginName">使用者名稱:</string>    <string name="lbl_LoginPass_Text">密    碼:</string>    <string name="btn_login">開始登陸</string></resources>

這裡編碼規範得注意一下了:假如控制項ID為lbl_LoginPass,則他的字典名應該為lbl_LoginPass_Text為了防止多個頁面的字典名重複建議最好加上頁面首碼,如Login_lbl_LoginPass_Text

完整代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" android:layout_margin="5dip" >            <TextView                android:id="@+id/form_title"                android:layout_width="wrap_content"                   android:layout_height="wrap_content"                android:text="初始使用者名稱和密碼都是123" />        <LinearLayout        android:id="@+id/layout_login_name"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_margin="5.0dip"        android:layout_marginTop="10.0dip"        android:orientation="horizontal" >            <TextView                android:layout_width="wrap_content"                   android:layout_height="wrap_content"                android:text="@string/lbl_LoginName" />            <EditText                android:id="@+id/txt_login_name"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:textSize="15.0sp" />        </LinearLayout>                <LinearLayout        android:id="@+id/login_pwd_layout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/layout_login_name"        android:layout_centerHorizontal="true"        android:layout_margin="5.0dip"        android:orientation="horizontal" >            <TextView                android:id="@+id/login_pass_edit"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/lbl_LoginPass"                android:textSize="15.0sp" />            <EditText                android:id="@+id/txt_login_pwd"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:password="true"                android:textSize="15.0sp" />            </LinearLayout>                       <Button        android:id="@+id/btn_login"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center"        android:onClick="btn_click"        android:text="登陸" /></LinearLayout>

3. 頁面後台
開啟頁面對應的後台代碼:MainActivity.java

手動實現按鈕的點擊事件:

    public void btn_click(View v)    {        TextView lblInfo=(TextView)findViewById(R.id.form_title);                EditText txt_login_name=(EditText)findViewById(R.id.txt_login_name);        EditText txt_login_pass=(EditText)findViewById(R.id.txt_login_pwd);        String loginName=txt_login_name.getText().toString().trim();        String loginPass=txt_login_pass.getText().toString().trim();        if(loginPass.equals("123")&&loginName.equals("123"))        {            lblInfo.setText("登入成功!");        }        else        {            lblInfo.setText("登入失敗!");        }            }

其實這些倒沒啥說的一看名字就知道啥意思。

.Net程式員安卓學習之路1:登陸介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.