標籤:
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常用控制項