Android04-UI01常用控制項

來源:互聯網
上載者:User

標籤:

1.常見控制項的使用:

TextViewButtonEditTextImageView 1.TextView
<TextView            android:id="@+id/text_view"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="This is TextView" />

android:layout_width 指定了控制項的寬度 

android:layout_height 指定了控制項的高度 

Android 中所有的控制項都具有這 兩個屬性,可選值有三種 match_parent、fill_parent 和 wrap_content (大小與內容相一致)

android:gravity="center"  文字對齊 

android:textSize="24sp"   字型大小

android:textColor="#00ff00" 字型顏色

2.Button 

<Button            android:id="@+id/button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="Button" />

註冊監聽器

1.//匿名類方法
button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {// 在此處添加邏輯 }});
2.//實現介面OnClickListener 

public class MainActivity extends Activity implements OnClickListener {

        private Button button;
        @Override        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button); 
        button.setOnClickListener(this);}
        @Override        public void onClick(View v) {
            switch (v.getId()) {            case R.id.button:// 在此處添加邏輯
                break;            default:
break; }
}

  

3.EditText 

允許用於在控制項裡輸入和編輯內容的控制項

<EditText            android:id="@+id/edit_text"            android:layout_width="match_parent"            android:layout_height="wrap_content"
//提示性文字
            android:hint="Type something here" /> 
            />
android:maxLines屬性可以指定顯示的最大行數

擷取EditText的輸入內容

   擷取EditText對象   調用EditText對象的getText().toString()方法ImageView用於展示圖片的控制項android:src屬性指定圖片資源
<ImageView            android:id="@+id/image_view"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/ic_launcher"            />

通過代碼動態地更改 ImageView 中的圖片 

imageView.setImageResource(R.drawable.jelly_bean);

ProgressBar 

ProgressBar 用於在介面上顯示一個進度條,表示我們的程式正在載入一些資料。 

  

   

Android04-UI01常用控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.