Android開發中,Google工程師已經給咱們封裝好了不少的按鈕,使得咱們在開發中很是的方便和便捷。html
那麼今天就來認識一下經常使用的按鈕,那麼在以前的課程中我已經詳細講過了Button按鈕,那麼這裏就再也不重複了android
Android開發-之監聽button點擊事件:http://www.cnblogs.com/xiao-chuan/p/6074075.htmlweb
按鈕的公共屬性包括:1)經常使用的樣式屬性網絡
a、background多線程
b、marginapp
c、……框架
2)寬高ide
a、widthpost
b、height學習
3)大小
a、size
b、max(min)
c、……
4)文本顯示內容
a、text
b、hint
5)惟一鍵ID
a、id
6)點擊事件
TextView:文本框
autoLink:文本的默認路徑
<TextView android:layout_width="match_parent" android:layout_height="50sp" android:text="中國移動:10086" android:textSize="20sp" android:autoLink="phone" /> <TextView android:layout_width="match_parent" android:layout_height="50sp" android:text="站長郵箱:10086@qq.com" android:textSize="20sp" android:autoLink="email" /> <TextView android:layout_width="match_parent" android:layout_height="50sp" android:text="有事找百度:http://www.baidu.com" android:textSize="20sp" android:autoLink="web" />
那麼在這裏呢,若是點擊第一個 TextView默認效果就是電話啦,那麼第二個第三個呢~~一下是實現的效果圖,這裏你們能夠看到我並無指定文本顏色,這裏注意的是autoLink有默認的顏色啦!
EditText輸入框
inputType:輸入框輸入的文本格式
<EditText android:id="@+id/userName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入用戶名" android:textColor="@android:color/holo_red_light" android:textSize="20sp" android:inputType="text" android:maxLength="12" /> <EditText android:id="@+id/userAge" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入年齡" android:maxLength="3" android:numeric="integer" android:inputType="number" /> <EditText android:id="@+id/userPwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入密碼" android:maxLength="12" android:password="true" android:inputType="textPassword" /> <EditText android:id="@+id/userAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入詳細地址" android:maxLength="500" android:lines="3" android:inputType="textMultiLine" />
inputType:
text:文本格式
textPassword:密碼格式
number:數字格式
textMultiLink:地址欄格式
……
注:那麼你輸入的時候呢輸入法也會自動切換輸入的方式,以密碼格式輸入那麼輸入的內容是不可見的。
這裏分爲:
1)SeekBar:調度,就好比咱們的音量,亮度等
2)RatingBar:選擇,常見到的就是在電商網站上面的評論等
3)ProgressBar:加載、下載,加載圖片,下載文件等
<TextView android:id="@+id/showText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是一個TextView" android:textSize="20sp" /> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="50" android:progress="20" /> <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:rating="1" android:stepSize="1" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="10" /> <ProgressBar android:id="@+id/progressBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="10" style="?android:attr/progressBarStyleLarge" /> <ProgressBar android:id="@+id/progressBar3" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="10" style="?android:attr/progressBarStyleHorizontal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="開始" android:onClick="doStart" /> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="結束" android:onClick="doStop" /> </LinearLayout>
頁面效果:
如下是爲了讓你們更加清楚它們的效果和區別:
private SeekBar sb; private TextView showText; private RatingBar rb; private ProgressBar pb3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seek_bars); showText=(TextView)findViewById(R.id.showText); sb=(SeekBar)findViewById(R.id.seekBar); rb=(RatingBar)findViewById(R.id.ratingBar); pb3=(ProgressBar)findViewById(R.id.progressBar3); //爲seekBar綁定事件 sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //改變showText字體大小 showText.setTextSize(progress); } }); rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Toast.makeText(SeekBarsActivity.this, "你選擇了"+rating+"星", 3000).show(); } }); } //開始下載[注意:除了ProgressBar外,全部的UI都必須在UI主線程中操做] boolean isRun=true; public void doStart(View view) throws Exception{ isRun=true; //構建一個執行進度條變化的子線程 new Thread(new Runnable() { @Override public void run() { //耗時操做不能放在主線程中執行 while(isRun){ if(pb3.getProgress()<100){ pb3.setProgress(pb3.getProgress()+1); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } }else{ isRun=false; //最簡單方法實如今多線程更新UI runOnUiThread(new Runnable() { public void run() { Toast.makeText(SeekBarsActivity.this, "下載完畢",3000).show(); } }); } } } }).start(); } //結束下載 public void doStop(View view){ isRun=false; }
注意:
1)除了ProgressBar外,全部的UI都必須在UI主線程中操做,不然會報錯
2)耗時操做不能放在主線程中執行,不然會報錯
3)Google工程師讓Android4.0之後的版本都不支持以上兩點了,那麼有人就要糾結了,那位了維護低版本,要怎麼辦~~本身想,很簡單的問題!
1)imageButton:圖片按鈕
2)imageView:圖片視圖
imageView與HTML5對比:
imageView:運行更流暢,在沒有網絡的狀況下也可使用。。
HTML5:運行時不夠流暢,沒有網就廢了……可是優勢在於頁面更加美觀,數據傳輸更加便捷。。
3)RadioButton:單選框,各元素是互斥的,只能選擇一個,好比性別
4)CheckBox:多選按鈕,能夠選擇多個,好比愛好
5)ToggleButton:單個提示按鈕,好比開關
<RadioGroup android:id="@+id/rgsex" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/sexOne" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true" /> <RadioButton android:id="@+id/sexTwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="籃球" /> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="足球" /> <CheckBox android:id="@+id/cb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="羽毛球" /> </LinearLayout> <ToggleButton android:id="@+id/stateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="開燈" android:textOff="關燈" android:checked="true" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image001" android:scaleType="fitXY" android:maxWidth="120dp" android:maxHeight="60dp" android:adjustViewBounds="true" android:onClick="getValues" />
效果以下:
ps:圖片打碼了是由於實在找不到合適的圖片而後又不想給人家打廣告又不得錢……
如下一樣爲了更加清楚它們的效果和區別:
private RadioGroup rg; private CheckBox cb1,cb2,cb3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_buttons); cb1=(CheckBox)findViewById(R.id.cb1); cb2=(CheckBox)findViewById(R.id.cb2); cb3=(CheckBox)findViewById(R.id.cb3); rg=(RadioGroup)findViewById(R.id.rgsex); //監聽單選按鈕組事件 rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb=(RadioButton)findViewById(checkedId); Toast.makeText(ButtonsActivity.this, "你選擇了:"+rb.getText().toString(), 3000).show(); } }); } //取值[單選按鈕|複選框] public void getValues(View view){ //單選按鈕 RadioButton rb=(RadioButton)findViewById(rg.getCheckedRadioButtonId()); StringBuffer sb=new StringBuffer(); sb.append("性別:"+rb.getText().toString()+"\n"); //複選框 sb.append("愛好:"); if(cb1.isChecked()) sb.append(cb1.getText().toString()+","); if(cb2.isChecked()) sb.append(cb2.getText().toString()+","); if(cb3.isChecked()) sb.append(cb3.getText().toString()+","); Toast.makeText(this, sb.toString(), 5000).show(); }
PS:Google工程師都給咱們封裝好了,咱們能夠直接使用。咱們也能夠本身寫一個底層的框架去實現這些按鈕,相信你們在學習Java的時候這些按鈕的實現都已經本身有寫過了,其實Google工程師所封裝的底層代碼也是那樣子實現的。只是說~誰那麼無聊啊現成的不用!可是若是你們之後作Android框架開發的時候……就須要本身寫了~在基礎知識更新完了之後呢……就會涉及到比較高級的內容了哈哈哈哈……徹底尚未準備!
一、其實還有不少經常使用的以及一些不經常使用的,無論怎樣都但願你們可以養成自學的習慣……到了公司更是如此!
二、Android框架的開發單純的就是Java知識,因此跟Android開發沒什麼關係,可是又要對Android有很高的認知!
三、能夠在Android頁面中嵌套,可是要區分HTML5與Android palette之間的區別!
四、TextView和EditView的區別,其實很大……
五、palette要與相應的事件和業務邏輯一塊兒使用纔會真正的有意義,好比數據的傳輸~~在之後的課程中我會詳細的講解到。