Android開發-之認識palette


  Android開發中,Google工程師已經給我們封裝好了很多的按鈕,使得我們在開發中非常的方便和便捷。

  那么今天就來認識一下常用的按鈕,那么在之前的課程中我已經詳細講過了Button按鈕,那么這里就不再重復了

  Android開發-之監聽button點擊事件http://www.cnblogs.com/xiao-chuan/p/6074075.html

 

一、常用按鈕

  1、按鈕公共屬性

  按鈕的公共屬性包括:1)常用的樣式屬性

              a、background

              b、margin

              c、……

            2)寬高

              a、width

              b、height

            3)大小

              a、size

              b、max(min)

              c、……

            4)文本顯示內容

              a、text

              b、hint

            5)唯一鍵ID

              a、id

            6)點擊事件

 

  2、TextView:

    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有默認的顏色啦!

 

 

 

  3、EditText

    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:地址欄格式

    ……

  注:那么你輸入的時候呢輸入法也會自動切換輸入的方式,以密碼格式輸入那么輸入的內容是不可見的。

  

  4、Bar

  這里分為:

    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以后的版本都不支持以上兩點了,那么有人就要糾結了,那位了維護低版本,要怎么辦~~自己想,很簡單的問題!

 

 

   

  

  5、Image與單選多選框

    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框架開發的時候……就需要自己寫了~在基礎知識更新完了以后呢……就會涉及到比較高級的內容了哈哈哈哈……完全還沒有准備!

 

二、總結

  1、其實還有很多常用的以及一些不常用的,不管怎樣都希望大家能夠養成自學的習慣……到了公司更是如此!

  2、Android框架的開發單純的就是Java知識,所以跟Android開發沒什么關系,但是又要對Android有很高的認知!

  3、可以在Android頁面中嵌套,但是要區分HTML5與Android palette之間的區別!

  4、TextView和EditView的區別,其實很大……

  5、palette要與相應的事件和業務邏輯一起使用才會真正的有意義,比如數據的傳輸~~在以后的課程中我會詳細的講解到。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM