`
baobaoupup
  • 浏览: 470311 次
文章分类
社区版块
存档分类
最新评论

Android软件开发之盘点常用系统控件界面大合集

 
阅读更多
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://xys289187120.blog.51cto.com/3361352/657555

雨松MOMO带大家盘点Android 开发中的一些常用系统控件的简单用法
今天我用自己写的一个Demo 和大家详细介绍一个Android开发中遇到的一些常用系统控件的使用技巧


1.文本框TextView
TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView, 第一种是通过xml布局文件呈现 ,第二种是通过代码来呈现,由此可见Android 的界面开发真的是非常灵活。

  1. publicclassTextViewActivityextendsActivity{
  2. @Override
  3. protectedvoidonCreate(BundlesavedInstanceState){
  4. setContentView(R.layout.textview);
  5. LinearLayoutll=(LinearLayout)findViewById(R.id.textviewll);
  6. TextViewtextView=newTextView(this);
  7. //设置显示文字
  8. textView.setText("从代码中添加一个TextView");
  9. //设置显示颜色
  10. textView.setTextColor(Color.WHITE);
  11. //设置显示字体大小
  12. textView.setTextSize(18);
  13. //设置显示背景颜色
  14. textView.setBackgroundColor(Color.BLUE);
  15. //设置锚点位置
  16. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
  17. //把这个view加入到布局当中
  18. ll.addView(textView);
  19. super.onCreate(savedInstanceState);
  20. }
  21. }
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/textviewll"
  4. android:orientation="vertical"android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextViewandroid:id="@+id/textView0"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:textColor="#000000"
  10. android:textSize="18dip"
  11. android:background="#00FF00"
  12. android:text="@string/textView"
  13. android:gravity="center_vertical|center_horizontal"
  14. />
  15. </LinearLayout>

2.网页框WebView

WebView可以实现 类似web的网页 的系统控件 最主要的是可以使用html代码,如访问网页等。

  1. publicclassWebViewActivityextendsActivity{
  2. WebViewwebView=null;
  3. staticfinalStringMIME_TYPE="text/html";
  4. staticfinalStringENCODING="utf-8";
  5. @Override
  6. protectedvoidonCreate(BundlesavedInstanceState){
  7. setContentView(R.layout.webview);
  8. webView=(WebView)findViewById(R.id.webview);
  9. webView.loadDataWithBaseURL(null,"<ahref='http://blog.csdn.net/xys289187120'>欢迎访问雨松MOMO的博客</a>",MIME_TYPE,ENCODING,null);
  10. super.onCreate(savedInstanceState);
  11. }
  12. }
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/textviewll"
  4. android:orientation="vertical"android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextViewandroid:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:textColor="#000000"
  9. android:textSize="18dip"
  10. android:background="#00FF00"
  11. android:text="网页框WebView测试"
  12. android:gravity="center_vertical|center_horizontal"
  13. />
  14. <WebViewandroid:id="@+id/webview"
  15. android:layout_height="wrap_content"
  16. android:layout_width="fill_parent"/>
  17. </LinearLayout>
3.Menu菜单
Menu菜单在android系统控件中真的很具有特色 点击以后会悬浮出一个菜单在次点击菜单则会消失,今天我只是简单的介绍一下系统的Menu菜单, 其实Menu菜单可以做出非常好看的效果,比如半透明 自定义按钮图片等等,后面我会详细的介绍menu菜单。

  1. publicclassMenuActivityextendsActivity{
  2. @Override
  3. protectedvoidonCreate(BundlesavedInstanceState){
  4. setContentView(R.layout.menuview);
  5. super.onCreate(savedInstanceState);
  6. }
  7. @Override
  8. publicbooleanonCreateOptionsMenu(Menumenu){
  9. menu.add(0,0,Menu.NONE,"菜单1").setIcon(R.drawable.icon);
  10. menu.add(0,1,Menu.NONE,"菜单2").setIcon(R.drawable.icon);
  11. menu.add(0,2,Menu.NONE,"菜单3").setIcon(R.drawable.icon);
  12. menu.add(0,3,Menu.NONE,"菜单4").setIcon(R.drawable.icon);
  13. menu.add(0,4,Menu.NONE,"菜单5").setIcon(R.drawable.icon);
  14. menu.add(0,5,Menu.NONE,"菜单6").setIcon(R.drawable.icon);
  15. returnsuper.onCreateOptionsMenu(menu);
  16. }
  17. @Override
  18. publicbooleanonOptionsItemSelected(MenuItemitem){
  19. Dialog(item.getItemId());
  20. returnsuper.onOptionsItemSelected(item);
  21. }
  22. privatevoidDialog(intmessage){
  23. newAlertDialog.Builder(this).setMessage(
  24. "您单击第【"+message+"】项Menu菜单项.").show();
  25. }
  26. }
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextViewandroid:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. android:textColor="#000000"
  8. android:textSize="18dip"
  9. android:background="#00FF00"
  10. android:text="Menu菜单测试"
  11. android:gravity="center_vertical|center_horizontal"
  12. />
  13. </LinearLayout>
4.按钮Button
第一个是绘制系统字的button, 第二个是带图片的button

  1. publicclassButtonActivityextendsActivity{
  2. ContextmContext=null;
  3. @Override
  4. protectedvoidonCreate(BundlesavedInstanceState){
  5. setContentView(R.layout.buttonview);
  6. mContext=this;
  7. //普通按钮
  8. Buttonbutton0=(Button)findViewById(R.id.buttonview0);
  9. //设置按钮文字颜色
  10. button0.setTextColor(Color.BLUE);
  11. //设置按钮文字大小
  12. button0.setTextSize(30);
  13. //设置按钮监听点击事件
  14. button0.setOnClickListener(newOnClickListener(){
  15. @Override
  16. publicvoidonClick(Viewarg0){
  17. Toast.makeText(ButtonActivity.this,"您点击了‘这是一个按钮’",Toast.LENGTH_LONG).show();
  18. }
  19. });
  20. //带图片的按钮
  21. ImageButtonbutton1=(ImageButton)findViewById(R.id.buttonview1);
  22. //设置按钮监听点击事件
  23. button1.setOnClickListener(newOnClickListener(){
  24. @Override
  25. publicvoidonClick(Viewarg0){
  26. Toast.makeText(ButtonActivity.this,"您点击了一个带图片的按钮",Toast.LENGTH_LONG).show();
  27. }
  28. });
  29. super.onCreate(savedInstanceState);
  30. }
  31. }
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextViewandroid:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. android:textColor="#000000"
  8. android:textSize="18dip"
  9. android:background="#00FF00"
  10. android:text="Button按钮测试"
  11. android:gravity="center_vertical|center_horizontal"
  12. />
  13. <Button
  14. android:id="@+id/buttonview0"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="这是一个按钮"
  18. />
  19. <ImageButton
  20. android:id="@+id/buttonview1"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:src="@drawable/icon"
  24. />
  25. </LinearLayout>
5.编辑框EditView
编辑框在实际开发中用到的非常普遍 比如登录 输入账号 密码 等等。

  1. publicclassEditTextActivityextendsActivity{
  2. ContextmContext=null;
  3. @Override
  4. protectedvoidonCreate(BundlesavedInstanceState){
  5. setContentView(R.layout.editview);
  6. mContext=this;
  7. //帐号
  8. finalEditTexteditText0=(EditText)findViewById(R.id.editview0);
  9. //密码
  10. finalEditTexteditText1=(EditText)findViewById(R.id.editview1);
  11. //确认按钮
  12. Buttonbutton=(Button)findViewById(R.id.editbutton0);
  13. button.setOnClickListener(newOnClickListener(){
  14. @Override
  15. publicvoidonClick(Viewarg0){
  16. Stringusername=editText0.getText().toString();
  17. Stringpassword=editText1.getText().toString();
  18. Toast.makeText(EditTextActivity.this,"用户名:"+username+"密码:"+password,Toast.LENGTH_LONG).show();
  19. }
  20. });
  21. super.onCreate(savedInstanceState);
  22. }
  23. }
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextViewandroid:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. android:textColor="#000000"
  8. android:textSize="18dip"
  9. android:background="#00FF00"
  10. android:text="EditText编辑框测试"
  11. android:gravity="center_vertical|center_horizontal"
  12. />
  13. <EditText
  14. android:id="@+id/editview0"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:hint="请输入帐号"
  18. android:phoneNumber="true"
  19. />
  20. <EditText
  21. android:id="@+id/editview1"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:hint="请输入密码"
  25. android:password="true"
  26. />
  27. <Button
  28. android:id="@+id/editbutton0"
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content"
  31. android:text="确定"
  32. />
  33. </LinearLayout>
6.单项选择
使用RadioGroup 包住若干个RadioButton 来实现单项选择。监听每一个RadioGroup 就可以知道那个单选组中的第一个ID被按下。


  1. publicclassRadioActivityextendsActivity{
  2. ContextmContext=null;
  3. @Override
  4. protectedvoidonCreate(BundlesavedInstanceState){
  5. setContentView(R.layout.radioview);
  6. mContext=this;
  7. //单选组(只有在一个组中的按钮可以单选)
  8. RadioGroupradioGroup=(RadioGroup)findViewById(R.id.radion0);
  9. //单选按钮(第一组)
  10. finalRadioButtonradioButton0=(RadioButton)findViewById(R.id.radionButton0);
  11. finalRadioButtonradioButton1=(RadioButton)findViewById(R.id.radionButton1);
  12. finalRadioButtonradioButton2=(RadioButton)findViewById(R.id.radionButton2);
  13. radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  14. @Override
  15. publicvoidonCheckedChanged(RadioGrouparg0,intcheckID){
  16. if(radioButton0.getId()==checkID){
  17. Toast.makeText(RadioActivity.this,"您选中了第一组"+radioButton0.getText(),Toast.LENGTH_LONG).show();
  18. }elseif(radioButton1.getId()==checkID){
  19. Toast.makeText(RadioActivity.this,"您选中了第一组"+radioButton1.getText(),Toast.LENGTH_LONG).show();
  20. }elseif(radioButton2.getId()==checkID){
  21. Toast.makeText(RadioActivity.this,"您选中了第一组"+radioButton2.getText(),Toast.LENGTH_LONG).show();
  22. }
  23. }
  24. });
  25. RadioGroupradioGroup0=(RadioGroup)findViewById(R.id.radion1);
  26. //单选按钮(第二组)
  27. finalRadioButtonradioButton3=(RadioButton)findViewById(R.id.radionButton3);
  28. finalRadioButtonradioButton4=(RadioButton)findViewById(R.id.radionButton4);
  29. finalRadioButtonradioButton5=(RadioButton)findViewById(R.id.radionButton5);
  30. radioGroup0.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  31. @Override
  32. publicvoidonCheckedChanged(RadioGrouparg0,intcheckID){
  33. if(radioButton3.getId()==checkID){
  34. Toast.makeText(RadioActivity.this,"您选中了第二组"+radioButton3.getText(),Toast.LENGTH_LONG).show();
  35. }elseif(radioButton4.getId()==checkID){
  36. Toast.makeText(RadioActivity.this,"您选中了第二组"+radioButton4.getText(),Toast.LENGTH_LONG).show();
  37. }elseif(radioButton5.getId()==checkID){
  38. Toast.makeText(RadioActivity.this,"您选中了第二组"+radioButton5.getText(),Toast.LENGTH_LONG).show();
  39. }
  40. }
  41. });
  42. super.onCreate(savedInstanceState);
  43. }
  44. }
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextViewandroid:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. android:textColor="#000000"
  8. android:textSize="18dip"
  9. android:background="#00FF00"
  10. android:text="单项选择测试第一组"
  11. android:gravity="center_vertical|center_horizontal"
  12. />
  13. <RadioGroup
  14. android:id="@+id/radion0"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content">
  17. <RadioButton
  18. android:id="@+id/radionButton0"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:text="item0"
  22. />
  23. <RadioButton
  24. android:id="@+id/radionButton1"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:text="item1"
  28. />
  29. <RadioButton
  30. android:id="@+id/radionButton2"
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:text="item2"
  34. />
  35. </RadioGroup>
  36. <TextViewandroid:layout_width="fill_parent"
  37. android:layout_height="wrap_content"
  38. android:textColor="#000000"
  39. android:textSize="18dip"
  40. android:background="#00FF00"
  41. android:text="单项选择测试第二组"
  42. android:gravity="center_vertical|center_horizontal"
  43. />
  44. <RadioGroup
  45. android:id="@+id/radion1"
  46. android:layout_width="fill_parent"
  47. android:layout_height="wrap_content">
  48. <RadioButton
  49. android:id="@+id/radionButton3"
  50. android:layout_width="fill_parent"
  51. android:layout_height="wrap_content"
  52. android:text="item3"
  53. />
  54. <RadioButton
  55. android:id="@+id/radionButton4"
  56. android:layout_width="fill_parent"
  57. android:layout_height="wrap_content"
  58. android:text="item4"
  59. />
  60. <RadioButton
  61. android:id="@+id/radionButton5"
  62. android:layout_width="fill_parent"
  63. android:layout_height="wrap_content"
  64. android:text="item5"
  65. />
  66. </RadioGroup>
  67. </LinearLayout>
7.多项选择
使用系统控件Checkbox 监听每一个checkbox 的点击事件就可以确定那几个选项被选择了。

  1. publicclassCheckboxActivityextendsActivity{
  2. //用来储存选中的内容
  3. ArrayList<String>item=newArrayList<String>();
  4. @Override
  5. protectedvoidonCreate(BundlesavedInstanceState){
  6. setContentView(R.layout.checkboxview);
  7. CheckBoxcheckbox0=(CheckBox)findViewById(R.id.checkboxview0);
  8. CheckBoxcheckbox1=(CheckBox)findViewById(R.id.checkboxview1);
  9. CheckBoxcheckbox2=(CheckBox)findViewById(R.id.checkboxview2);
  10. CheckBoxcheckbox3=(CheckBox)findViewById(R.id.checkboxview3);
  11. Buttonbutton=(Button)findViewById(R.id.checkboxbutton);
  12. //对checkbox进行监听
  13. checkbox0.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  14. @Override
  15. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  16. Stringstr=button.getText().toString();
  17. if(button.isChecked()){
  18. item.add(str);
  19. }else{
  20. item.remove(str);
  21. }
  22. }
  23. });
  24. checkbox1.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  25. @Override
  26. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  27. Stringstr=button.getText().toString();
  28. if(button.isChecked()){
  29. item.add(str);
  30. }else{
  31. item.remove(str);
  32. }
  33. }
  34. });
  35. checkbox2.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  36. @Override
  37. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  38. Stringstr=button.getText().toString();
  39. if(button.isChecked()){
  40. item.add(str);
  41. }else{
  42. item.remove(str);
  43. }
  44. }
  45. });
  46. checkbox3.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  47. @Override
  48. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  49. Stringstr=button.getText().toString();
  50. if(button.isChecked()){
  51. item.add(str);
  52. }else{
  53. item.remove(str);
  54. }
  55. }
  56. });
  57. button.setOnClickListener(newOnClickListener(){
  58. @Override
  59. publicvoidonClick(Viewarg0){
  60. Stringstr=item.toString();
  61. Toast.makeText(CheckboxActivity.this,"您选中了"+str,Toast.LENGTH_LONG).show();
  62. }
  63. });
  64. super.onCreate(savedInstanceState);
  65. }
  66. }

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextViewandroid:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. android:textColor="#000000"
  8. android:textSize="18dip"
  9. android:background="#00FF00"
  10. android:text="多项选择测试"
  11. android:gravity="center_vertical|center_horizontal"
  12. />
  13. <CheckBox
  14. android:id="@+id/checkboxview0"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="item0"
  18. />
  19. <CheckBox
  20. android:id="@+id/checkboxview1"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:text="item1"
  24. />
  25. <CheckBox
  26. android:id="@+id/checkboxview2"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:text="item2"
  30. />
  31. <CheckBox
  32. android:id="@+id/checkboxview3"
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. android:text="item3"
  36. />
  37. <Button
  38. android:id="@+id/checkboxbutton"
  39. android:layout_width="fill_parent"
  40. android:layout_height="wrap_content"
  41. android:text="确定"
  42. />
  43. </LinearLayout>
最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习
雨松MOMO希望可以和大家一起进步。

下载地址:
http://download.csdn.net/source/3449861

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics