主要內(nèi)容
UI布局--四種布局
UI控件的事件處理
UI常用控件及使用
Toast--吐司
dailoge--對(duì)話框
----------------------------------------------------------------------------
1,View的概念
a,用來(lái)顯示數(shù)據(jù)、影像或是其他信息的組件,組件全部繼承與View
ViewGroup是一種View容器,本身也是一種View,但是可以包含View及其他ViewGroup組件的View,
例如: LinearLayout,ViewGroup繼承自View,所以ViewGroup is-aView的觀念,只是
ViewGroup有容器的特色。
b,UI組件都放在android.widget包,android.view包中,
c,UI編程方式
1.通過(guò)java代碼創(chuàng)建view
2.用xml文件(ui語(yǔ)言)--用的比較多
2,布局對(duì)象
a, activtiy與layout的關(guān)系:
Activity就是布滿整個(gè)窗口或者懸浮于其他窗口上的交互界面
一般一個(gè)Activity都有一個(gè)以上的layout,用于擺放要顯示的組件
線性:LinearLayout
表格:TableLayout
相對(duì):RelativeLayout
幀(框架布局):FrameLayout
絕對(duì):AbsoluteLayout
以上布局都可以嵌套使用
xml布局文件和屬性:
xml布局文件:
1,必須在res/layout目錄
2,xml中的根節(jié)點(diǎn)必須是xmlns:android="http://schemas.android.com/apk/res/android"
XML命名空間,告訴Android開(kāi)發(fā)工具你準(zhǔn)備使用Android命名空間里的一些通用屬性
3,xml中的每個(gè)組件的id會(huì)在R類中生成對(duì)應(yīng)的變量,在代碼中可以引用的到
4,需要在Activity的onCreate方法中調(diào)用setContentView(R.layout.main)顯示xml中的view
xml中的屬性
長(zhǎng)度單位:px,像素,表示屏幕的實(shí)際像素,比如320*480 (很少用)
dp(dip) 是屏幕的物理尺寸, 大小為1英寸的1/72
sp(與刻度無(wú)關(guān)的像素),
技巧:長(zhǎng)度和高度: 可以選擇dp/sp, 如果是字體的話,用sp
android:layout_width="68dp" // 是指定控件的顯示大小的區(qū)域
android:layout_height="94dp"
layout_margin: 是控件邊緣相對(duì)于父空間的邊距,有top,buttom,left,right
(注意哦,如果選擇了對(duì)齊方式,比如居中對(duì)齊,margin=0dp時(shí),是以中間為開(kāi)始的,不是從手機(jī)的頂部開(kāi)始的)
layout_padding: 是控件內(nèi)容相對(duì)于空間邊緣的邊距
layout_gravity : 設(shè)置組件相對(duì)容器(layout)的對(duì)齊方式
gravity: 設(shè)置View組件(即是控件中內(nèi)容)的對(duì)齊方式
"wrap_content" :根據(jù)內(nèi)容的大小而定
match_parent 和fill是一樣的,剛好顯示空間中的內(nèi)容
android:layout_weight: 各個(gè)空間在布局中的比重
@+id/test :為組件設(shè)定id
----------------------------------------------------------------------------------
線性布局:
掌握點(diǎn)
0,控件是依次一個(gè)一個(gè)的擺放
1,在xml文件中通過(guò)<LinearLayout></LinearLayout>來(lái)表示
2,分為垂直和水平布局特性,每行每列只能有一個(gè)組件
3,子view中的gravity屬性和weight屬性
4,layout_weight:layout中控件在某個(gè)方向上的占用比例
5,線性布局是可以嵌套
屬性解釋:
layout_weight屬性:權(quán)重,默認(rèn)為0,意思是需要顯示多大的視圖就占據(jù)多大的屏幕空間
最好和wrap_content配合使用
在一個(gè)容器中的view的占用比例為view的權(quán)重/所有view權(quán)重之和
比如有三個(gè)view: v1 :1
v2:2 :該view占用整個(gè)容器的2/4
v3:1
layout_gravity屬性:在整個(gè)容器中的對(duì)齊方式:上下左右居中等等
例子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="這是一個(gè)測(cè)試" />
<!-- 里面在包含兩個(gè)大的線性布局 , 橫向一個(gè)比重1,一個(gè)比重3 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- 里面在包含兩個(gè)小的線性布局 , 垂直,比重1:1 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<!-- 這個(gè)layout中有三個(gè)按鈕, 垂直 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/seven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/eight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/nine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
</LinearLayout>
<!-- 這個(gè)layout中有兩個(gè)按鈕 橫向 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/zero"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/point"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="." />
</LinearLayout>
</LinearLayout>
<!-- 這個(gè)layout中只有一個(gè)按鈕 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
<Button
android:id="@+id/equal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="=" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
計(jì)算器實(shí)例
線性布局不易嵌套過(guò)多,過(guò)多會(huì)影響運(yùn)行速度
---------------------------------------------------------------
表格布局:
掌握點(diǎn)
1,有行和列,繼承了linearLayout,在創(chuàng)建表格的時(shí)候不需要明確聲明包含多少行和列,
而是通過(guò)tableRow和其他組件來(lái)控制行和列數(shù)據(jù)
2,tablerow表示的就是一個(gè)表格行,也是一個(gè)容器,可以往里面添加組件,添加一個(gè)就算是一個(gè)列
3,同時(shí)如果添加一個(gè)另外的組件,該組件占用一行
4,列寬是由該列中最寬的那個(gè)單元格決定
5,TableLayout所特有的單元格中組件三種行為:(設(shè)置是在TableLayout標(biāo)簽,效果影響tablerow)
a,shrinkableColumns,該列能收縮,以適應(yīng)父容器的大小(內(nèi)容過(guò)多,則收縮)
android:stretchColumns="0"
b,stretchableColumns,該列的寬度可以拉伸,保證組件能填滿父容器(有空白則填充)
android:stretchColumns="0,2,3"
c,collapsedColumns: 該列會(huì)被隱藏(索引列從0開(kāi)始)
6,行中的組件的id從0開(kāi)始
例子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tablelayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0" >
<TableRow
android:id="@+id/tablerow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fd8d8d"
android:text="你好呀"
android:textColor="#000000" />
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/tablelayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,2,3" >
<TableRow
android:id="@+id/tablerow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button4" />
</TableRow>
</TableLayout>
</LinearLayout>
--------------------------------------------------------------
frameLayout: 也叫堆棧布局,視圖以層疊的方式顯示,一層一層的顯示,一般可以用于設(shè)置背景
比如一個(gè)背景,上面有按鈕,當(dāng)按鈕按下后就可以直接播放音樂(lè)
最后一個(gè)視圖放在頂端
第一個(gè)視圖放在最低層
屬性:
top:將視圖放在屏幕的頂端
button: 將視圖放在屏幕的底端
left
right
center_vertical: 視圖垂直居中
horizontal_vertical:視圖水平居中
例子: 比如說(shuō)要做背景,或者是一個(gè)合成圖
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- 前一個(gè)比后一個(gè)大,后一個(gè)會(huì)疊加在前一個(gè)上面 -->
<TextView
android:id="@+id/tv_04"
android:layout_width="180sp"
android:layout_height="180sp"
android:layout_gravity="center"
android:background="#0f0"
android:text="04" />
<TextView
android:id="@+id/tv_03"
android:layout_width="160sp"
android:layout_height="160sp"
android:layout_gravity="center"
android:background="#ff0"
android:text="03" />
<TextView
android:id="@+id/tv_02"
android:layout_width="140sp"
android:layout_height="140sp"
android:layout_gravity="center"
android:background="#f0f"
android:text="02" />
<TextView
android:id="@+id/tv_01"
android:layout_width="120sp"
android:layout_height="120sp"
android:layout_gravity="center"
android:background="#0ff"
android:text="01" />
</FrameLayout>
-------------------------------------------------------------
相對(duì)布局:
在配置布局xml文件的時(shí)候,都需要考慮該布局在橫豎屏不同狀態(tài)下的顯示樣式,
盡可能的將一個(gè)xml文件即適應(yīng)于橫屏,又適應(yīng)于豎屏。這就要求在xml文件中盡量少使用
類似于“50dip”,”“13px”這樣的硬性數(shù)據(jù)
相對(duì)布局:某個(gè)控件的位置是相對(duì)于另外一個(gè)控件做參照物的
屬性:
根據(jù)父容器來(lái)定位:
左對(duì)齊:android:layout_alighParentLeft
右對(duì)齊:android:layout_alighParentRight
頂端對(duì)齊:android:layout_alighParentTop
底部對(duì)齊:android:layout_alighParentBottom
水平居中:android:layout_centerHorizontal
垂直居中:android:layout_centerVertical
中央位置:android:layout_centerInParent
根據(jù)兄弟組件來(lái)定位(根據(jù)兄弟組件的id)
左邊:android:layout_toLeftOf
右邊:android:layout_toRightOf
上方:android:layout_above
下方:android:layout_below
對(duì)齊上邊界:android:layout_alignTop
對(duì)齊下邊界:android:layout_alignBottom
對(duì)齊左邊界:android:layout_alignLeft
對(duì)齊右邊界:android:layout_alignRight
Margin和Padding屬性
Margin:設(shè)置組件與父容器(通常是布局)的邊距
android:layout_margin: 指定控件的四周的外部留出一定的邊距
android:layout_marginLeft: 指定控件的左邊的外部留出一定的邊距
android:layout_marginTop: 指定控件的上邊的外部留出一定的邊距
android:layout_marginRight: 指定控件的右邊的外部留出一定的邊距
android:layout_marginBottom: 指定控件的下邊的外部留出一定的邊距
android:layout_marginLeft="100dp"
Padding:設(shè)置組件內(nèi)部元素間的邊距(可以理解為填充)
android:padding :指定控件的四周的內(nèi)部留出一定的邊距
android:paddingLeft: 指定控件的左邊的內(nèi)部留出一定的邊距
android:paddingTop: 指定控件的上邊的內(nèi)部留出一定的邊距
android:paddingRight: 指定控件的右邊的內(nèi)部留出一定的邊距
android:paddingBottom: 指定控件的下邊的內(nèi)部留出一定的邊距
android:paddingLeft="100dp"
一般沒(méi)有android:orientation
必須有一個(gè)id: @+id/xxx
例子:
A B
中心
C D
-----------------------------------------------------------------------------------------------------------------------
android中UI的事件處理機(jī)制和編程方式:
為什么要有事件處理?
當(dāng)用戶在程序界面上執(zhí)行了各種操作時(shí),應(yīng)用程序必須為用戶動(dòng)作提供相應(yīng)
的響應(yīng)動(dòng)作,這種相應(yīng)動(dòng)作就需要通過(guò)事件處理來(lái)完成,比如按下某個(gè)控件,
拖動(dòng)某個(gè)控件,對(duì)應(yīng)應(yīng)用程序可以做出不同的響應(yīng)
UI如何處理響應(yīng)事件?
1,引用Java事件處理機(jī)制,包括事件,事件源和事件監(jiān)聽(tīng)器
2,事件可以是鼠標(biāo),鍵盤,觸摸事件,比如單擊,雙擊,長(zhǎng)按,拖動(dòng)等等
3,事件源是產(chǎn)生事件的組件,比如Button,ProgressBar,CheckBox等等
4,事件監(jiān)聽(tīng)器是組件產(chǎn)生事件時(shí)響應(yīng)的接口,事件產(chǎn)生后的處理方法
android系統(tǒng)提供了哪些事件處理方式?
兩種:
1,基于回調(diào)的事件處理方式(不推薦,了解)
android中的每個(gè)控件本身自帶處理事件的回調(diào)方法,當(dāng)用戶在該對(duì)象上,發(fā)生了動(dòng)作,Android系統(tǒng)框架的代碼會(huì)自動(dòng)去調(diào)用
比如,當(dāng)一個(gè)視圖(如一個(gè)按鈕)被觸摸時(shí),該對(duì)象上的onTouchEvent()方法會(huì)被調(diào)用
缺點(diǎn):不夠靈活,很明顯,擴(kuò)展每個(gè)你想使用的視圖對(duì)象(只是處理一個(gè)事件)是荒唐的,因?yàn)槿绻幸话賯€(gè)控件,就要重寫
一般個(gè)控件的回調(diào)方法,麻煩的很
public interface Callback {
boolean onKeyDown(int keyCode,KeyEvent event):當(dāng)用戶在該組件上按下某個(gè)鍵時(shí)觸發(fā)的方法。
boolean onKeyLongPress(int keyCode,KeyEvent event):當(dāng)用戶在該組件上長(zhǎng)按某個(gè)按鈕時(shí)觸發(fā)該方法。
boolean onKeyShortcut(int keyCode,KeyEvent event): 當(dāng)一個(gè)快捷鍵事件發(fā)生時(shí)觸發(fā)該放過(guò)。
boolean onKeyUp(int keyCode,KeyEvent event):當(dāng)用戶在該組件上松開(kāi)某個(gè)按鍵時(shí)觸發(fā)該方法
boolean onTouchEvent(MotionEvent event):當(dāng)用戶在該組件上觸發(fā)觸摸屏事件時(shí)觸發(fā)該方法。
boolean onTrackballEvent(MotionEvent event):當(dāng)用戶在該組件上觸發(fā)軌跡球屏事件時(shí)觸發(fā)該事
}
2,基于監(jiān)聽(tīng)的事件處理方式
ui控件自己去處理事件不好辦,那就交給別人去辦,通過(guò)觀察者模式,為Android界面組件綁定特定的事件監(jiān)聽(tīng)器
這種處理方式將事件源和事件監(jiān)聽(tīng)器分離,有利于提供程序的可維護(hù)性
每個(gè)組件可以針對(duì)特定的事件指定一個(gè)事件監(jiān)聽(tīng)器來(lái)處理,每個(gè)事件監(jiān)聽(tīng)器也可以監(jiān)聽(tīng)一個(gè)或多個(gè)事件源頭
基于監(jiān)聽(tīng)的事件處理(第二種)編程方式:
基本步驟:
a,獲取普通組件(事件源),也就是被監(jiān)控的對(duì)象
b,實(shí)現(xiàn)事件監(jiān)聽(tīng)類,比如xxxListener
c,調(diào)用setOnXXXListener
不同的實(shí)現(xiàn)方法:
1,匿名類:
btn1.setOnclickListener(new OnclickListener(){
public void onClick(View v){
// 要執(zhí)行的操作
}
})
2,直接構(gòu)建一個(gè)OnxxListener:
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
Button button = (Button)findViewById(R.id.corky);
//設(shè)置監(jiān)聽(tīng)
button.setOnClickListener(mCorkyListener);
}
3,在Activity組件中實(shí)現(xiàn)xxxListener的接口,這樣在同一個(gè)窗口中多個(gè)控件共享一個(gè)處理接口,相對(duì)方便
但是容易導(dǎo)致Activity工作過(guò)于繁瑣,因?yàn)锳ctivity的工作應(yīng)該是完成界面的初始化,而不必要去處理UI的事件
public class TestMedia extends Activity implements OnClickListner{
Button btn1=(Button)findViewById(R.id.myButton1);
Button btn2=(Button)findViewById(R.id.myButton2);
btn1.setOnclickListener(this);
btn2.setOnclickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.myButton1;
//要執(zhí)行的動(dòng)作1
break;
case R.id.myButton2;
//要執(zhí)行的動(dòng)作2
break;
}
}
----------------------------------------------------------------------------------------
TextView和EditText
1,用于文字顯示(TextView)或輸入EditText
2,TextView其實(shí)是一個(gè)文本編譯器,但是關(guān)閉了編輯功能,
EditText和Button繼承于TextView,
TextView的xml屬性:(可以參考API文檔)
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#669933"
android:drawableRight="@drawable/ic_launcher"
android:autoLink="phone|email"
android:text="信息: 12306; www.12306.cn"
代碼中:
setText();
getText().toString();
EditText對(duì)內(nèi)容可以進(jìn)行限制:
android:digits="1234567890.+-*/%\n()"
限制輸入框中只能輸入自己定義的這些字符串 如果輸入其它將不予以顯示
android:phoneNumber="true"
限制輸入框中只能輸入手機(jī)號(hào)碼
android:password="true"
限制輸入框中輸入的任何內(nèi)容將以"*"符號(hào)來(lái)顯示
android:hint="默認(rèn)文字"
輸入內(nèi)容前默認(rèn)顯示在輸入框中的文字
android:textColorHint="#FF0000"
設(shè)置文字內(nèi)容顏色
android:enabled="false"
設(shè)置輸入框不能被編輯
android:text="在圖片下方"
android:drawableBottom="@drawable/jay"
可以顯示圖片,比如在文字的下面顯示一張圖片
radioButton(單選按鈕), checkbox(復(fù)選框), ToggleButton(開(kāi)關(guān)按鈕), 都是繼承了Button
前兩個(gè)有一個(gè)可選中的功能,所以android:checked屬性可以設(shè)置
radioButton用于多選一,如果想在選中的某一個(gè)選項(xiàng)按鈕后,其他的選項(xiàng)都設(shè)置為未選擇的狀態(tài),
需要將<RadioButton>添加到<RadioGroup>標(biāo)簽中
特性:
1,單個(gè)RadioButton在選中后,通過(guò)點(diǎn)擊無(wú)法變?yōu)槲催x中
2,在沒(méi)有RadioGroup的情況下,RadioButton可以全部都選中;
當(dāng)多個(gè)RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個(gè)
3,RadioButton表示單個(gè)圓形單選框,而RadioGroup是可以容納多個(gè)RadioButton的容器
4,大部分場(chǎng)合下,一個(gè)RadioGroup中至少有2個(gè)RadioButton
5,大部分場(chǎng)合下,一個(gè)RadioGroup中的RadioButton默認(rèn)會(huì)有一個(gè)被選中,并建議您將它放在RadioGroup中的起始位置
6,在radiogroup里面我們也可以使用RelativeLayout,LinearLayout這樣的布局的
xml的屬性:
android:checked="true"
表示初始化的時(shí)候是否被選中,默認(rèn)選擇
1,布局xml文件
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
<RadioButton
android:id="@+id/nosex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人妖" />
</RadioGroup>
<!-- 之間畫條橫線 -->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffffff"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnGetRet"
android:text="獲取當(dāng)前選擇的性別"
/>
監(jiān)聽(tīng)事件:
OnCheckedChangeListener radioListener = new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId)
if(checkedId==R.id.radioBtn1){
Toast.makeText(MainActivity.this, "你來(lái)自廣東省", Toast.LENGTH_LONG).show();
}
//獲取變更后的選中項(xiàng)的ID
int radioButtonId = group.getCheckedRadioButtonId();
//根據(jù)ID獲取RadioButton的實(shí)例
RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId);
tv.setText("您的性別是:" + rb.getText());
}
RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
group.setOnCheckedChangeListener(radioListener);
-------------------------------------------------------------------------------
checkbox:復(fù)選框,默認(rèn)情況是未選中狀態(tài):
xml的屬性:
android:checked="false" <!-- 缺省為flase,可以不進(jìn)行說(shuō)明 -->
例子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/cb_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="red"/>
<CheckBox
android:id="@+id/cb_green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="green" />
<CheckBox
android:id="@+id/cb_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blue" />
</LinearLayout>
監(jiān)聽(tīng)事件:和上面的稍微有點(diǎn)不一樣
android.widget.CompoundButton.OnCheckedChangeListener checkListener
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
//buttonView 選中狀態(tài)發(fā)生改變的那個(gè)按鈕
//isChecked 按鈕新的狀態(tài)
if(isChecked){
}
}
beijing=(CheckBox)findViewById(R.id.beijing);
beijing.setOnCheckedChangeListener(checkListener);
--------------------------------------------------------------
ToggleButton:帶開(kāi)關(guān)的按鈕,選中和未選擇狀態(tài),并且需要為不同的狀態(tài)設(shè)置不同的操作
xml屬性:
android:checked="true"
android:textOn="燈亮"
android:textOff="燈滅"
例子:
<ToggleButton
android:id="@+id/tgBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="燈亮"
android:textOff="燈滅"
/>
監(jiān)聽(tīng)事件
android.widget.CompoundButton.OnCheckedChangeListener checkListener
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
if(isChecked){//true時(shí)
test.setOrientation(1);// 1 的話,設(shè)置為縱向
}else{//false時(shí)
test.setOrientation(0);// 0 的話,設(shè)置為橫向
}
}
ToggleButton toggle = (ToggleButton) this.findViewById(R.id.toggle);
final LinearLayout test = (LinearLayout) this.findViewById(R.id.test);
toggle.setOnCheckedChangeListener(checkListener);
------------------------------------------------------------------------------------------------
動(dòng)態(tài)操作布局文件和通過(guò)代碼動(dòng)態(tài)產(chǎn)生ui控件:
動(dòng)態(tài)操作布局文件: LayoutInflater
LayoutInflater適用于初始化Layout布局xml文件,不可以直接new出來(lái)
//1,通過(guò)Framework中的服務(wù)管理器中獲取到inflater服務(wù)實(shí)例對(duì)象
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
// 2,通過(guò)inflater服務(wù)實(shí)例對(duì)象加載xml布局文件
View layout = inflater.inflate(R.layout.main, null);
例子:
onCreate
|
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.main, null);
et = (EditText) layout.findViewById(R.id.edittext);
et.setBackgroundColor(Color.YELLOW);
btn = (Button) layout.findViewById(R.id.btn);
btn.setBackgroundColor(Color.CYAN);
setContentView(layout); //findViewById一般都是在該函數(shù)之后才能適用,而現(xiàn)在可以在之前適用
動(dòng)態(tài)產(chǎn)生ui控件:
Android 硬編碼-代碼來(lái)實(shí)現(xiàn)UI界面:
好處:從單獨(dú)語(yǔ)言和簡(jiǎn)單程序來(lái)說(shuō)具有運(yùn)行效率高和設(shè)計(jì)簡(jiǎn)單等好處
壞處:代碼和ui沒(méi)有分離,耦合性強(qiáng),對(duì)于復(fù)雜的工程還是建議MVC方式設(shè)計(jì)比較合理,即在xml文件中控制ui
代碼實(shí)現(xiàn):
onCreate()
|
//setContentView(R.layout.main); 不需要xml文件了
//視圖的參數(shù),可以用來(lái)設(shè)置視圖中屬性,類似android:layout_height="fill_parent"
LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
//創(chuàng)建布局
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//創(chuàng)建一個(gè)TextView
TextView tv=new TextView(this);
tv.setText("This is a TextView");
tv.setLayoutParams(params);
//創(chuàng)建一個(gè)Button
Button btn=new Button(this);
btn.setText("This isa Button");
btn.setLayoutParams(params);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv1.setText("Hello,Java Activity! " + new java.util.Date());
}
});
//向布局中添加TextView
layout.addView(tv);
//向布局中添加Button
layout.addView(btn);
//創(chuàng)建布局使用的屬性
LinearLayout.LayoutParamslayoutParam= new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
//將layout顯示出來(lái),LinearLayout對(duì)象添加到Activity中去
this.addContentView(layout,layoutParam);
//或者用這個(gè)
setContentView(linear);
注意:以上的ui對(duì)象都是通過(guò)new創(chuàng)建出來(lái),然后用容器來(lái)盛裝,任何UI都需要傳入this參數(shù),即Context。
UI通過(guò)context獲取Android應(yīng)用環(huán)境全局info。
-----------------------------------------------------------------------------------------------------------------
Toast:向用戶提供簡(jiǎn)單的提示信息
1,提示信息不會(huì)獲取焦點(diǎn)
2,提示信息過(guò)一段時(shí)間后會(huì)消失
使用:
1,創(chuàng)建Toast對(duì)象:
2,設(shè)置消息的內(nèi)容和顯示時(shí)間ms
3,通過(guò)Toast的show()方法顯示出來(lái)
使用方法:
1,默認(rèn)
Toast.makeText(getApplicationContext(), "默認(rèn)的Toast", Toast.LENGTH_SHORT).show();
2,設(shè)定顯示位置
Toast toast=Toast.makeText(getApplicationContext(), "自定義顯示位置的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);
//第一個(gè)參數(shù):設(shè)置toast在屏幕中顯示的位置。我現(xiàn)在的設(shè)置是居中靠頂
//第二個(gè)參數(shù):相對(duì)于第一個(gè)參數(shù)設(shè)置toast位置的橫向X軸的偏移量,正數(shù)向右偏移,負(fù)數(shù)向左偏移
//第三個(gè)參數(shù):同的第二個(gè)參數(shù)道理一樣
//獲取一個(gè)用于顯示的view,并將其設(shè)置成layout布局
LinearLayout layout = (LinearLayout) toast.getView();
ImageView imageView = new ImageView(ToastActivity.this);
imageView.setImageResource(R.drawable.ic_launcher);
layout.addView(imageView);
toast.show();
3,
Toast toast = Toast.makeText(LoginActivity.this, "", 100);
toast.setText("方法2-芝麻開(kāi)門," + name +"正在登錄" );
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, -20, -100);
toast.show();
對(duì)話框:
AlertDialog:跟用戶進(jìn)行交互,方便用戶可操作,比如玩游戲退出時(shí)就會(huì)有對(duì)話框進(jìn)行選擇
界面描述:
1,圖標(biāo)區(qū)域
2,標(biāo)題區(qū)域
3,內(nèi)容區(qū)域
4,按鈕區(qū)域
創(chuàng)建步驟:
1,創(chuàng)建一個(gè)AlertDialog.Builder
AlertDialog.Builder builder=new AlertDialog.Builder(this);
2,調(diào)用builder.setTitle("this is title")設(shè)置標(biāo)題
3,調(diào)用builder.setIcon();設(shè)置圖標(biāo)
4,調(diào)用builder.setMessage設(shè)置文本內(nèi)容,當(dāng)然也可以設(shè)置其他
5,調(diào)用builder.setPositiveButton()--確定,取消
6, 利用create創(chuàng)建,再調(diào)用show()方法顯示
builder.create();
第二天
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 隨手翻到的一篇文章寫道,生活要做減法。 想到自己很長(zhǎng)一段時(shí)間以來(lái)的焦慮,無(wú)不是來(lái)自于鋪天蓋地取之不竭的信息和商品。...
- 父母昨晚過(guò)來(lái)了,早上起床的時(shí)候,父親說(shuō)想喝早茶。剛好小家伙沒(méi)有上幼兒園,外面天氣不好,大霧,下著大雨,于是滴滴叫了...
- 說(shuō)到韓國(guó)電影明星里的實(shí)力派,想必很多人和小編一樣,腦海里出現(xiàn)的第一反應(yīng)就是:宋康昊、崔岷植、黃政民。這些人憑借精湛...
- 我們?cè)缤頃?huì)被生活打敗,換人名額用完,體力用光,我們最終會(huì)被生活打敗,就看堅(jiān)持到下半場(chǎng)的什么時(shí)候。所以,當(dāng)你有能力的...