Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 入門系列:TextView、EditText、Button、ImageView 四大基礎控件實戰

編輯:關於 Android 開發

Android 入門系列:TextView、EditText、Button、ImageView 四大基礎控件實戰

這是我的第一篇原創筆記。說實話,我自己也還是個新手,這裡只是把一些最基礎的心得整理出來,希望能給同樣剛入門的朋友一點參考。

在 Android 開發裡,TextView、EditText、Button、ImageView 這幾個控件幾乎是最常用、最基礎的成員。本文就從最簡單的角度,帶大家走一遍它們的用法。

本文預設你已經完成開發環境的搭建。筆者早期用的是 Eclipse,後來還是建議大家直接上手 Android Studio,畢竟現在很多開源項目都只提供 AS 版本。

MainActivity.java

 1 public class MainActivity extends Activity { 2 3 private TextView tv; 4 private EditText et; 5 private Button btn; 6 private ImageView iv; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 // 加載布局文件 12 setContentView(R.layout.activity_main); 13 // 初始化控件 14 init(); 15 // 設置Button的OnClickListener監聽器 16 btn.setOnClickListener(new OnClickListener() { 17 @Override 18 public void onClick(View v) { 19 // 將et中輸入的內容通過Toast顯示出來 20 Toast.makeText(MainActivity.this, 21 "et的輸入為:" + et.getText().toString(), Toast.LENGTH_SHORT) 22 .show(); 23 } 24 }); 25 // 設置ImageView的OnClickListener監聽器 26 iv.setOnClickListener(new OnClickListener() { 27 @Override 28 public void onClick(View v) { 29 // 當點擊btn的時候就讓iv的圖片變成另一張圖片 30 iv.setBackgroundResource(R.drawable.animal); 31 } 32 }); 33 } 34 35 private void init() { 36 tv = (TextView) this.findViewById(R.id.acMain_tv_username); 37 et = (EditText) this.findViewById(R.id.acMain_et_password); 38 btn = (Button) this.findViewById(R.id.acMain_btn_login); 39 iv = (ImageView) this.findViewById(R.id.acMain_iv_show); 40 tv.setText("Hello , this is gh"); 41 }

activity_main.xml

 1 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 xmlns:tools="https://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.cnblogs001.MainActivity" > 10 11 <TextView 12 android:id="@+id/acMain_tv_username" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:gravity="center_horizontal" 16 android:padding="10dp" 17 android:text="@string/hello_world" 18 android:textColor="@android:color/holo_blue_dark" 19 android:textSize="20sp" /> 20 21 <EditText 22 android:id="@+id/acMain_et_password" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:layout_below="@id/acMain_tv_username" 26 android:layout_marginTop="10dp" 27 android:ellipsize="end" 28 android:gravity="center_horizontal" 29 android:hint="@string/hint_et_password" 30 android:inputType="textPassword" 31 android:maxLines="1" /> 32 33 <Button 34 android:id="@+id/acMain_btn_login" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:layout_below="@id/acMain_et_password" 38 android:layout_marginTop="10dp" 39 android:alpha="0.5" 40 android:background="@android:color/holo_blue_dark" 41 android:gravity="center" 42 android:text="@string/text_btn_login" 43 android:textColor="#000000" 44 android:textSize="20sp" /> 45 46 <ImageView 47 android:id="@+id/acMain_iv_show" 48 android:layout_width="150dp" 49 android:layout_height="150dp" 50 android:layout_below="@id/acMain_btn_login" 51 android:layout_centerHorizontal="true" 52 android:layout_marginTop="10dp" 53 android:background="@drawable/logo" 54 android:scaleType="fitCenter" /> 55 56 </RelativeLayout>

效果圖:

小結:

在 XML 布局裡,我只用了這幾個控件最基礎的一批屬性,都是入門必備的;如果想深入了解,建議直接查閱官方文檔。

而在 Activity 中,我只為 Button 和 ImageView 設置了 OnClickListener 監聽,其實它們還能監聽更多類型的事件,有興趣的朋友可以自己動手試試。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved