Android使用者介面 UI組件–TextView及其子類(五) DigitalClock,AnalogClock,RadioButton,CheckBox,ToggleButton匯總

來源:互聯網
上載者:User

DigitalClock和AnalogClock兩個時鐘類

可以為DigitalClock設定背景圖片,自訂時針,秒針,分針的樣式

例子:

<?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"android:gravity="center_horizontal"><!-- 定義類比時鐘 --><AnalogClock  android:layout_width="wrap_content" android:layout_height="wrap_content" /><!-- 定義數字時鐘 --><DigitalClockandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14pt"android:textColor="#f0f"android:drawableRight="@drawable/ic_launcher"/><!-- 定義類比時鐘,並使用自訂表格盤、時針圖片 --><AnalogClock  android:layout_width="wrap_content" android:layout_height="wrap_content" android:dial="@drawable/watch"android:hand_minute="@drawable/hand"/></LinearLayout>

運行結果:

RadioGroup單選組RadioGroup.

OnCheckedChangeListener()監聽選項按鈕點擊事件

注意:各個RadioButton之間的id不能重複,RadioButton的id與RadioGroup的id也不能重複.

ToggleButton

常用屬性:

android:textOff未選中時按鈕的文本

android:textOn選中時按鈕的文本

android:checked="true"  該狀態用於設定初始狀態,true表示開,false表示關,若沒有設定該狀態則預設為關android:background="@drawable/toggleselector"  如果要想改變ToggleButton開和關的圖片,則需要設定這個屬性,toggleselector將放到下面,是一個selector設定檔監聽事件:CompoundButton.OnCheckedChangeListener()

重要補充:在xml中設定文字有時候間距什麼的不好控制,這個時候可以把文字寫在圖片裡面,在xml中把texton和textoff都設定為“”

執行個體:

package com.light.study.android;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.CheckBox;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity {private RadioGroup gender;private CheckBox swimming, reading, basketball;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();initListener();}private void init() {gender = (RadioGroup) findViewById(R.id.myRadioGroup);swimming = (CheckBox) findViewById(R.id.swimming);reading = (CheckBox) findViewById(R.id.reading);basketball = (CheckBox) findViewById(R.id.basketball);}private void initListener() {gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if (checkedId == R.id.female) {Toast.makeText(MainActivity.this, "性別:女", Toast.LENGTH_LONG).show();} else if (checkedId == R.id.male) {Toast.makeText(MainActivity.this, "性別:男", Toast.LENGTH_LONG).show();}}});swimming.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "愛好:遊泳", Toast.LENGTH_LONG).show();}});reading.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "愛好:閱讀", Toast.LENGTH_LONG).show();}});basketball.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "愛好:籃球", Toast.LENGTH_LONG).show();}});}}

<?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:gravity="center_horizontal"    android:orientation="vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="性別:" />        <!-- 建立一個RadioGroup -->        <RadioGroup            android:id="@+id/myRadioGroup"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="horizontal" >            <!-- 第一個RadioButton -->            <RadioButton                android:id="@+id/female"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="女" />            <!-- 第二個RadioButton -->            <RadioButton                android:id="@+id/male"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="男" />        </RadioGroup>    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="愛好:" />        <CheckBox            android:id="@+id/swimming"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="遊泳" />        <CheckBox            android:id="@+id/basketball"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="籃球" />        <CheckBox            android:id="@+id/reading"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="閱讀" />    </LinearLayout></LinearLayout>

:



聯繫我們

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