android之常用控制項

來源:互聯網
上載者:User

android中的組件可謂應有盡有,從簡單的Button到複雜的WebView,無所不能,無所不有。
本案例通過一個註冊介面儘可能多的為大家展示一些控制項的使用。本案例涉及的控制項有:TextView、EditText、RadioButton、Button、ToggleButton、CheckBox、Spinner等。
本案例整體的布局結構如:


 
MainActivity.java中main.xml的代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id="@+id/tvTitleRegister"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="使用者註冊介面"
    android:textColor="#EEE"
    android:textStyle="bold"
    android:textSize="28sp"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    />
<TextView
    android:id="@+id/tvUserName"
    style="@style/TextViewTitleWidgetStyle"
    android:text="使用者名稱稱:"
    android:layout_below="@+id/tvTitleRegister"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="8dp"
    />
<EditText
    android:id="@+id/etUserName"
    style="@style/EditTextInputWidgetStyle"
    android:layout_toRightOf="@+id/tvUserName"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/tvTitleRegister"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    />
<TextView
    android:id="@+id/tvUserPassword"
    style="@style/TextViewTitleWidgetStyle"
    android:text="使用者密碼:"
    android:layout_below="@+id/tvUserName"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="26dp"
    />
<EditText
    android:id="@+id/etUserPassword"
    style="@style/EditTextInputWidgetStyle"
    android:layout_toRightOf="@+id/tvUserPassword"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/etUserName"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginTop="5dp"
    />
<TextView
    android:id="@+id/tvSex"
    style="@style/TextViewTitleWidgetStyle"
    android:text="性  別:"
    android:layout_below="@+id/tvUserPassword"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="25dp"
    />
<RadioGroup
    android:id="@+id/radioSexGroup"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/etUserPassword"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_toRightOf="@+id/tvSex"
    android:orientation="horizontal"
    >
    <RadioButton
        android:id="@+id/radioMale"
        android:text="男"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="5dp"
        />
    <RadioButton
        android:id="@+id/radioFemale"
        android:text="女"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="15dp"
        />
</RadioGroup>
<TextView
    android:id="@+id/tvMarriage"
    style="@style/TextViewTitleWidgetStyle"
    android:text="婚  否:"
    android:layout_below="@+id/tvSex"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="20dp"
    />
<ToggleButton
    android:id="@+id/tbIsGetMarried"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/tvMarriage"
    android:layout_below="@+id/radioSexGroup"
    android:layout_marginLeft="5dp"
    />
<TextView
    android:id="@+id/tvhobbies"
    style="@style/TextViewTitleWidgetStyle"
    android:text="愛  好:"
    android:layout_below="@+id/tvMarriage"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="20dp"
    />
<CheckBox
    android:id="@+id/cb_basketball"
    style="@style/CheckBoxWidgetStyle"
    android:layout_below="@+id/tbIsGetMarried"
    android:layout_toRightOf="@+id/tvhobbies"
    android:text="籃球"
    android:layout_marginLeft="5dp"
    />
<CheckBox
    android:id="@+id/cb_Reading"
    style="@style/CheckBoxWidgetStyle"
    android:layout_below="@+id/tbIsGetMarried"
    android:layout_toRightOf="@+id/cb_basketball"
    android:text="閱讀"
    android:layout_marginLeft="5dp"
    />
<CheckBox
    android:id="@+id/cb_pc"
    style="@style/CheckBoxWidgetStyle"
    android:layout_below="@+id/cb_basketball"
    android:layout_toRightOf="@+id/tvhobbies"
    android:text="電腦"
    android:layout_marginLeft="5dp"
    />
<CheckBox
    android:id="@+id/cb_Sing"
    style="@style/CheckBoxWidgetStyle"
    android:layout_below="@+id/cb_Reading"
    android:layout_toRightOf="@+id/cb_pc"
    android:text="唱歌"
    android:layout_marginLeft="5dp"
    />
<TextView
    android:id="@+id/tvTitle"
    style="@style/TextViewTitleWidgetStyle"
    android:text="職  務:"
    android:layout_below="@+id/tvhobbies"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="60dp"
    />
<Spinner
    android:id="@+id/position"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/tvTitle"
    android:layout_marginLeft="5dp"
    android:layout_below="@+id/cb_Sing"
    />
<Button
    android:id="@+id/bt_OK"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="註冊"
    android:layout_marginLeft="65dp"
    />
<Button
    android:id="@+id/bt_Cancel"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/bt_OK"
    android:text="取消"
    android:layout_marginLeft="5dp"
    />
</RelativeLayout>
values/style.xml檔案
這裡建議一下大家,以後盡量將一些相同控制項的通用屬性集中起來放在style.xml檔案中。
第一:提高代碼複用率
第二:便於後期的維護
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--TextView-->
<style name="TextViewTitleWidgetStyle">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#EEE</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">18sp</item>
</style>
   
<!--EditText-->
<style name="EditTextInputWidgetStyle">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:lines">1</item>
</style>
   
<!--CheckBox-->
<style name="CheckBoxWidgetStyle">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>
</resources>
2011.10.19
jack.li
QQ:523072842

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.