Android 程式开发:(十)基本控件 —— 10.2 Button,ImageButton,EditText,ChcekBox,ToggleButton,RadioButton...

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

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

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

1、创建一个工程:BasicViews。

2、main.xml中的代码。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <Buttonandroid:id="@+id/btnSave"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/save"
  10. android:onClick="btnSaved_clicked"/>
  11. <Buttonandroid:id="@+id/btnOpen"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="Open"/>
  15. <ImageButtonandroid:id="@+id/btnImg1"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:src="@drawable/ic_launcher"/>
  19. <EditTextandroid:id="@+id/txtName"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"/>
  22. <CheckBoxandroid:id="@+id/chkAutosave"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:text="Autosave"/>
  26. <CheckBoxandroid:id="@+id/star"
  27. style="?android:attr/starStyle"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"/>
  30. <RadioGroupandroid:id="@+id/rdbGp1"
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:orientation="vertical">
  34. <RadioButtonandroid:id="@+id/rdb1"
  35. android:layout_width="fill_parent"
  36. android:layout_height="wrap_content"
  37. android:text="Option1"/>
  38. <RadioButtonandroid:id="@+id/rdb2"
  39. android:layout_width="fill_parent"
  40. android:layout_height="wrap_content"
  41. android:text="Option2"/>
  42. </RadioGroup>
  43. <ToggleButtonandroid:id="@+id/toggle1"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"/>
  46. </LinearLayout>
3、F11调试。

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

5、对事件进行处理。

  1. packagenet.learn2develop.BasicViews1;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.View;
  5. importandroid.widget.Button;
  6. importandroid.widget.CheckBox;
  7. importandroid.widget.RadioButton;
  8. importandroid.widget.RadioGroup;
  9. importandroid.widget.RadioGroup.OnCheckedChangeListener;
  10. importandroid.widget.Toast;
  11. importandroid.widget.ToggleButton;
  12. publicclassBasicViews1ActivityextendsActivity{
  13. publicvoidbtnSaved_clicked(Viewview){
  14. DisplayToast("YouhaveclickedtheSavebutton1");
  15. }
  16. /**Calledwhentheactivityisfirstcreated.*/
  17. @Override
  18. publicvoidonCreate(BundlesavedInstanceState){
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. //---Buttonview---
  22. ButtonbtnOpen=(Button)findViewById(R.id.btnOpen);
  23. btnOpen.setOnClickListener(newView.OnClickListener(){
  24. publicvoidonClick(Viewv){
  25. DisplayToast("YouhaveclickedtheOpenbutton");
  26. }
  27. });
  28. /*
  29. //---Buttonview---
  30. ButtonbtnSave=(Button)findViewById(R.id.btnSave);
  31. btnSave.setOnClickListener(newView.OnClickListener()
  32. {
  33. publicvoidonClick(Viewv){
  34. DisplayToast("YouhaveclickedtheSavebutton");
  35. }
  36. });
  37. */
  38. //---CheckBox---
  39. CheckBoxcheckBox=(CheckBox)findViewById(R.id.chkAutosave);
  40. checkBox.setOnClickListener(newView.OnClickListener()
  41. {
  42. publicvoidonClick(Viewv){
  43. if(((CheckBox)v).isChecked())
  44. DisplayToast("CheckBoxischecked");
  45. else
  46. DisplayToast("CheckBoxisunchecked");
  47. }
  48. });
  49. //---RadioButton---
  50. RadioGroupradioGroup=(RadioGroup)findViewById(R.id.rdbGp1);
  51. radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener()
  52. {
  53. publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
  54. RadioButtonrb1=(RadioButton)findViewById(R.id.rdb1);
  55. if(rb1.isChecked()){
  56. DisplayToast("Option1checked!");
  57. }else{
  58. DisplayToast("Option2checked!");
  59. }
  60. }
  61. });
  62. //---ToggleButton---
  63. ToggleButtontoggleButton=
  64. (ToggleButton)findViewById(R.id.toggle1);
  65. toggleButton.setOnClickListener(newView.OnClickListener()
  66. {
  67. publicvoidonClick(Viewv){
  68. if(((ToggleButton)v).isChecked())
  69. DisplayToast("TogglebuttonisOn");
  70. else
  71. DisplayToast("TogglebuttonisOff");
  72. }
  73. });
  74. }
  75. privatevoidDisplayToast(Stringmsg)
  76. {
  77. Toast.makeText(getBaseContext(),msg,
  78. Toast.LENGTH_SHORT).show();
  79. }
  80. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值