android常用系统界面控件使用大合集.docx

上传人:小飞机 文档编号:3152592 上传时间:2023-03-11 格式:DOCX 页数:24 大小:41.54KB
返回 下载 相关 举报
android常用系统界面控件使用大合集.docx_第1页
第1页 / 共24页
android常用系统界面控件使用大合集.docx_第2页
第2页 / 共24页
android常用系统界面控件使用大合集.docx_第3页
第3页 / 共24页
android常用系统界面控件使用大合集.docx_第4页
第4页 / 共24页
android常用系统界面控件使用大合集.docx_第5页
第5页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《android常用系统界面控件使用大合集.docx》由会员分享,可在线阅读,更多相关《android常用系统界面控件使用大合集.docx(24页珍藏版)》请在三一办公上搜索。

1、android常用系统界面控件使用大合集常用系统控件界面大合集 今天我用自己写的一个Demo 和大家详细介绍一个Android开发中遇到的一些常用系统控件的使用技巧。 1.文本框TextView TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView, 第一种是通过xml布局文件呈现 ,第二种是通过代码来呈现,由此可见Android 的界面非常灵活。 1. public class TextViewActivity extends Activity 2. Override 3. 4. 5. 6. protected void onCreate(Bundle sa

2、vedInstanceState) setContentView(R.layout.textview); LinearLayout ll = (LinearLayout) findViewById(R.id.textviewll); 7. TextView textView = new TextView(this); 8. /设置显示文字 9. textView.setText(从代码中添加一个TextView); 10. /设置显示颜色 11. textView.setTextColor(Color.WHITE); 12. /设置显示字体大小 13. textView.setTextSize

3、(18); 14. /设置显示背景颜色 15. textView.setBackgroundColor(Color.BLUE); 16. /设置锚点位置 17. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL); 18. /把这个view加入到布局当中 19. ll.addView(textView); 20. 21. super.onCreate(savedInstanceState); 22. 23. 复制代码 1. 2. 6. 15. 复制代码 2.网页框WebView WebView可以实现 类

4、似web的网页 的系统控件 最主要的是可以使用html代码,如访问网页等。 1. public class WebViewActivity extends Activity 2. WebView webView = null; 3. 4. 5. 6. static final String MIME_TYPE = text/html; static final String ENCODING = utf-8; 7. Override 8. protected void onCreate(Bundle savedInstanceState) 9. setContentView(R.layout.

5、webview); 10. 11. webView = (WebView) findViewById(R.id.webview); 12. webView.loadDataWithBaseURL(null,a href= MIME_TYPE, ENCODING, null); 13. super.onCreate(savedInstanceState); 14. 15. 复制代码 1. 2. 6. 14. 17. 复制代码 3.Menu菜单 Menu菜单在android系统控件中真的很具有特色 点击以后会悬浮出一个菜单在次点击菜单则会消失,今天我只是简单的介绍一下系统的Menu菜单, 其实Me

6、nu菜单可以做出非常好看的效果,比如半透明 自定义按钮图片等等,后面我会详细的介绍menu菜单。 1. public class MenuActivity extends Activity 2. 3. 4. 5. 6. Override protected void onCreate(Bundle savedInstanceState) setContentView(R.layout.menuview); super.onCreate(savedInstanceState); 7. 8. 9. Override 10. public boolean onCreateOptionsMenu(Me

7、nu menu) 11. menu.add(0, 0, Menu.NONE, 菜单1).setIcon(R.drawable.icon); 12. menu.add(0, 1, Menu.NONE, 菜单2).setIcon(R.drawable.icon); 13. menu.add(0, 2, Menu.NONE, 菜单3).setIcon(R.drawable.icon); 14. menu.add(0, 3, Menu.NONE, 菜单4).setIcon(R.drawable.icon); 15. menu.add(0, 4, Menu.NONE, 菜单5).setIcon(R.dr

8、awable.icon); 16. menu.add(0, 5, Menu.NONE, 菜单6).setIcon(R.drawable.icon); 17. return super.onCreateOptionsMenu(menu); 18. 19. 20. Override 21. public boolean onOptionsItemSelected(MenuItem item) 22. Dialog(item.getItemId); 23. return super.onOptionsItemSelected(item); 24. 25. 26. private void Dialo

