




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
Android移動手機開發(fā)進階(三)常用控件文本顯示組件——TextView
TextView組件的主要功能是用于顯示文本,實際上這種控件主要就是提供了一個標簽的顯示操作,即在界面上顯示文本。TextView直接繼承了View,它還是EditText、Button兩個UI組件類的父類。
TextView類的繼承關(guān)系如下:
java.lang.Object
?android.view.View ?android.widget.TextView
從功能上來看,TextView其實就是一個文本編輯器,只不過是Android關(guān)閉了它的文字編輯功能。如果開發(fā)者想要定義一個可以編輯內(nèi)容的文本框,則可以使用它的子類——EditText,EditText允許用戶編輯文本框中的內(nèi)容。
TextView還派生了一個CheckedTextView,CheckedTextView增加了一個checked狀態(tài),開發(fā)者可通過setChecked(boolean)和isChecked()方法來改變、訪問該組件的checked狀態(tài)。除此之外,CheckedTextView還可以通過setCheckMarkDrawable()方法來設(shè)置它的勾選圖標。TextView常用XML屬性及其對應(yīng)方法(1)TextView和EditText具有很多相似之處,它們之間的最大區(qū)別在于TextView不允許用戶編輯它里面的文本內(nèi)容,而EditText則允許用戶編輯它里面的文件內(nèi)容。No.XML屬性相關(guān)方法說明1android:textsetText(CharSequencetext)設(shè)置該文本框內(nèi)文本的內(nèi)容2android:maxLengthsetFilters(InputFilter[]filters)設(shè)置該文本框的最大字符長度3android:textColorsetTextColor(ColorStateListcolors)設(shè)置該文本框中文本的顏色4android:textSizesetTextSize(floatsize)設(shè)置該文本框中文本字號的大小5android:textStyle設(shè)置該文本框內(nèi)文本的顯示的樣式,如:粗體、斜體等6android:selectAllOnFocussetSelectAllOnFocus(booleanselectAllOnFocus)如果文本框的內(nèi)容可選擇,設(shè)置是否當該文本框獲得焦點時自動選中它里面的所有文本內(nèi)容7android:passwordsetTransformationMethod(TransformationMethodmethod)設(shè)置該文本框是一個密碼框,即按密文方式顯示文本信息TextView常用XML屬性及其對應(yīng)方法(2)No.XML屬性相關(guān)方法說明8android:autoLinksetAutoLinkMask(int)是否將符合指定格式的文本轉(zhuǎn)換為可單擊的超鏈接形式,如:URL鏈接、email、電話號碼等9android:autoTextsetKeyListener(KeyListener)控制是否將URL、E-Mail地址等鏈接自動轉(zhuǎn)換為可單擊的鏈接10android:capitalizesetKeyListener(KeyListener)控制是否將用戶輸入的文本轉(zhuǎn)換為大寫字母。該屬性支持如下屬性值:none:不轉(zhuǎn)換sentences:每個句子的首字母大寫words:每個單詞的首字母大寫characters:每個字母都大寫11android:cursorVisiblesetCursorVisible(boolean)設(shè)置該文本框的光標是否可見12android:digitssetKeyListener(KeyListener)如果該屬性值為true,則該文本框?qū)?yīng)一個數(shù)字輸入方法,并且只接受那些合法字符,即:設(shè)置允許輸入哪些字符,如“1234567890.+-*/%\n()”TextView常用XML屬性及其對應(yīng)方法(3)No.XML屬性相關(guān)方法說明13android:drawableBottomsetCompoundDrawablesWithIntrinsicBounds(int,int,int,int)在文本框內(nèi)文本的底端繪制指定圖像14android:drawableEnd在文本框內(nèi)文本的結(jié)尾處繪制指定圖像15android:drawableLeftsetCompoundDrawablesWithIntrinsicBounds(int,int,int,int)在文本框內(nèi)文本的左邊繪制指定圖像16android:drawablePaddingsetCompoundDrawablesWithIntrinsicBounds(int,int,int,int)設(shè)置文本框內(nèi)文本與圖形之間的間距17android:drawableRightsetCompoundDrawablesWithIntrinsicBounds(int,int,int,int)在文本框內(nèi)文本的右邊繪制指定圖像18android:drawableTopsetCompoundDrawablesWithIntrinsicBounds(int,int,int,int)在文本框內(nèi)文本的頂端繪制指定圖像19android:drawableStart在文本框內(nèi)文本的開始處繪制指定圖像20android:editable設(shè)置該文本框是否允許編輯,TextView組件的該屬性無效果TextView常用XML屬性及其對應(yīng)方法(4)No.XML屬性相關(guān)方法說明21android:ellipsizesetEllipsize(TextUtils.TruncateAt)設(shè)置當前顯示的文本超過了TextView的長度時如何處理文本內(nèi)容。該屬性支持如下屬性值:none:不做任何處理start:在文本開始處截斷,并顯示省略號middle:在文本中間處截斷,并顯示省略號end:在文本結(jié)尾處截斷,并顯示省略號marquee:使用marquee滾動動畫顯示文本22android:emssetEms(int)設(shè)置該組件的寬度,以em為單位23android:fontFamilysetTypeface(Typeface)設(shè)置該文本框內(nèi)文本的字體24android:gravitysetGravity(int)設(shè)置文本框內(nèi)文本的對齊方式25android:heightsetHeight(int)設(shè)置該文本框的高度(以pixel為單位,象素)26android:hintsetHint(int)設(shè)置當該本框框內(nèi)容為空時,文本框內(nèi)默認顯示的提示文本27android:imeActionIdsetImeActionLabel(CharSequence,int)當該文本框關(guān)聯(lián)輸入法時,為輸入法提供EditorInfo.actionId值TextView常用XML屬性及其對應(yīng)方法(5)No.XML屬性相關(guān)方法說明28android:imeActionLabelsetImeActionLabel(CharSequence,int)當該文本框關(guān)聯(lián)輸入法時,為輸入法提供EditorInfo.actionLabel值29android:imeOptionssetImeOptions(int)當該文本框關(guān)聯(lián)輸入法時,為輸入法指定額外的選項30android:includeFontPaddingsetIncludeFontPadding(boolean)設(shè)置是否為字體保留足夠的空間。默認為true31android:inputMethodsetKeyListener(KeyListener)為該文本框指定特點的輸入法。該屬性值為輸入法的全類名32android:inputTypesetRawInputType(int)指定該文本框的類型。該屬性有點類似于HTML中的<input…/>元素的type屬性。該屬性支持大量屬性值,不同屬性值用于指定特定的輸入框,該屬性對于TextView組件無效33android:lineSpacingExtrasetLineSpacing(float,float)控制兩行文本之間的額外間距。與android:lineSpacingMulitiplier屬性結(jié)合使用34android:lineSpacingMulitipliersetLineSpacing(float,float)控制兩行文本之間的額外間距。每行文本為高度×該屬性值+android:lineSpacingExtra屬性值TextView常用XML屬性及其對應(yīng)方法(6)No.XML屬性相關(guān)方法說明35android:linessetLines(int)設(shè)置該文本框默認占幾行36android:linksClickablesetLinksClickable(boolean)控制該文本框的URL、E-Mail等鏈接是否可點擊37android:marqueeRepeatLimitsetMarqueeRepeatLimit(int)設(shè)置marquee動畫重復(fù)的次數(shù)38android:maxEmssetMaxEms(int)指定該文本框的最大寬度(以em為單位)39android:maxHeightsetMaxHeight(int)指定該文本框的最大高度(以pixel為單位)40android:maxLinessetMaxLines(int)指定該文本框最多占幾行41android:maxWidthsetMaxWidth(int)指定該文本框的最大寬度(以pixel為單位)42android:minEmssetMinEms(int)指定該文本框的最小寬度(以em為單位)43android:minHeightsetMinHeight(int)指定該文本框的最小高度(以pixel為單位)44android:minLinessetMinLines(int)指定該文本框最少占幾行45android:minWidthsetMinWidth(int)指定該文本框的最小寬度(以pixel為單位)TextView常用XML屬性及其對應(yīng)方法(7)No.XML屬性相關(guān)方法說明46android:numericsetKeyListener(KeyListener)設(shè)置該文本框關(guān)聯(lián)的數(shù)值輸入法。該屬性支持如下屬性值:integer:指定關(guān)聯(lián)整數(shù)輸入法signed:允許輸入符號的數(shù)值輸入法decimal:允許輸入小數(shù)點的數(shù)值輸入法47android:phoneNumbersetKeyListener(KeyListener)設(shè)置該文本框只能接受電話號碼48android:singleLinesetTransformationMethod設(shè)置該文本框是否為單行模式。如果設(shè)置為true,文本框不會換行49android:scrollHorizontallysetHorizontallyScrolling(boolean)設(shè)置當該文本框不夠顯示全部內(nèi)容時是否允許水平滾動50android:shadowColorsetShadowLayer(float,float,float,int)設(shè)置文本框內(nèi)文本的陰影的顏色51android:shadowDxsetShadowLayer(float,float,float,int)設(shè)置文本框內(nèi)文本的陰影在水平方向的偏移52android:shadowDysetShadowLayer(float,float,float,int)設(shè)置文本框內(nèi)文本的陰影在垂直方向的偏移TextView常用XML屬性及其對應(yīng)方法(8)No.XML屬性相關(guān)方法說明53android:shadowRadiussetShadowLayer(float,float,float,int)設(shè)置文本框內(nèi)文本的陰影的模糊程度。該值越大,陰影越模糊54android:textAllCapssetAllCaps(boolean)設(shè)置是否將文本框的所有字母顯示為大寫字母55android:textAppearance設(shè)置該文本框的顏色、字體、大小等樣式56android:textColorHighlightsetHighlightColor(int)設(shè)置文本框中文本被選中時的顏色57android:textColorHintsetTextColor(int)設(shè)置文本框中提示文本的顏色58android:textColorLinksetTextColor(int)設(shè)置該文本框中鏈接的顏色59android:textIsSelectableisTextSelectedable()設(shè)置該文本框不能編輯時,文本框的文本是否可以被選中60android:textScaleXisTextScaleX(float)設(shè)置該文本框內(nèi)文本在水平方向上的縮放因子TextView常用XML屬性及其對應(yīng)方法(9)No.XML屬性相關(guān)方法說明61android:typefacesetTypeface(Typeface)設(shè)置文本框內(nèi)文本的字體風(fēng)格62android:widthsetWidth(int)設(shè)置該文本框的寬度(以pixel為單位)配置文本顯示框(TextView)<TextView
定義文本顯示框組件
android:id="@+id/mytext1"
定義此文本組件的ID
android:layout_width="match_parent"
寬度為整個容器的寬度
android:layout_height="wrap_content"
高度為文字高度
android:textColor="#FFFF00"
文字顏色黃色的RGB碼
android:textSize="12pt"
設(shè)置文字大小為12像素
android:text="軟件學(xué)院"/>
設(shè)置默認的顯示文本配置文本顯示框(TextView)
<TextView
定義文本顯示框組件
android:id="@+id/mytext2"
定義此文本組件的ID
android:layout_width="match_parent"
寬度為整個容器的寬度
android:layout_height="wrap_content"
高度為文字高度
android:text="網(wǎng)址:"
默認的文本信息
android:layout_margin="30dip"/>
距離左邊30個像素的距離配置文本顯示框(TextView)
<TextView
定義文本顯示框組件
android:id="@+id/mytext3"
定義此文本組件的ID,為Activity程序使用
android:layout_width="match_parent"
寬度為整個容器的寬度
android:layout_height="wrap_content"
高度為文字高度
android:text="谷老師"
設(shè)置顯示文字
android:layout_marginTop="10px"
設(shè)置距離上邊控件距離為10像素
android:maxLength="3"/>
只顯示3個長度文字配置文本顯示框(TextView)
<TextView
定義文本顯示框組件
android:id="@+id/mytext4"
定義此文本組件的ID
android:layout_width="wrap_content"
寬度為圖片寬度
android:layout_height="wrap_content"
高度為圖片高度
android:background="@drawable/logo"
將文本框的背景設(shè)置為圖片
android:text="這是在背景上的文字信息"
設(shè)置顯示文字
android:textStyle="bold"
設(shè)置為粗體文字
android:textColor="#000000"/>
文字顏色為黑色設(shè)置“android:textStyle”屬性設(shè)置文字的顯示風(fēng)格(android:textStyle):正常(normal)粗體(bold)斜體(italic)定義布局管理器,增加鏈接顯示功能<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
定義線型布局管理器
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<TextView
定義文本組件
android:id="@+id/msg"
組件ID,程序中使用
android:layout_width="match_parent"
組件寬度為屏幕寬度
android:layout_height="match_parent"
組件高度為屏幕高度
android:autoLink="all"
如果有網(wǎng)址則進行顯示
android:textColor="#FFFF00"
文字顏色為黃色
android:textSize="45px"
文字大小為45像素
android:text="網(wǎng)址:"/>
默認文字</LinearLayout>使用樣式表文件在Android中為了方便美工對組件進行修飾,也可以使用一些樣式文件對組件顯示進行控制,用戶只需要按照如下的xml文件格式即可定義組件的顯示樣式,格式如下:<?xml
version="1.0"
encoding="utf-8"?><resources>
<style
name="樣式名稱"
parent="父樣式表">
<item
name="定義的屬性">屬性內(nèi)容</item>
</style></resources>定義樣式文件——values/styles.xml<?xml
version="1.0"
encoding="utf-8"?><resources>
<style
name="msg_style">
定義樣式文件
<item
name="android:textSize">45px</item>
文字大小為45像素
<item
name="android:textColor">#FFFF00</item>
文字顏色設(shè)置為黃色
<item
name="android:autoLink">all</item>
顯示文本中的鏈接
<item
name="android:layout_width">match_parent</item>
組件寬度為屏幕寬度
<item
name="android:layout_height">wrap_content</item>
組件高度為文字高度
</style></resources>定義布局管理器——main.xml文件<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
定義線型布局管理器
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<TextView
定義文本顯示組件
android:id="@+id/msg"
組件ID,程序中使用
style="@style/msg_style"
定義組件的樣式文件
android:text="網(wǎng)址:"/>
組件的默認顯示文字</LinearLayout>示例1:不同顏色、字體、帶鏈接文本效果圖如下所示:示例2:圓角邊框、漸變背景的TextView
默認情況下,TextView是不帶邊框的如果想為TextView添加邊框,只能通過其它方法實現(xiàn)——可以考慮為TextView設(shè)置一個背景Drawable,該Drawable只是一個邊框,這樣就實現(xiàn)了帶邊框的TextView。由于可以為TextView設(shè)置背景Drawable對象,因此可以定義Drawable時不僅指定邊框,還可以指定漸變背景,這樣即可為TextView添加漸變背景和邊框。效果圖如下所示:文本編輯框——EditTextEditText與TextView非常相似,文本顯示組件(TextView)的功能只是顯示一些基礎(chǔ)的文字信息,而如果用戶要想定義可以輸入的文本組件以達到很好的人機交互操作,則只能使用編輯框:EditText完成。所以EditText與TextView的最大區(qū)別在于:EditText可以接受用戶輸入,前面列的的TextView屬性中關(guān)于輸入相關(guān)的XML屬性就是為EditText準備的。
EditText類的繼承關(guān)系如下:java.lang.Object ?android.view.View ?android.widget.TextView ?android.widget.EditText
EditText組件最重要的屬性是android:inputType,該屬性相錄于HTML中的<input…/>元素的type屬性,用于EditText為指定類型的輸入組件。android:inputType能接受的屬性值非常豐富,而且隨著Android版本的升級,該屬性值能接受的類型還會增加。
EditText還派生了如下兩個子類:AutoCompleteTextView
帶有自動完成功能的EditText,實際上該組件的命名不太恰當,如果命名成AutoCompleteEditText更好一些。該類能常需要與Adapter結(jié)合使用。ExtractEditText
它并不是UI組件,而是ExitText組件的底層服務(wù)類,負責(zé)提供全屏輸入法支持。在main.xml文件之中定義文本編輯框<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<EditText
定義文本編輯框
android:id="@+id/myet1"
此編輯框ID,為程序中使用
android:layout_width="match_parent"
寬度將填充整個屏幕
android:layout_height="wrap_content"
高度是文字高度
android:text="軟件學(xué)院"
默認文字信息
android:selectAllOnFocus="true"/>
默認選中,并設(shè)為焦點
<EditText
定義文本編輯框
android:id="@+id/myet2"
此編輯框ID,為程序中使用
android:layout_width="match_parent"
寬度將填充整個屏幕
android:layout_height="wrap_content"
高度是文字高度
android:text="網(wǎng)址:"/>
默認文字信息
<EditText
定義文本編輯框
android:id="@+id/myet3"
此編輯框ID,為程序中使用
android:layout_width="match_parent"
寬度將填充整個屏幕
android:layout_height="wrap_content"
高度是文字高度
android:password="true"
密文形式顯示文本
android:text=“用戶登陸密碼”/>
默認文字信息
<EditText
定義文本編輯框
android:id="@+id/myet4"
此編輯框ID,為程序中使用
android:layout_width="match_parent"
寬度將填充整個屏幕
android:layout_height="wrap_content"
高度是文字高度
android:text="51283346"
默認文字信息
android:numeric="integer"/>
只能輸入數(shù)字
<EditText
定義文本編輯框</LinearLayout>在Activity程序中編寫——MyEditTextDemo.javapackageorg.gqy.demo;importandroid.app.Activity;importandroid.os.Bundle;importandroid.widget.EditText;public
classMyEditTextDemoextendsActivity{
privateEditTextedit=null; //文本輸入組件
@Override
public
voidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //父類onCreate()
super.setContentView(R.layout.main); //設(shè)置布局管理器
this.edit=(EditText)super.findViewById(R.id.myet2);//取得組件
this.edit.setEnabled(false); //此文本框不可編輯
}}示例:用戶友好的UI輸入界面
效果如下圖所示:按鈕——ButtonButton繼承了TextView,它主要是在UI界面上生成一個按鈕,該按鈕可以供用戶單擊,當用戶單擊按鈕時,按鈕會觸發(fā)一個onClick事件即單擊事件。
Button類的繼承結(jié)構(gòu)如下:java.lang.Object ?android.view.View ?android.widget.TextView ?android.widget.Button
按鈕使用起來非常容易,可以通過為按鈕指定android:background屬性為按鈕增加背景顏色或背景圖片,如果將背景圖片設(shè)為不規(guī)則的背景圖片,則可以開發(fā)現(xiàn)各種不規(guī)則形狀的按鈕,即如果為Button設(shè)置了背景圖象,則該按鈕的形狀將與該背景圖象一致。如果只是使用普通的背景顏色或背景圖片,那么這些背景是固定的,不會隨著用戶的動作而改變。如果需要將背景顏色、背景圖片,隨用戶動作動態(tài)改變,則可以考慮使用自定義的Drawable對象(可以使用XML進行定義)來實現(xiàn)。
Button生成的按鈕功能非常強大,它可以通過背景來設(shè)置圖片,因此使用Button生成的按鈕不僅可以是普通的文字按鈕,也可以定制任意形狀的按鈕,同時也可以隨用戶交互動作改變按鈕外觀。在布局文件中定義按鈕(1)<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
定義線型布局管理器
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<Button
定義按鈕組件
android:id="@+id/mybut1"
定義此按鈕組件的ID,為Activity程序使用
android:layout_width="match_parent"
寬度為整個容器的寬度
android:layout_height="wrap_content"
高度為文字高度
android:textColor="#FFFF00"
文字的顏色設(shè)置為黃色的RGB碼
android:textSize="12px"
設(shè)置文字大小為12像素
android:text="軟件學(xué)院"/>
設(shè)置默認的顯示文本
</LinearLayout>在布局文件中定義按鈕(2)<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
定義線型布局管理器
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<Button
定義按鈕組件
android:id="@+id/mybut2"
定義此按鈕組件的ID,為Activity程序使用
android:layout_width="match_parent"
寬度為整個容器的寬度
android:layout_height="wrap_content"
高度為文字高度
android:text="網(wǎng)址:"
設(shè)置默認的顯示文本
android:layout_margin="30px"/>
距離左邊30個像素的距離</LinearLayout>在布局文件中定義按鈕(3)<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
定義線型布局管理器
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<Button
定義按鈕組件
android:id="@+id/mybut3"
定義此按鈕組件的ID,為Activity程序使用
android:layout_width="match_parent"
寬度為整個容器的寬度
android:layout_height="wrap_content"
高度為文字高度
android:text="谷老師"
設(shè)置默認的顯示文本
android:layout_marginTop="10px"
設(shè)置距離上邊控件距離為10像素
android:maxLength="3"/>
只顯示3個長度文字</LinearLayout>示例:普通按鈕、圓形按鈕、帶文字的圖片按鈕
為了定義圖片隨用戶動作改變的按鈕,可以考慮使用XML資源文件來定義Drawable對象,再將Drawable對象設(shè)為Button的android:background屬性值,或設(shè)為ImageButton(圖片按鈕)的android:src屬性。效果如下圖所示:單擊該按鈕圖片可切換單選按鈕(RadioButton)、單選按鈕組(RadioGroup)、復(fù)選框(CheckBox)
單選按鈕(RadioButton)在開發(fā)中提供了一種多選一的操作模式,也是經(jīng)常見到的一種組件,例如:在選擇文件編碼的時候只能從多種編碼中選擇一種,或者是選擇性別的時候只能從“男”或“女”之中選擇一個。復(fù)選框(CheckBox)的主要功能是完成復(fù)選框的操作,在用戶輸入信息的時候,可以一次性選擇多個內(nèi)容,例如:用戶在選擇個人興趣愛好的時候一定會存在多個,則此時就直接使用CheckBox即可完成該功能。
RadioButton、CheckBox與普通按鈕不同的是,它們多了一個可選中的功能,因此RadioButton與CheckBox都可額外指定一個android:checked屬性,該屬性用于指定RadioButton與CheckBox初始時是否被選中。
RadioButton與CheckBox的不同之處在于,一組RadioButton只能選中其中的一個,因此RadioButton通常需要與RadioGroup一起使用,用于定義一組單選按鈕,即在這一組中的單選按鈕只能選中一個,如果RadioButton不放在RadioGroup中,則單選按鈕無法實現(xiàn)單選的效果。單選按鈕組(RadioGroup)在Android中可以使用RadioGroup來定義單選鈕組件,此類的繼承結(jié)構(gòu)如下
java.lang.Object ?android.view.View ?android.view.ViewGroup ?android.widget.LinearLayout ?android.widget.RadioGroupRadioGroup類的常用操作方法:No.方法類型描述1publicvoidcheck(intid)普通設(shè)置要選中的單選鈕編號2publicvoidclearCheck()普通清空選中狀態(tài)3publicintgetCheckedRadioButtonId()普通取得選種按鈕的RadioButton的ID4publicvoidsetOnCheckedChangeListener(RadioGroup.OnCheckedChangeListenerlistener)普通設(shè)置單選鈕選中的操作事件單選按鈕(RadioButton)RadioGroup提供的只是一個單選鈕的容器,只有在此容器之中配置多個單選按鈕組件之后才可以使用,而要想設(shè)置單選鈕的內(nèi)容,則需要使用RadioButton類,此類繼承結(jié)構(gòu)如下:java.lang.Object ?android.view.View ?android.widget.TextView ?android.widget.Button ?android.widget.CompoundButton ?android.widget.RadioButton
定義一組單選鈕(1)<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
定義線型布局管理器
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<TextView
定義一個文本信息顯示組件
android:id="@+id/encinfo"
此文本顯示組件的ID,程序使用
android:text="請選擇要使用的文字編碼:"
設(shè)置要顯示的文字
android:textSize="20px"
文字大小為20像素
android:layout_width="match_parent"
文本寬度為屏幕寬度
android:layout_height="wrap_content"/>
文本高度為文字高度
<RadioGroup
定義一個單選組
android:id="@+id/encoding"
此單選組的ID,程序中使用
android:layout_width="match_parent"
此組單選鈕寬度為屏幕寬度
android:layout_height="wrap_content"
此組單選鈕高度為文字高度
android:orientation="vertical"
單選按鈕為垂直排列
android:checkedButton="@+id/gbk">
默認選中ID為“gbk”的按鈕
<RadioButton
定義一個選項鈕
android:id="@+id/utf"
此選項鈕的ID
android:text="UTF編碼"/>
此選項鈕的顯示文字
<RadioButton
定義一個選項鈕
android:id="@+id/gbk"
此選項鈕的ID
android:text="GBK編碼"/>
此選項鈕的顯示文字
</RadioGroup>
</LinearLayout>定義一組單選鈕(2)<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
定義線型布局管理器
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<TextView
文本顯示組件
android:id="@+id/sexinfo"
組件ID,程序中使用
android:text="您的性別是:"
默認顯示文字
android:textSize="20px"
文字大小為20像素
android:layout_width="match_parent"
組件寬度為屏幕寬度
android:layout_height="wrap_content"/>
組件高度為文字高度
<RadioGroup
定義單選鈕
android:id="@+id/sex"
組件ID,程序中使用
android:layout_width="match_parent"
組件寬度為屏幕寬度
android:layout_height="wrap_content"
組件高度為文字高度
android:orientation="horizontal"
組件選項水平排列
android:checkedButton="@+id/male">
默認選中組件ID
<RadioButton
定義選項鈕
android:id="@+id/male"
組件ID,程序中使用
android:text="男"/>
默認文字
<RadioButton
定義選項鈕
android:id="@+id/female"
組件ID,程序中使用
android:text="女"/>
默認文字
</RadioGroup></LinearLayout>復(fù)選框(CheckBox)在Android中定義復(fù)選框,可以使用CheckBox類,此類繼承結(jié)構(gòu)如下:
java.lang.Object?android.view.View ? android.widget.TextView ?android.widget.Button ? android.widget.CompoundButton ? android.widget.CheckBoxCheckBox類的常用方法No.方法類型描述1publicCheckBox(Contextcontext)構(gòu)造實例化CheckBox組件2publicvoidsetChecked(booleanchecked)普通設(shè)置默認選中定義復(fù)選框(1)<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
定義線型布局管理器
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<TextView
定義文本顯示組件
android:id="@+id/info"
此組件的ID,程序中使用
android:text="您經(jīng)常瀏覽的網(wǎng)站是:"
顯示的文本信息
android:textSize="20px"
文字大小為20像素
android:layout_width="match_parent"
文本顯示組件寬度為屏幕寬度
android:layout_height="wrap_content"
/>
文本顯示組件高度為文字高度
<CheckBox
定義復(fù)選框
android:id="@+id/url1"
此組件ID,程序中使用
android:text=""
組件的顯示文字
android:layout_width="match_parent"
組件的寬度為屏幕寬度
android:layout_height="wrap_content"
/>
組件的高度為文字高度
<CheckBox
定義復(fù)選框
android:id="@+id/url2"
此組件ID,程序中使用
android:text=""
組件的顯示文字
android:layout_width="match_parent"
組件的寬度為屏幕寬度
android:layout_height="wrap_content"
組件的高度為文字高度
android:checked="true"
/>
設(shè)置為默認選中
<CheckBox
定義復(fù)選框
android:id="@+id/url3"
此組件ID,程序中使用
android:layout_width="match_parent"
組件的寬度為屏幕寬度
android:layout_height="wrap_content"
/>
組件的高度為文字高度</LinearLayout>通過程序操作復(fù)選框組件——MyCheckBoxDemo.javapackageorg.lxh.demo;importandroid.app.Activity;importandroid.os.Bundle;importandroid.widget.CheckBox;public
classMyCheckBoxDemoextendsActivity{
privateCheckBoxbox=null; //復(fù)選框
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState); //父類onCreate()方法
super.setContentView(R.layout.main); //調(diào)用布局管理器
this.box=(CheckBox)super.findViewById(R.id.url3); //取得組件
box.setChecked(true); //設(shè)置為默認選中
box.setText(""); //設(shè)置顯示的文字信息
}}示例:利用單選按鈕、復(fù)選框獲取用戶信息效果如下圖所示:狀態(tài)開關(guān)按鈕(ToggleButton)與開關(guān)按鈕(Switch)
狀態(tài)開關(guān)按鈕(ToggleButton)與開關(guān)按鈕(Switch)也是由Button派生出來的,因此它們的本質(zhì)也是按鈕,Button支持的各種屬性、方法也適用于ToggleButton和Switch。從功能上來看,ToggleButton、Switch與CheckBox復(fù)選框非常相似,它們都可以提供兩個狀態(tài)。不過ToggleButton、Switch與CheckBox的區(qū)主要體現(xiàn)在功能上,ToggleButton、Swithc通常用于切換程序中的某種狀態(tài)。
ToggleButton支持的常用XML屬性及相關(guān)方法XML屬性相關(guān)方法說明android:checkedsetChecked(boolean)設(shè)置該按鈕是否被選中android:textOff設(shè)置該按鈕的狀態(tài)關(guān)閉時顯示的文本android:textOn設(shè)置該按鈕的狀態(tài)打開時顯示的文本狀態(tài)開關(guān)按鈕(ToggleButton)與開關(guān)按鈕(Switch)Switch支持的常用XML屬性及相關(guān)方法XML屬性相關(guān)方法說明android:checkedsetChecked(boolean)設(shè)置該按鈕是否被選中android:textOffsetTextOff(CharSequence)設(shè)置該按鈕的狀態(tài)關(guān)閉時顯示的文本android:textOnsetTextOn(CharSequence)設(shè)置該按鈕的狀態(tài)打開時顯示的文本android:switchMinWidthsetSwitchMinWidth(int)設(shè)置開關(guān)按鈕的最小寬度android:switchPaddingsetSwitchPadding(int)設(shè)置開關(guān)按鈕與標題文本之間的距離android:switchTextAppearancesetSwitchTextAppearance(Context,int)設(shè)置開關(guān)按鈕圖標上的文本樣式android:textStylesetSwitchTypeface(Typeface)設(shè)置開關(guān)按鈕的文本風(fēng)格android:thumbsetThumbResource(int)指定使用自定義Drawable繪制該開關(guān)按鈕的按鈕android:tracksetTrackResource(int)指定使用自定義Drawable繪制該開關(guān)按鈕的軌道android:typefacesetSwitchTypeface(Typeface)設(shè)置該開關(guān)按鈕的文本的字體風(fēng)格示例:使用狀態(tài)開關(guān)按鈕與開關(guān)按鈕動態(tài)控制布局管理器
效果如下圖所示:本例中,隨著狀態(tài)開關(guān)按鈕或開關(guān)按鈕的改變,按鈕上的文字會根據(jù)按鈕的狀態(tài)改變,這主要是通過android:textOff和android:textOn兩個屬性實現(xiàn)的。圖片顯示組件——ImageViewImageView繼承自View組件,它的主要功能是用于顯示圖片,但是實際上它不僅僅能顯示圖片,任何的Drawable對象都可以使用ImageView組件來顯示。除此之外,ImageView還派生了ImageButton、ZoomButton等組件,因此ImageView支持的XML屬性及方法,基本上也可以應(yīng)用于ImageButton和ZoomButton等組件。
android.widget.ImageView類的繼承結(jié)構(gòu)如下:java.lang.Object ?android.view.View ?android.widget.ImageViewImageView常用XML屬性及其對應(yīng)方法XML屬性相關(guān)方法說明android:maxHeightsetMaxHeight(intmaxHeight)設(shè)置ImageView的最大高度android:maxWidthsetMaxWidth(intmaxWidth)設(shè)置ImageView的最大寬度android:srcsetImageResource(intresId)設(shè)置ImageView所顯示的Drawable對象的ID(即把圖片的ID號賦給該屬性)android:adjustViewBoundssetAdjustViewBounds(boolean)設(shè)置ImageView是否調(diào)整自己的邊界來保持所顯示圖片的長寬比android:cropToPaddingsetCropToPadding(boolean)如果將該屬性設(shè)為true,該組件將會被裁剪到保留該ImageView的paddingandroid:scaleTypesetScaleType(ImageView.ScaleType)設(shè)置所顯示的圖片如何縮放或移動以適應(yīng)ImageView的大小補充:android:scalType與ImageView.ScaleType類中常量對應(yīng)關(guān)系XML屬性值對應(yīng)的常量名說明centerCENTER把圖片放在ImageView的中間,但不進行任何縮放centerCropCENTER_CROP保持縱橫比縮放圖片,以使得圖片能完全覆蓋ImageView。只要圖片的最短邊能顯示出來即可。centerInsideCENTER_INSIDE保持縱橫比縮放圖片,以使得ImageView能完全顯示該圖片fitCenterFIT_CENTER保持縱橫比縮放圖片,直到該圖片能完全顯示在ImageView中(圖片較長的邊長與ImageView相應(yīng)的邊長相等),縮放完成后將該圖片放在ImageView的中央。fitEndFIT_END保持縱橫比縮放圖片,直到該圖片能完全顯示在ImageView中(圖片較長的邊長與ImageView相應(yīng)的邊長相等),縮放完成后將該圖片放在ImageView的右下角。fitStartFIT_START保持縱橫比縮放圖片,直到該圖片能完全顯示在ImageView中(圖片較長的邊長與ImageView相應(yīng)的邊長相等),縮放完成后將該圖片放在ImageView的左上角。fitXYFIT_XY對圖片橫向、縱向獨立縮放,使得該圖片完全適應(yīng)于該ImageView,圖片的縱橫比可能會改變。matrixMATRIX使用matrix方式進行縮放在布局文件中定義ImageView組件<?xml
version="1.0"
encoding="utf-8"?><LinearLayout
定義線型布局管理器
xmlns:android="/apk/res/android"
android:orientation="vertical"
所有組件垂直擺放
android:layout_width="match_parent"
布局管理器寬度為屏幕寬度
android:layout_height="match_parent">
布局管理器高度為屏幕高度
<ImageView
定義圖片顯示組件
android:id="@+id/img"
此組件ID,程序中使用
android:src="@drawable/m3g"
從drawable中讀取圖片ID
android:layout_width="match_parent"
組件的寬度為屏幕寬度
android:layout_height="wrap_content"
/>
組件的高度為圖片高度</LinearLayout>示例:簡易圖片瀏覽器效果如下圖所示:圖片按鈕——ImageButtonButton與ImageButton的區(qū)別在于,Button生成的按鈕上顯示文字,而ImageButton上則顯示圖片。需要說明的是:為ImageButton按鈕即使設(shè)定了文字(即設(shè)置了android:text屬性),也是沒有用的(因為ImageButton本質(zhì)上是一個ImageView,所以圖片按鈕上不會顯示文字的)。使用ImageButton圖片按鈕,則可以指定android:src屬性,該屬性既可以使用靜態(tài)圖片,也可以使用自定義的Drawable對象,這樣就可以開發(fā)出隨用戶動作改變的圖片按鈕了。
ImageButton類的繼承結(jié)構(gòu)如下:java.lang.Object ?android.view.View ?android.widget.ImageView ?android.widget.ImageButton示例:圓形的ImageButton按鈕效果如下圖所示:AdapterView及子類AdapterView組件是一組重要的UI組件,AdapterView本身是一個抽象基類,它派生的子類在用法上十分相似,只是顯示界面有一定的區(qū)別。
AdapterView具有如下特征:AdapterView繼承了ViewGroup,所以它的本質(zhì)是容器AdapterView可以包括多個“列表項”,并將多個“列表項”以合適的形式顯示出來AdapterView顯示的多個“列表項”由Adapter提供。調(diào)用AdapterView的setAdapter(Adapt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 勞務(wù)合同補充協(xié)議合同范本
- 單位房屋借用合同范本
- 勞動使用期合同范本
- 利用合同范本掙錢
- 上海徐匯金杯租車合同范本
- 監(jiān)控弱電維護合同范本
- 醫(yī)院電動車租售合同范本
- 備案的借住合同范本
- 單位之間借支合同范本
- 2003勞務(wù)合同范本
- 2024年湖南環(huán)境生物職業(yè)技術(shù)學(xué)院高職單招職業(yè)技能測驗歷年參考題庫(頻考版)含答案解析
- 《化工流程教案》課件
- 后循環(huán)缺血治療
- 體育學(xué)科核心素養(yǎng)解析
- 2024年浙江紹興杭紹臨空示范區(qū)開發(fā)集團有限公司招聘筆試真題
- 2025年體檢科醫(yī)療質(zhì)量控制工作計劃
- 2024年萍鄉(xiāng)衛(wèi)生職業(yè)學(xué)院單招職業(yè)適應(yīng)性測試題庫參考答案
- 飛行器小學(xué)生課件
- 無人機法律法規(guī)與安全飛行 第2版2-2 領(lǐng)空
- 《單片機應(yīng)用實訓(xùn)教程》課件第4章
- 應(yīng)急突發(fā)處置
評論
0/150
提交評論