【Android 开发教程】Button,ImageButton,EditText,ChcekBox,ToggleButton,RadioButton

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


除了最常用的TextView,Android还提供了一些其他的基本控件。

  • Button
  • ImageButton
  • EditText
  • CheckBox
  • RadioGroup和RadioButton
  • ToggleButton

下面的例子,展示如何使用这些基本控件。

1. 创建一个工程:BasicViews。

2. main.xml中的代码。

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7. <Button android:id="@+id/btnSave"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:text="@string/save"   
  11.     android:onClick="btnSaved_clicked"/>  
  12.   
  13. <Button android:id="@+id/btnOpen"  
  14.     android:layout_width="wrap_content"  
  15.     android:layout_height="wrap_content"  
  16.     android:text="Open" />  
  17.   
  18. <ImageButton android:id="@+id/btnImg1"  
  19.     android:layout_width="fill_parent"  
  20.     android:layout_height="wrap_content"  
  21.     android:src="@drawable/ic_launcher" />  
  22.   
  23. <EditText android:id="@+id/txtName"  
  24.     android:layout_width="fill_parent"   
  25.     android:layout_height="wrap_content" />  
  26.   
  27. <CheckBox android:id="@+id/chkAutosave"  
  28.     android:layout_width="fill_parent"  
  29.     android:layout_height="wrap_content"  
  30.     android:text="Autosave" />  
  31.   
  32. <CheckBox android:id="@+id/star"  
  33.     style="?android:attr/starStyle"  
  34.     android:layout_width="wrap_content"  
  35.     android:layout_height="wrap_content" />  
  36.   
  37.   
  38. <RadioGroup android:id="@+id/rdbGp1"  
  39.     android:layout_width="fill_parent"  
  40.     android:layout_height="wrap_content"  
  41.     android:orientation="vertical" >  
  42.       
  43.     <RadioButton android:id="@+id/rdb1"  
  44.         android:layout_width="fill_parent"   
  45.         android:layout_height="wrap_content"  
  46.         android:text="Option 1" />  
  47.       
  48.     <RadioButton android:id="@+id/rdb2"  
  49.         android:layout_width="fill_parent"   
  50.         android:layout_height="wrap_content"  
  51.         android:text="Option 2" />  
  52.   
  53. </RadioGroup>  
  54.   
  55.           
  56. <ToggleButton android:id="@+id/toggle1"  
  57.     android:layout_width="wrap_content"  
  58.     android:layout_height="wrap_content" />  
  59.   
  60. </LinearLayout>  
3. F11调试。

4. 点击不同的控件,观察不同的效果。

5. 对事件进行处理。

[java] view plain copy
  1. public class BasicViews1Activity extends Activity {  
  2.       
  3.     public void btnSaved_clicked (View view) {  
  4.         DisplayToast("You have clicked the Save button1");  
  5.     }  
  6.           
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.   
  13.           
  14.         //---Button view---  
  15.         Button btnOpen = (Button) findViewById(R.id.btnOpen);  
  16.         btnOpen.setOnClickListener(new View.OnClickListener() {  
  17.             public void onClick(View v) {  
  18.                 DisplayToast("You have clicked the Open button");  
  19.             }  
  20.         });  
  21.           
  22.         /* 
  23.         //---Button view--- 
  24.         Button btnSave = (Button) findViewById(R.id.btnSave); 
  25.         btnSave.setOnClickListener(new View.OnClickListener()  
  26.         { 
  27.             public void onClick(View v) { 
  28.                 DisplayToast("You have clicked the Save button"); 
  29.             } 
  30.         }); 
  31.         */  
  32.   
  33.         //---CheckBox---  
  34.         CheckBox checkBox = (CheckBox) findViewById(R.id.chkAutosave);  
  35.         checkBox.setOnClickListener(new View.OnClickListener()   
  36.         {  
  37.             public void onClick(View v) {  
  38.                 if (((CheckBox)v).isChecked())   
  39.                     DisplayToast("CheckBox is checked");  
  40.                 else  
  41.                     DisplayToast("CheckBox is unchecked");  
  42.             }  
  43.         });  
  44.   
  45.         //---RadioButton---  
  46.         RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);  
  47.         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()  
  48.         {  
  49.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  50.                 RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1);  
  51.                 if (rb1.isChecked()) {  
  52.                     DisplayToast("Option 1 checked!");  
  53.                 } else {  
  54.                     DisplayToast("Option 2 checked!");  
  55.                 }  
  56.             }  
  57.         });  
  58.   
  59.         //---ToggleButton---  
  60.         ToggleButton toggleButton =  
  61.                 (ToggleButton) findViewById(R.id.toggle1);  
  62.         toggleButton.setOnClickListener(new View.OnClickListener()  
  63.         {  
  64.             public void onClick(View v) {  
  65.                 if (((ToggleButton)v).isChecked())  
  66.                     DisplayToast("Toggle button is On");  
  67.                 else  
  68.                     DisplayToast("Toggle button is Off");  
  69.             }  
  70.         });  
  71.     }  
  72.   
  73.     private void DisplayToast(String msg)  
  74.     {  
  75.         Toast.makeText(getBaseContext(), msg,  
  76.                 Toast.LENGTH_SHORT).show();  
  77.     }  
  78.   


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值