9、g(int message) 27. new AlertDialog.Builder(this).setMessage( 28. 您单击第项Menu菜单项.).show; 29. 30. 复制代码 1. 2. 5. 13. 复制代码 4.按钮Button 第一个是绘制系统字的button, 第二个是带图片的button 。 1. public class ButtonActivity extends Activity 2. 3. 4. 5. 6. Context mContext = null; Override protected void onCreate(Bundle savedInst

10、anceState) setContentView(R.layout.buttonview); 7. mContext = this; 8. 9. /普通按钮 10. Button button0 = (Button)findViewById(R.id.buttonview0); 11. 12. /设置按钮文字颜色 13. button0.setTextColor(Color.BLUE); 14. /设置按钮文字大小 15. button0.setTextSize(30); 16. 17. /设置按钮监听 点击事件 18. button0.setOnClickListener(new OnCl

11、ickListener 19. 20. Override 21. public void onClick(View arg0) 22. Toast.makeText(ButtonActivity.this, 您点击了这是一个按钮, Toast.LENGTH_LONG).show; 23. 24. 25. ); 26. 27. /带图片的按钮 28. ImageButton button1 = (ImageButton)findViewById(R.id.buttonview1); 29. /设置按钮监听 点击事件 30. button1.setOnClickListener(new OnCli

12、ckListener 31. 32. Override 33. public void onClick(View arg0) 34. Toast.makeText(ButtonActivity.this, 您点击了一个带图片的按钮, Toast.LENGTH_LONG).show; 35. 36. 37. ); 38. super.onCreate(savedInstanceState); 39. 40. 复制代码 1. 2. 5. 13. 19. 25. 复制代码 5.编辑框EditView 编辑框在实际开发中用到的非常普遍 比如登录 输入账号 密码 等等。 1. public class

13、EditTextActivity extends Activity 2. 3. Context mContext = null; 4. Override 5. protected void onCreate(Bundle savedInstanceState) 6. setContentView(R.layout.editview); 7. mContext = this; 8. /帐号 9. final EditText editText0 = (EditText)findViewById(R.id.editview0); 10. /密码 11. final EditText editTex

14、t1 = (EditText)findViewById(R.id.editview1); 12. 13. /确认按钮 14. Button button = (Button)findViewById(R.id.editbutton0); 15. 16. button.setOnClickListener(new OnClickListener 17. 18. Override 19. public void onClick(View arg0) 20. String username = editText0.getText.toString; 21. String password = edi

15、tText1.getText.toString; 22. Toast.makeText(EditTextActivity.this, 用户名:+username +密码:+ password, Toast.LENGTH_LONG).show; 23. 24. ); 25. super.onCreate(savedInstanceState); 26. 27. 复制代码 1. 2. 5. 13. 20. 21. 28. 34. 复制代码 6.单项选择 使用RadioGroup 包住若干个RadioButton 来实现单项选择。监听每一个RadioGroup 就可以知道那个单选组中的第一个ID被按

16、下。 1. public class RadioActivity extends Activity 2. 3. 4. 5. 6. Context mContext = null; Override protected void onCreate(Bundle savedInstanceState) setContentView(R.layout.radioview); 7. mContext = this; 8. /单选组(只有在一个组中的按钮可以单选) 9. RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radion0); 10.

17、 11. /单选按钮 12. final RadioButton radioButton0 = (RadioButton)findViewById(R.id.radionButton0); 13. final RadioButton radioButton1 = (RadioButton)findViewById(R.id.radionButton1); 14. final RadioButton radioButton2 = (RadioButton)findViewById(R.id.radionButton2); 15. 16. radioGroup.setOnCheckedChange

18、Listener(new OnCheckedChangeListener 17. 18. Override 19. public void onCheckedChanged(RadioGroup arg0, int checkID) 20. if(radioButton0.getId = checkID) 21. Toast.makeText(RadioActivity.this, 您选中了第一组 + radioButton0.getText, Toast.LENGTH_LONG).show; 22. else if(radioButton1.getId = checkID) 23. Toas

19、t.makeText(RadioActivity.this, 您选中了第一组 + radioButton1.getText, Toast.LENGTH_LONG).show; 24. else if(radioButton2.getId = checkID) 25. Toast.makeText(RadioActivity.this, 您选中了第一组 + radioButton2.getText, Toast.LENGTH_LONG).show; 26. 27. 28. ); 29. 30. RadioGroup radioGroup0 = (RadioGroup)findViewById(R

20、.id.radion1); 31. 32. /单选按钮(第二组) 33. final RadioButton radioButton3 = (RadioButton)findViewById(R.id.radionButton3); 34. final RadioButton radioButton4 = (RadioButton)findViewById(R.id.radionButton4); 35. final RadioButton radioButton5 = (RadioButton)findViewById(R.id.radionButton5); 36. 37. radioGr

21、oup0.setOnCheckedChangeListener(new OnCheckedChangeListener 38. 39. Override 40. public void onCheckedChanged(RadioGroup arg0, int checkID) 41. if(radioButton3.getId = checkID) 42. Toast.makeText(RadioActivity.this, 您选中了第二组 + radioButton3.getText, Toast.LENGTH_LONG).show; 43. else if(radioButton4.ge

22、tId = checkID) 44. Toast.makeText(RadioActivity.this, 您选中了第二组 + radioButton4.getText, Toast.LENGTH_LONG).show; 45. else if(radioButton5.getId = checkID) 46. Toast.makeText(RadioActivity.this, 您选中了第二组 + radioButton5.getText, Toast.LENGTH_LONG).show; 47. 48. 49. ); 50. super.onCreate(savedInstanceState); 51. 52. 复制代码 1. 2. 5. 13. RadioGroup 14. android:id=+id/radion0 15. android:layout_width=fill_parent 16. android:layout_height=wrap_cont

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号