Android 基礎組件

來源:互聯網
上載者:User

標籤:

基礎組件

所有的控制項都可以在java代碼中建立出來,並且大部分的屬性都對應set和get方法,比如

View view = new View(Context context)  context是上下文,是Activity父類,一般傳入當前Activity

1、TextView

  text 文本  setText()  getText()

  textColor文本顏色  #FFFFFF  setTextColor(Color.Blue)  getTextColor()

  textSize文字大小   sp  setTextSize()  getTextSize()

  textSytle文本樣式   沒有java代碼  (畫筆畫)

  gravity文本對齊  center

 

2、Button

  繼承TextView ,擁有TextView中所有的屬性和方法

  enable=true不能點   setEnable()

 

3、ImageView

  src設定前置圖片  setImageResource(R.drawable.xxx);

  scaleType圖片展開  setScaleType(ScaleType.xxx);

  adjustViewBounds  setAdjustViewBounds(Boolean);

 

4、EditText

  EditText是程式用於和使用者進行互動的另一個重要控制項,它允許使用者在控制項裡輸入和編輯內容,並可以在程式中對這些內容進行處理

  EditText繼承於TextView,擁有TextView中所有的屬性和方法

  常用屬性:

  ems設定TextView的寬度為N個字元的寬度

  maxLines最多顯示行數

  hint提示

  textColorHint提示顏色

  maxLength限制輸入字元數,如設定5,那麼僅可以輸入5個漢字/數字/英文字母

  inputType="textPassword"輸入類型,...2,設定密碼,number設定數字(切換到數字軟鍵盤)

  digits="abcd"只允許輸入abcd這字元

  singleLine="true"單行模式

  imeOptions設定軟鍵盤的Enter鍵。可用|設定多個

    //normal預設換行

    //actionGo軟鍵盤下方變成“前往”“去往”“Go”根據IME不同而不同

    //actionSearch軟鍵盤下方變成“搜尋”或者放大鏡

    //actionSend軟鍵盤下方變成“發送”

    //actionNext軟鍵盤下方變成“下一個”

    actionDone軟鍵盤下方變成”完成”,點擊後游標保持在原來的輸入框上,並且軟鍵盤關閉

   EditText專屬的事件

    //檔案改變監聽

    addTextChangeListener(new Watcher(){

      @Override

      public void onTextChanged(CharSequence s, int start, int before,int count) {

        文字改變時回調此方法}

      @Override

      public void beforeTextChanged(CharSequence s, int start, int count,int after) {}

      @Override

      public void afterTextChanged(Editable s) {}

    });

   //Enter鍵監聽

   et.setOnEditorActionListener(new OnEditorActionListener(){

      @Override

      public boolean onEditorAction(TextView v, int actionId, KeyEvent event){

        //處理,actiongId,當前事件組件的資源ID,用來區分多個EditText用同一個監聽器

        //event事件來源,封裝了當前操作動作

        return false;

      }

    });

    

    

Android 基礎組件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.