Android菜鳥
常用的基本控件有
屬性名稱 |
作用描述 |
TextView |
顯示文本信息 |
Button |
普通按鈕 |
EditText |
可編輯的文本框組件(輸入框) |
ImageView |
用於顯示圖片 |
ImageButton |
圖片按鈕 |
CheckBox |
復選框 |
RadioGroup |
單選按鈕組 |
Spinner |
下拉列表組件 |
ProgressBar |
進度條 |
SeekBar |
拖動條 |
RatingBar |
評分組件 |
ListView |
列表 |
Diaolog |
對話框 |
Toast |
信息提示組件 |
TextView在xml中的基本屬性有
屬性名稱 |
作用描述 |
android:autoLink |
設置是否當文本為 URL 鏈接/email/電話號碼/map 時,文本顯示為可點擊的鏈接。可選值 (none/web/email/phone/map/all) |
android:digits |
設置允許輸入哪些字符。如“1234567890.+-*/%\n()” |
android:drawableBottom |
在 text 的下方輸出一個 drawable,如圖片。如果指定一個顏色的話會把 text 的背景設為該顏 色,並且同時和 background 使用時覆蓋后者。 |
android:drawablePadding |
設置 text 與 drawable(圖片)的間隔,與 drawableLeft、drawableRight、drawableTop、 drawableBottom 一起使用,可設置為負數,單獨使用沒有效果。 |
android:gravity |
設置文本位置,如設置成“center”,文本將居中顯示。 |
android:hint |
Text 為空時顯示的文字提示信息,可通過 textColorHint 設置提示信息的顏色。比較奇怪的是 TextView 本來就相當於 Label,怎么會不設置 Text?! |
android:marqueeRepeatLimit |
在 ellipsize 指定 marquee 的情況下,設置重復滾動的次數,當設置為 marquee_forever 時表 示無限次。 |
android:lines |
設置文本的行數,設置兩行就顯示兩行,即使第二行沒有數據。 |
android:phoneNumber |
設置為電話號碼的輸入方式。 |
android:singleLine |
設置單行顯示。如果和 layout_width 一起使用,當文本不能全部顯示時,后面用“…”來表示。 如 android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"將 只顯示“t…”。如果不設置 singleLine 或者設置為 false,文本將自動換行 |
android:text |
設置顯示文本. |
android:textSize |
設置文字大小,推薦度量單位”sp”,如”15sp” android:textStyle 設置字形[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2] 可以設置一個或多個,用“|”隔開 |
android:typeface |
設置文本字體,必須是以下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) |
android:height |
設置文本區域的高度,支持度量單位:px(像素)/dp/sp/in/mm(毫米) |
android:width |
設置文本區域的寬度,支持度量單位:px(像素)/dp/sp/in/mm(毫米)。 |
而TextView在類中的使用
方法 |
功能描述 |
返回值 |
TextView |
TextView的構造方法 |
Null |
getDefaultMovementmethod |
獲取默認的箭頭按鍵移動方式 |
Movementmethod |
getText |
獲得TextView 對象的文本 |
CharSquence |
length |
獲得 TextView 中的文本長度 |
Int |
getEditableText |
取得文本的可編輯對象,通過 這個對象可對 TextView 的文本進行操作,如 在光標之后插入字符 |
Void |
setCompoundDrawables |
設置圖像顯示的位置,在 設置該 Drawable 資源之前需要調用 setBounds (Rect) |
Void |
setCompoundDrawablesWithIn trinsicBounds |
設置 Drawable 圖像的顯示位置,但其邊界不變 |
Void |
setTextColor |
設置文本顯示的顏色 |
Void |
setHighlightColor |
設置文本選中時顯示的顏色 |
Void |
setShadowLayer |
設置文本顯示的陰影顏色 |
Void |
setHintTextColor |
設置提示文字的顏色 |
Void |
舉例
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please choose you like !"
android:textSize="40dp"
android:textColor="@android:color/black"
android:id="@+id/yf_1"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"/>
</TextView>
EditText在xml中的基本屬性
屬性名稱 |
作用描述 |
android:inputType |
設置文本類型 |
android:digits |
設置輸入哪些符號 |
android:hint |
設置編輯框為空時顯示的提示信息 |
android:password |
設置只能輸入密碼,以“.”顯示文本 |
android:singleLine |
設置文本單行顯示 |
android:editable |
設置是否可編輯。仍然可以獲取光標,但是無法輸入 |
requestFocus() |
使當前組件對象獲得焦點 |
android:ems |
設置 TextView 的寬度為 N 個字符的寬度 |
而EditText在類中的使用
方法 |
功能描述 |
返回值 |
getDefaultEditable |
獲取是否默認可編輯 |
boolean |
setEllipse |
設置文件過長時控件的顯示方式 |
void |
setFreeezesText |
設置保存文本內容及光標位置 |
void |
getFreeezesText |
獲取保存文本內容及光標位置 |
boolean |
setGravity |
設置文本框在布局中的位置 |
void |
getGravity |
獲取文本框在布局中的位置 |
int |
setHint |
設置文本框為空時,文本框默認顯示的 字符 |
void |
getHint |
獲取文本框為空時,文本框默認顯示的 字符 |
Charsequence |
setIncludeFontPadding |
設置文本框是否包含底部和頂端的額外空白 |
void |
setMarqueeRepeatLimit |
在 ellipsize 指定 marquee 的情況下,設置重復滾動的次數, 當設置為 marquee_forever 時表示無限次 |
void |
舉例
<EditText
android:id="@+id/yf_1"
android:inputType="date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="40"
android:hint=" 輸入電話號碼 "
android:textColorHint="@android:color/black"
android:phoneNumber="true"
android:imeOptions="actionGo">
</EditText>
屬性名稱 |
功能描述 |
android:layout_height |
設置控件高度,可選值:fill_parent,warp_content,px |
android:layout_width |
設置控件寬度,可選值:fill_parent,warp_content,px |
android:text |
設置控件名稱,可以是任意字符 |
android:layout_gravity |
設置控件在布局中的位置,可選項: top,left,bottom,right,center_vertical,fill_vertica,fill_horizonal,center,fill 等 |
android:layout_weight |
設置控件在布局中的比重,可選值:任意的數字 |
android:textColor |
設置文字的顏色 |
android:bufferType |
設置取得的文本類別,normal、spannable、editable |
android:hint |
設置文本為空是所顯示的字符 |
android:textColorHighlight |
設置文本被選中時,高亮顯示的顏色 |
android:inputType |
設置文本的類型,none,text,textWords 等 |
方法 |
功能描述 |
返回值 |
Button |
Button 類的構造方法 |
Null |
onKeyDown |
當用戶按鍵時,該方法調用 |
Boolean |
onKeyUp |
當用戶按鍵彈起后,該方法被調用 |
Boolean |
onKeyLongPress |
當用戶保持按鍵時,該方法被調用 |
Boolean |
onKeyMultiple |
當用戶多次調用時,該方法被調用 | Boolean
invalidateDrawable |
刷新 Drawable 對象 |
void |
scheduleDrawable |
定義動畫方案的下一幀 |
void |
unscheduleDrawable |
取消 scheduleDrawable 定義的動畫方案 |
void |
onPreDraw |
設置視圖顯示,列如在視圖顯示之前調整滾動軸的邊界 |
Boolean |
sendAccessibilityEvent |
發送事件類型指定的 AccessibilityEvent。發送請求之前,需要檢查 Accessibility 是否打開 void sendAccessibilityEventUnchecked 發送事件類型指定的 AccessibilityEvent。發送請求之前,不需要檢查 Accessibility 是否打開 |
void |
setOnKeyListener |
設置按鍵監聽 void |
舉例
<Button
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/yf_2"
android:layout_width="wrap_content"
android:textColor="@android:color/black""
android:text=" 這是 按鈕 , 可點擊 " />
而其它控件在xml中的使用方法都大同小異,接下來說的就是布局
目前所學的線性布局
LinearLayout 的使用
<!-- 水平線性布局 -->
<LinearLayout
android:layout_height="100dip"
android:orientation="horizontal"
android:layout_width="fill_parent">
<TextView
android:background="@android:color/black"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:text=" 第一列 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
<TextView
android:background="@android:color/black"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:text=" 第二列 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
<TextView
android:background="@android:color/black"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:text=" 第三列 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
</LinearLayout>
<!-- 垂直線性布局 -->
<LinearLayout
android:layout_height="100dip"
android:orientation="vertical"
android:layout_width="fill_parent">
<TextView
android:background="@android:color/black"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:text=" 第一行 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
<TextView
android:background="@android:color/black"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:text=" 第二行 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
</LinearLayout>
Dialog對話框
Android 中實現對話框可以使用 AlterDialog.Builder 類,還可以自定義對話框。如果對話框設置了按鈕,那么就需要對其設置監聽 OnClickListener。
Dialog的使用
首先在 Dialog.xml 中定義一個對話框
然后在 Activity 中使用 AlterDialog 創建對話框
發生的錯誤
1.布局有時候會因為一時大意而失去了頭或尾,所以在寫布局時要避免一些因為大意而導致的錯誤
2.有時候因為xml中對於某個控件沒有正確命名而導致類發生了關聯的錯誤
其中有關控件更多的屬性和功能可以看以下的文件
http://pan.baidu.com/s/1kVt5iGf
(出處為百度)
用我家李易峰來給大家賣個萌
