今天我用自己写的一个Demo 和大家详细介绍一个Android开发中遇到的一些常用系统控件的使用技巧。
1.文本框TextViewTextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView,第一种是通过xml布局文件呈现,第二种是通过代码来呈现,由此可见Android 的界面开发真的是非常灵活。
view plaincopy to clipboardprint?1. public class TextViewActivity extends Activity {2. @Override3. protected void onCreate(Bundle savedInstanceState) {4. setContentView(yout.textview);5.6. 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(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. <?xml version="1.0"encoding="utf-8"?>2. <LinearLayout xmlns:android="/apk/res/android"3. android:id="@+id/textviewll"4. android:orientation="vertical"android:layout_width="fill_parent"5. android:layout_height="fill_parent">6. <TextView android: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>view plaincopy to clipboardprint?1. public class WebViewActivity extends Activity {2. WebView webView = null;3. static final String MIME_TYPE = "text/html";4. static final String ENCODING = "utf-8";5.6.7. @Override8. protected void onCreate(Bundle savedInstanceState) {9. setContentView(yout.webview);10.11. webView = (WebView) findViewById(R.id.webview);12. webView.loadDataWithBaseURL(null,"<a href='http://blog.csd/xys289187120'>欢迎访问雨松MOMO的博客</a>", MIME_TYPE, ENCODING, null);13. super.onCreate(savedInstanceState);14. }15. }1. <?xml version="1.0"encoding="utf-8"?>2. <LinearLayout xmlns:android="/apk/res/android"3. android:id="@+id/textviewll"4. android:orientation="vertical"android:layout_width="fill_parent"5. android:layout_height="fill_parent">6. <TextView android: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. <WebView android:id="@+id/webview"15. android:layout_height="wrap_content"16. android:layout_width="fill_parent"/>17. </LinearLayout>view plaincopy to clipboardprint?1. public class MenuActivity extends Activity {2.3. @Override4. protected void onCreate(Bundle savedInstanceState) {5. setContentView(yout.menuview);6. super.onCreate(savedInstanceState);7. }8.9. @Override10. public boolean onCreateOptionsMenu(Menu 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.drawable.icon);16. menu.add(0, 5, Menu.NONE, "菜单6").setIcon(R.drawable.icon);17. return super.onCreateOptionsMenu(menu);18. }19.20. @Override21. public boolean onOptionsItemSelected(MenuItem item) {22. Dialog(item.getItemId());23. return super.onOptionsItemSelected(item);24. }25.26. private void Dialog(int message) {27. new AlertDialog.Builder(this).setMessage(28. "您单击第【" + message + "】项Menu菜单项.").show();29. }30. }1. <?xml version="1.0"encoding="utf-8"?>2. <LinearLayout xmlns:android="/apk/res/android"3. android:orientation="vertical"android:layout_width="fill_parent"4. android:layout_height="fill_parent">5. <TextView android: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>view plaincopy to clipboardprint?1. public class ButtonActivity extends Activity {2.3. Context mContext = null;4. @Override5. protected void onCreate(Bundle savedInstanceState) {6. setContentView(yout.buttonview);7. mContext = this;8.9. //普通按钮10. Button button0 = (Button)findViewById(R.id.buttonview0);12. //设置按钮文字颜色13. button0.setTextColor(Color.BLUE);14. //设置按钮文字大小15. button0.setTextSize(30);16.17. //设置按钮监听点击事件18. button0.setOnClickListener(new OnClickListener() {19.20. @Override21. 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 OnClickListener() {31.32. @Override33. public void onClick(View arg0) {34. Toast.makeText(ButtonActivity.this, "您点击了一个带图片的按钮", Toast.LENGTH_LONG).show();35.36. }37. });38. super.onCreate(savedInstanceState);39. }1. <?xml version="1.0"encoding="utf-8"?>2. <LinearLayout xmlns:android="/apk/res/android"3. android:orientation="vertical"android:layout_width="fill_parent"4. android:layout_height="fill_parent">5. <TextView android: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. <Button14. android:id="@+id/buttonview0"15. android:layout_width="fill_parent"16. android:layout_height="wrap_content"17. android:text="这是一个按钮"18. />19. <ImageButton20. android:id="@+id/buttonview1"21. android:layout_width="fill_parent"22. android:layout_height="wrap_content"23. android:src="@drawable/icon"24. />25. </LinearLayout>view plaincopy to clipboardprint?1. public class EditTextActivity extends Activity {2.3. Context mContext = null;4. @Override5. protected void onCreate(Bundle savedInstanceState) {6. setContentView(yout.editview);7. mContext = this;8. //帐号9. final EditText editText0 = (EditText)findViewById(R.id.editview0);10. //密码11. final EditText editText1 = (EditText)findViewById(R.id.editview1);12.13. //确认按钮14. Button button = (Button)findViewById(R.id.editbutton0);15.16. button.setOnClickListener(new OnClickListener() {17.18. @Override19. public void onClick(View arg0) {20. String username = editText0.getText().toString();21. String password = editText1.getText().toString();22. Toast.makeText(EditTextActivity.this, "用户名:"+username +"密码:"+ password, Toast.LENGTH_LONG).show();23. }24. });25. super.onCreate(savedInstanceState);26. }27. }1. <?xml version="1.0"encoding="utf-8"?>2. <LinearLayout xmlns:android="/apk/res/android"3. android:orientation="vertical"android:layout_width="fill_parent"4. android:layout_height="fill_parent">5. <TextView android: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. <EditText14. 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.21. <EditText22. android:id="@+id/editview1"23. android:layout_width="fill_parent"24. android:layout_height="wrap_content"25. android:hint="请输入密码"26. android:password="true"27. />28. <Button29. android:id="@+id/editbutton0"30. android:layout_width="fill_parent"31. android:layout_height="wrap_content"32. android:text="确定"33. />34. </LinearLayout>view plaincopy to clipboardprint?1. public class RadioActivity extends Activity {2.3. Context mContext = null;4. @Override5. protected void onCreate(Bundle savedInstanceState) {6. setContentView(yout.radioview);7. mContext = this;8. //单选组(只有在一个组中的按钮可以单选)9. RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radion0);10.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.setOnCheckedChangeListener(new OnCheckedChangeListener() {17.18. @Override19. 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. Toast.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.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. radioGroup0.setOnCheckedChangeListener(new OnCheckedChangeListener() {38.39. @Override40. 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.getId() == 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. <?xml version="1.0"encoding="utf-8"?>2. <LinearLayout xmlns:android="/apk/res/android"3. android:orientation="vertical"android:layout_width="fill_parent"4. android:layout_height="fill_parent">5. <TextView android: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. <RadioGroup14. android:id="@+id/radion0"15. android:layout_width="fill_parent"16. android:layout_height="wrap_content">17. <RadioButton18. android:id="@+id/radionButton0"19. android:layout_width="fill_parent"20. android:layout_height="wrap_content"21. android:text="item0"22. />23. <RadioButton24. android:id="@+id/radionButton1"26. android:layout_height="wrap_content"27. android:text="item1"28. />29. <RadioButton30. android:id="@+id/radionButton2"31. android:layout_width="fill_parent"32. android:layout_height="wrap_content"33. android:text="item2"34. />35. </RadioGroup>36.37. <TextView android:layout_width="fill_parent"38. android:layout_height="wrap_content"39. android:textColor="#000000"40. android:textSize="18dip"41. android:background="#00FF00"42. android:text="单项选择测试第二组"43. android:gravity="center_vertical|center_horizontal"44. />45. <RadioGroup46. android:id="@+id/radion1"47. android:layout_width="fill_parent"48. android:layout_height="wrap_content">49. <RadioButton50. android:id="@+id/radionButton3"51. android:layout_width="fill_parent"52. android:layout_height="wrap_content"53. android:text="item3"54. />55. <RadioButton56. android:id="@+id/radionButton4"58. android:layout_height="wrap_content"59. android:text="item4"60. />61. <RadioButton62. android:id="@+id/radionButton5"63. android:layout_width="fill_parent"64. android:layout_height="wrap_content"65. android:text="item5"66. />67. </RadioGroup>68. </LinearLayout>view plaincopy to clipboardprint?1. public class CheckboxActivity extends Activity {2.3. //用来储存选中的内容4. ArrayList <String>item = new ArrayList<String>();5.6. @Override7. protected void onCreate(Bundle savedInstanceState) {8. setContentView(yout.checkboxview);9.10. CheckBox checkbox0 = (CheckBox)findViewById(R.id.checkboxview0);11. CheckBox checkbox1 = (CheckBox)findViewById(R.id.checkboxview1);12. CheckBox checkbox2 = (CheckBox)findViewById(R.id.checkboxview2);13. CheckBox checkbox3 = (CheckBox)findViewById(R.id.checkboxview3);14. Button button = (Button)findViewById(R.id.checkboxbutton);15. //对checkbox进行监听16. checkbox0.setOnCheckedChangeListener(new OnCheckedChangeListener() {17.18. @Override19. public void onCheckedChanged(CompoundButton button, boolean arg1) {20. String str = button.getText().toString();21. if (button.isChecked()) {22. item.add(str);23. } else {24. item.remove(str);25. }26.27. }28. });29.30. checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {31.32. @Override33. public void onCheckedChanged(CompoundButton button, boolean arg1) {34. String str = button.getText().toString();35. if (button.isChecked()) {36. item.add(str);37. } else {38. item.remove(str);39. }40.41. }42. });43. checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {44.45. @Override46. public void onCheckedChanged(CompoundButton button, boolean arg1) {47. String str = button.getText().toString();48. if (button.isChecked()) {49. item.add(str);50. } else {51. item.remove(str);52. }53.54. }55. });56. checkbox3.setOnCheckedChangeListener(new OnCheckedChangeListener() {57.58. @Override59. public void onCheckedChanged(CompoundButton button, boolean arg1) {60. String str = button.getText().toString();61. if (button.isChecked()) {62. item.add(str);63. } else {64. item.remove(str);65. }66.67. }68. });69.70. button.setOnClickListener(new OnClickListener() {71.72. @Override73. public void onClick(View arg0) {74. String str = item.toString();75. Toast.makeText(CheckboxActivity.this, "您选中了" + str, Toast.LENGTH_LONG).show();76.77. }78. });79. super.onCreate(savedInstanceState);80. }81. }1. <?xml version="1.0"encoding="utf-8"?>2. <LinearLayout xmlns:android="/apk/res/android"3. android:orientation="vertical"android:layout_width="fill_parent"4. android:layout_height="fill_parent">5. <TextView android: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. <CheckBox14. android:id="@+id/checkboxview0"15. android:layout_width="fill_parent"16. android:layout_height="wrap_content"17. android:text="item0"18. />19. <CheckBox20. android:id="@+id/checkboxview1"21. android:layout_width="fill_parent"22. android:layout_height="wrap_content"23. android:text="item1"24. />25. <CheckBox26. android:id="@+id/checkboxview2"27. android:layout_width="fill_parent"28. android:layout_height="wrap_content"29. android:text="item2"30. />31. <CheckBox32. android:id="@+id/checkboxview3"33. android:layout_width="fill_parent"34. android:layout_height="wrap_content"35. android:text="item3"36. />37. <Button38. android:id="@+id/checkboxbutton"39. android:layout_width="fill_parent"40. android:layout_height="wrap_content"41. android:text="确定"42. />43. </LinearLayout>。