> Android在线手册 > 【Android 界面效果18】Android软件开发之常用系统控件界面整理
[java] view plaincopyprint?
  1. <span style="font-size:18px">1.文本框TextView
  2. TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView, 第一种是通过xml布局文件呈现 ,第二种是通过代码来呈现,由此可见Android 的界面开发真的是非常灵活。
  3. public class TextViewActivity extends Activity {
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. setContentView(R.layout.textview);
  7. LinearLayout ll = (LinearLayout) findViewById(R.id.textviewll);
  8. TextView textView = new TextView(this);
  9. //设置显示文字
  10. textView.setText("从代码中添加一个TextView");
  11. //设置显示颜色
  12. textView.setTextColor(Color.WHITE);
  13. //设置显示字体大小
  14. textView.setTextSize(18);
  15. //设置显示背景颜色
  16. textView.setBackgroundColor(Color.BLUE);
  17. //设置锚点位置
  18. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
  19. //把这个view加入到布局当中
  20. ll.addView(textView);
  21. super.onCreate(savedInstanceState);
  22. }
  23. }
  24. <?xml version="1.0" encoding="utf-8"?>
  25. <LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
  26. android:id="@+id/textviewll"
  27. android:orientation="vertical" android:layout_width="fill_parent"
  28. android:layout_height="fill_parent">
  29. <TextView android:id="@+id/textView0"
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content"
  32. android:textColor="#000000"
  33. android:textSize="18dip"
  34. android:background="#00FF00"
  35. android:text="@string/textView"
  36. android:gravity="center_vertical|center_horizontal"
  37. />
  38. </LinearLayout>
  39. 2.网页框WebView
  40. WebView可以实现 类似web的网页 的系统控件 最主要的是可以使用HTML代码,如访问网页等。
  41. public class WebViewActivity extends Activity {
  42. WebView webView = null;
  43. static final String mime_TYPE = "text/html";
  44. static final String ENCODING = "utf-8";
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. setContentView(R.layout.webview);
  48. webView = (WebView) findViewById(R.id.webview);
  49. webView.loadDataWitHbaseURL(null,"<a href='http://blog.csdn.net/xys289187120'>欢迎访问雨松MOMO的博客</a>", MIME_TYPE, ENCODING, null);
  50. super.onCreate(savedInstanceState);
  51. }
  52. }
  53. <?xml version="1.0" encoding="utf-8"?>
  54. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  55. android:id="@+id/textviewll"
  56. android:orientation="vertical" android:layout_width="fill_parent"
  57. android:layout_height="fill_parent">
  58. <TextView android:layout_width="fill_parent"
  59. android:layout_height="wrap_content"
  60. android:textColor="#000000"
  61. android:textSize="18dip"
  62. android:background="#00FF00"
  63. android:text="网页框WebView测试"
  64. android:gravity="center_vertical|center_horizontal"
  65. />
  66. <WebView android:id="@+id/webview"
  67. android:layout_height="wrap_content"
  68. android:layout_width="fill_parent"/>
  69. </LinearLayout>
  70. 3.Menu菜单
  71. Menu菜单在android系统控件中真的很具有特色点击以后会悬浮出一个菜单在次点击菜单则会消失,今天我只是简单的介绍一下系统的Menu菜单, 其实Menu菜单可以做出非常好看的效果,比如半透明自定义按钮图片等等,后面我会详细的介绍menu菜单。
  72. public class MenuActivity extends Activity {
  73. @Override
  74. protected void onCreate(Bundle savedInstanceState) {
  75. setContentView(R.layout.menuview);
  76. super.onCreate(savedInstanceState);
  77. }
  78. @Override
  79. public boolean onCreateOptionsMenu(Menu menu) {
  80. menu.add(0, 0, Menu.NONE, "菜单1").setIcon(R.drawable.icon);
  81. menu.add(0, 1, Menu.NONE, "菜单2").setIcon(R.drawable.icon);
  82. menu.add(0, 2, Menu.NONE, "菜单3").setIcon(R.drawable.icon);
  83. menu.add(0, 3, Menu.NONE, "菜单4").setIcon(R.drawable.icon);
  84. menu.add(0, 4, Menu.NONE, "菜单5").setIcon(R.drawable.icon);
  85. menu.add(0, 5, Menu.NONE, "菜单6").setIcon(R.drawable.icon);
  86. return super.onCreateOptionsMenu(menu);
  87. }
  88. @Override
  89. public boolean onOptionsItemSelected(MenuItem item) {
  90. Dialog(item.getItemId());
  91. return super.onOptionsItemSelected(item);
  92. }
  93. private void Dialog(int message) {
  94. new AlertDialog.Builder(this).setMessage(
  95. "您单击第【" + message + "】项Menu菜单项.").show();
  96. }
  97. }
  98. <?xml version="1.0" encoding="utf-8"?>
  99. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  100. android:orientation="vertical" android:layout_width="fill_parent"
  101. android:layout_height="fill_parent">
  102. <TextView android:layout_width="fill_parent"
  103. android:layout_height="wrap_content"
  104. android:textColor="#000000"
  105. android:textSize="18dip"
  106. android:background="#00FF00"
  107. android:text="Menu菜单测试"
  108. android:gravity="center_vertical|center_horizontal"
  109. />
  110. </LinearLayout>
  111. 4.按钮Button
  112. 第一个是绘制系统字的button, 第二个是带图片的button 。
  113. public class ButtonActivity extends Activity {
  114. Context mContext = null;
  115. @Override
  116. protected void onCreate(Bundle savedInstanceState) {
  117. setContentView(R.layout.buttonview);
  118. mContext = this;
  119. //普通按钮
  120. Button button0 = (Button)findViewById(R.id.buttonview0);
  121. //设置按钮文字颜色
  122. button0.setTextColor(Color.BLUE);
  123. //设置按钮文字大小
  124. button0.setTextSize(30);
  125. //设置按钮监听 点击事件
  126. button0.setOnClickListener(new OnClickListener() {
  127. @Override
  128. public void onClick(View arg0) {
  129. Toast.makeText(ButtonActivity.this, "您点击了‘这是一个按钮’", Toast.LENGTH_LONG).show();
  130. }
  131. });
  132. //带图片的按钮
  133. ImageButton button1 = (ImageButton)findViewById(R.id.buttonview1);
  134. //设置按钮监听 点击事件
  135. button1.setOnClickListener(new OnClickListener() {
  136. @Override
  137. public void onClick(View arg0) {
  138. Toast.makeText(ButtonActivity.this, "您点击了一个带图片的按钮", Toast.LENGTH_LONG).show();
  139. }
  140. });
  141. super.onCreate(savedInstanceState);
  142. }
  143. }
  144. <?xml version="1.0" encoding="utf-8"?>
  145. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  146. android:orientation="vertical" android:layout_width="fill_parent"
  147. android:layout_height="fill_parent">
  148. <TextView android:layout_width="fill_parent"
  149. android:layout_height="wrap_content"
  150. android:textColor="#000000"
  151. android:textSize="18dip"
  152. android:background="#00FF00"
  153. android:text="Button按钮测试"
  154. android:gravity="center_vertical|center_horizontal"
  155. />
  156. <Button
  157. android:id="@+id/buttonview0"
  158. android:layout_width="fill_parent"
  159. android:layout_height="wrap_content"
  160. android:text="这是一个按钮"
  161. />
  162. <ImageButton
  163. android:id="@+id/buttonview1"
  164. android:layout_width="fill_parent"
  165. android:layout_height="wrap_content"
  166. android:src="@drawable/icon"
  167. />
  168. </LinearLayout>
  169. 5.编辑框EditView
  170. 编辑框在实际开发中用到的非常普遍比如登录 输入账号 密码等等。
  171. public class EditTextActivity extends Activity {
  172. Context mContext = null;
  173. @Override
  174. protected void onCreate(Bundle savedInstanceState) {
  175. setContentView(R.layout.editview);
  176. mContext = this;
  177. //帐号
  178. final EditText editText0 = (EditText)findViewById(R.id.editview0);
  179. //密码
  180. final EditText editText1 = (EditText)findViewById(R.id.editview1);
  181. //确认按钮
  182. Button button = (Button)findViewById(R.id.editbutton0);
  183. button.setOnClickListener(new OnClickListener() {
  184. @Override
  185. public void onClick(View arg0) {
  186. String username = editText0.getText().toString();
  187. String password = editText1.getText().toString();
  188. Toast.makeText(EditTextActivity.this, "用户名:"+username +"密码:"+ password, Toast.LENGTH_LONG).show();
  189. }
  190. });
  191. super.onCreate(savedInstanceState);
  192. }
  193. }
  194. <?xml version="1.0" encoding="utf-8"?>
  195. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  196. android:orientation="vertical" android:layout_width="fill_parent"
  197. android:layout_height="fill_parent">
  198. <TextView android:layout_width="fill_parent"
  199. android:layout_height="wrap_content"
  200. android:textColor="#000000"
  201. android:textSize="18dip"
  202. android:background="#00FF00"
  203. android:text="EditText编辑框测试"
  204. android:gravity="center_vertical|center_horizontal"
  205. />
  206. <EditText
  207. android:id="@+id/editview0"
  208. android:layout_width="fill_parent"
  209. android:layout_height="wrap_content"
  210. android:hint="请输入帐号"
  211. android:phoneNumber="true"
  212. />
  213. <EditText
  214. android:id="@+id/editview1"
  215. android:layout_width="fill_parent"
  216. android:layout_height="wrap_content"
  217. android:hint="请输入密码"
  218. android:password="true"
  219. />
  220. <Button
  221. android:id="@+id/editbutton0"
  222. android:layout_width="fill_parent"
  223. android:layout_height="wrap_content"
  224. android:text="确定"
  225. />
  226. </LinearLayout>
  227. 6.单项选择
  228. 使用RadioGroup 包住若干个RadioButton 来实现单项选择。监听每一个RadioGroup 就可以知道那个单选组中的第一个ID被按下。
  229. public class RadioActivity extends Activity {
  230. Context mContext = null;
  231. @Override
  232. protected void onCreate(Bundle savedInstanceState) {
  233. setContentView(R.layout.radioview);
  234. mContext = this;
  235. //单选组(只有在一个组中的按钮可以单选)
  236. RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radion0);
  237. //单选按钮(第一组)
  238. final RadioButton radioButton0 = (RadioButton)findViewById(R.id.radionButton0);
  239. final RadioButton radioButton1 = (RadioButton)findViewById(R.id.radionButton1);
  240. final RadioButton radioButton2 = (RadioButton)findViewById(R.id.radionButton2);
  241. radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  242. @Override
  243. public void onCheckedChanged(RadioGroup arg0, int checkID) {
  244. if(radioButton0.getId() == checkID) {
  245. Toast.makeText(RadioActivity.this, "您选中了第一组" + radioButton0.getText(), Toast.LENGTH_LONG).show();
  246. }else if(radioButton1.getId() == checkID) {
  247. Toast.makeText(RadioActivity.this, "您选中了第一组" + radioButton1.getText(), Toast.LENGTH_LONG).show();
  248. }else if(radioButton2.getId() == checkID) {
  249. Toast.makeText(RadioActivity.this, "您选中了第一组" + radioButton2.getText(), Toast.LENGTH_LONG).show();
  250. }
  251. }
  252. });
  253. RadioGroup radioGroup0 = (RadioGroup)findViewById(R.id.radion1);
  254. //单选按钮(第二组)
  255. final RadioButton radioButton3 = (RadioButton)findViewById(R.id.radionButton3);
  256. final RadioButton radioButton4 = (RadioButton)findViewById(R.id.radionButton4);
  257. final RadioButton radioButton5 = (RadioButton)findViewById(R.id.radionButton5);
  258. radioGroup0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  259. @Override
  260. public void onCheckedChanged(RadioGroup arg0, int checkID) {
  261. if(radioButton3.getId() == checkID) {
  262. Toast.makeText(RadioActivity.this, "您选中了第二组" + radioButton3.getText(), Toast.LENGTH_LONG).show();
  263. }else if(radioButton4.getId() == checkID) {
  264. Toast.makeText(RadioActivity.this, "您选中了第二组" + radioButton4.getText(), Toast.LENGTH_LONG).show();
  265. }else if(radioButton5.getId() == checkID) {
  266. Toast.makeText(RadioActivity.this, "您选中了第二组" + radioButton5.getText(), Toast.LENGTH_LONG).show();
  267. }
  268. }
  269. });
  270. super.onCreate(savedInstanceState);
  271. }
  272. }
  273. <?xml version="1.0" encoding="utf-8"?>
  274. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  275. android:orientation="vertical" android:layout_width="fill_parent"
  276. android:layout_height="fill_parent">
  277. <TextView android:layout_width="fill_parent"
  278. android:layout_height="wrap_content"
  279. android:textColor="#000000"
  280. android:textSize="18dip"
  281. android:background="#00FF00"
  282. android:text="单项选择测试第一组"
  283. android:gravity="center_vertical|center_horizontal"
  284. />
  285. <RadioGroup
  286. android:id="@+id/radion0"
  287. android:layout_width="fill_parent"
  288. android:layout_height="wrap_content" >
  289. <RadioButton
  290. android:id="@+id/radionButton0"
  291. android:layout_width="fill_parent"
  292. android:layout_height="wrap_content"
  293. android:text="item0"
  294. />
  295. <RadioButton
  296. android:id="@+id/radionButton1"
  297. android:layout_width="fill_parent"
  298. android:layout_height="wrap_content"
  299. android:text="item1"
  300. />
  301. <RadioButton
  302. android:id="@+id/radionButton2"
  303. android:layout_width="fill_parent"
  304. android:layout_height="wrap_content"
  305. android:text="item2"
  306. />
  307. </RadioGroup>
  308. <TextView android:layout_width="fill_parent"
  309. android:layout_height="wrap_content"
  310. android:textColor="#000000"
  311. android:textSize="18dip"
  312. android:background="#00FF00"
  313. android:text="单项选择测试第二组"
  314. android:gravity="center_vertical|center_horizontal"
  315. />
  316. <RadioGroup
  317. android:id="@+id/radion1"
  318. android:layout_width="fill_parent"
  319. android:layout_height="wrap_content" >
  320. <RadioButton
  321. android:id="@+id/radionButton3"
  322. android:layout_width="fill_parent"
  323. android:layout_height="wrap_content"
  324. android:text="item3"
  325. />
  326. <RadioButton
  327. android:id="@+id/radionButton4"
  328. android:layout_width="fill_parent"
  329. android:layout_height="wrap_content"
  330. android:text="item4"
  331. />
  332. <RadioButton
  333. android:id="@+id/radionButton5"
  334. android:layout_width="fill_parent"
  335. android:layout_height="wrap_content"
  336. android:text="item5"
  337. />
  338. </RadioGroup>
  339. </LinearLayout>
  340. 7.多项选择
  341. 使用系统控件Checkbox 监听每一个checkbox 的点击事件就可以确定那几个选项被选择了。
  342. public class CheckboxActivity extends Activity {
  343. //用来储存选中的内容
  344. ArrayList <String>item = new ArrayList<String>();
  345. @Override
  346. protected void onCreate(Bundle savedInstanceState) {
  347. setContentView(R.layout.checkboxview);
  348. CheckBox checkbox0 = (CheckBox)findViewById(R.id.checkboxview0);
  349. CheckBox checkbox1 = (CheckBox)findViewById(R.id.checkboxview1);
  350. CheckBox checkbox2 = (CheckBox)findViewById(R.id.checkboxview2);
  351. CheckBox checkbox3 = (CheckBox)findViewById(R.id.checkboxview3);
  352. Button button = (Button)findViewById(R.id.checkboxbutton);
  353. //对checkbox进行监听
  354. checkbox0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  355. @Override
  356. public void onCheckedChanged(CompoundButton button, boolean arg1) {
  357. String str = button.getText().toString();
  358. if (button.isChecked()) {
  359. item.add(str);
  360. } else {
  361. item.remove(str);
  362. }
  363. }
  364. });
  365. checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  366. @Override
  367. public void onCheckedChanged(CompoundButton button, boolean arg1) {
  368. String str = button.getText().toString();
  369. if (button.isChecked()) {
  370. item.add(str);
  371. } else {
  372. item.remove(str);
  373. }
  374. }
  375. });
  376. checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  377. @Override
  378. public void onCheckedChanged(CompoundButton button, boolean arg1) {
  379. String str = button.getText().toString();
  380. if (button.isChecked()) {
  381. item.add(str);
  382. } else {
  383. item.remove(str);
  384. }
  385. }
  386. });
  387. checkbox3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  388. @Override
  389. public void onCheckedChanged(CompoundButton button, boolean arg1) {
  390. String str = button.getText().toString();
  391. if (button.isChecked()) {
  392. item.add(str);
  393. } else {
  394. item.remove(str);
  395. }
  396. }
  397. });
  398. button.setOnClickListener(new OnClickListener() {
  399. @Override
  400. public void onClick(View arg0) {
  401. String str = item.toString();
  402. Toast.makeText(CheckboxActivity.this, "您选中了" + str, Toast.LENGTH_LONG).show();
  403. }
  404. });
  405. super.onCreate(savedInstanceState);
  406. }
  407. }
  408. 1.文本框TextView
  409. TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView, 第一种是通过xml布局文件呈现 ,第二种是通过代码来呈现,由此可见Android 的界面开发真的是非常灵活。
  410. public class TextViewActivity extends Activity {
  411. @Override
  412. protected void onCreate(Bundle savedInstanceState) {
  413. setContentView(R.layout.textview);
  414. LinearLayout ll = (LinearLayout) findViewById(R.id.textviewll);
  415. TextView textView = new TextView(this);
  416. //设置显示文字
  417. textView.setText("从代码中添加一个TextView");
  418. //设置显示颜色
  419. textView.setTextColor(Color.WHITE);
  420. //设置显示字体大小
  421. textView.setTextSize(18);
  422. //设置显示背景颜色
  423. textView.setBackgroundColor(Color.BLUE);
  424. //设置锚点位置
  425. textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
  426. //把这个view加入到布局当中
  427. ll.addView(textView);
  428. super.onCreate(savedInstanceState);
  429. }
  430. }
  431. <?xml version="1.0" encoding="utf-8"?>
  432. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  433. android:id="@+id/textviewll"
  434. android:orientation="vertical" android:layout_width="fill_parent"
  435. android:layout_height="fill_parent">
  436. <TextView android:id="@+id/textView0"
  437. android:layout_width="fill_parent"
  438. android:layout_height="wrap_content"
  439. android:textColor="#000000"
  440. android:textSize="18dip"
  441. android:background="#00FF00"
  442. android:text="@string/textView"
  443. android:gravity="center_vertical|center_horizontal"
  444. />
  445. </LinearLayout>
  446. 2.网页框WebView
  447. WebView可以实现 类似web的网页 的系统控件 最主要的是可以使用html代码,如访问网页等。
  448. public class WebViewActivity extends Activity {
  449. WebView webView = null;
  450. static final String MIME_TYPE = "text/html";
  451. static final String ENCODING = "utf-8";
  452. @Override
  453. protected void onCreate(Bundle savedInstanceState) {
  454. setContentView(R.layout.webview);
  455. webView = (WebView) findViewById(R.id.webview);
  456. webView.loadDataWithBaseURL(null,"<a href='http://blog.csdn.net/xys289187120'>欢迎访问雨松MOMO的博客</a>", MIME_TYPE, ENCODING, null);
  457. super.onCreate(savedInstanceState);
  458. }
  459. }
  460. <?xml version="1.0" encoding="utf-8"?>
  461. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  462. android:id="@+id/textviewll"
  463. android:orientation="vertical" android:layout_width="fill_parent"
  464. android:layout_height="fill_parent">
  465. <TextView android:layout_width="fill_parent"
  466. android:layout_height="wrap_content"
  467. android:textColor="#000000"
  468. android:textSize="18dip"
  469. android:background="#00FF00"
  470. android:text="网页框WebView测试"
  471. android:gravity="center_vertical|center_horizontal"
  472. />
  473. <WebView android:id="@+id/webview"
  474. android:layout_height="wrap_content"
  475. android:layout_width="fill_parent"/>
  476. </LinearLayout>
  477. 3.Menu菜单
  478. Menu菜单在android系统控件中真的很具有特色点击以后会悬浮出一个菜单在次点击菜单则会消失,今天我只是简单的介绍一下系统的Menu菜单, 其实Menu菜单可以做出非常好看的效果,比如半透明自定义按钮图片等等,后面我会详细的介绍menu菜单。
  479. public class MenuActivity extends Activity {
  480. @Override
  481. protected void onCreate(Bundle savedInstanceState) {
  482. setContentView(R.layout.menuview);
  483. super.onCreate(savedInstanceState);
  484. }
  485. @Override
  486. public boolean onCreateOptionsMenu(Menu menu) {
  487. menu.add(0, 0, Menu.NONE, "菜单1").setIcon(R.drawable.icon);
  488. menu.add(0, 1, Menu.NONE, "菜单2").setIcon(R.drawable.icon);
  489. menu.add(0, 2, Menu.NONE, "菜单3").setIcon(R.drawable.icon);
  490. menu.add(0, 3, Menu.NONE, "菜单4").setIcon(R.drawable.icon);
  491. menu.add(0, 4, Menu.NONE, "菜单5").setIcon(R.drawable.icon);
  492. menu.add(0, 5, Menu.NONE, "菜单6").setIcon(R.drawable.icon);
  493. return super.onCreateOptionsMenu(menu);
  494. }
  495. @Override
  496. public boolean onOptionsItemSelected(MenuItem item) {
  497. Dialog(item.getItemId());
  498. return super.onOptionsItemSelected(item);
  499. }
  500. private void Dialog(int message) {
  501. new AlertDialog.Builder(this).setMessage(
  502. "您单击第【" + message + "】项Menu菜单项.").show();
  503. }
  504. }
  505. <?xml version="1.0" encoding="utf-8"?>
  506. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  507. android:orientation="vertical" android:layout_width="fill_parent"
  508. android:layout_height="fill_parent">
  509. <TextView android:layout_width="fill_parent"
  510. android:layout_height="wrap_content"
  511. android:textColor="#000000"
  512. android:textSize="18dip"
  513. android:background="#00FF00"
  514. android:text="Menu菜单测试"
  515. android:gravity="center_vertical|center_horizontal"
  516. />
  517. </LinearLayout>
  518. 4.按钮Button
  519. 第一个是绘制系统字的button, 第二个是带图片的button 。
  520. public class ButtonActivity extends Activity {
  521. Context mContext = null;
  522. @Override
  523. protected void onCreate(Bundle savedInstanceState) {
  524. setContentView(R.layout.buttonview);
  525. mContext = this;
  526. //普通按钮
  527. Button button0 = (Button)findViewById(R.id.buttonview0);
  528. //设置按钮文字颜色
  529. button0.setTextColor(Color.BLUE);
  530. //设置按钮文字大小
  531. button0.setTextSize(30);
  532. //设置按钮监听 点击事件
  533. button0.setOnClickListener(new OnClickListener() {
  534. @Override
  535. public void onClick(View arg0) {
  536. Toast.makeText(ButtonActivity.this, "您点击了‘这是一个按钮’", Toast.LENGTH_LONG).show();
  537. }
  538. });
  539. //带图片的按钮
  540. ImageButton button1 = (ImageButton)findViewById(R.id.buttonview1);
  541. //设置按钮监听 点击事件
  542. button1.setOnClickListener(new OnClickListener() {
  543. @Override
  544. public void onClick(View arg0) {
  545. Toast.makeText(ButtonActivity.this, "您点击了一个带图片的按钮", Toast.LENGTH_LONG).show();
  546. }
  547. });
  548. super.onCreate(savedInstanceState);
  549. }
  550. }
  551. <?xml version="1.0" encoding="utf-8"?>
  552. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  553. android:orientation="vertical" android:layout_width="fill_parent"
  554. android:layout_height="fill_parent">
  555. <TextView android:layout_width="fill_parent"
  556. android:layout_height="wrap_content"
  557. android:textColor="#000000"
  558. android:textSize="18dip"
  559. android:background="#00FF00"
  560. android:text="Button按钮测试"
  561. android:gravity="center_vertical|center_horizontal"
  562. />
  563. <Button
  564. android:id="@+id/buttonview0"
  565. android:layout_width="fill_parent"
  566. android:layout_height="wrap_content"
  567. android:text="这是一个按钮"
  568. />
  569. <ImageButton
  570. android:id="@+id/buttonview1"
  571. android:layout_width="fill_parent"
  572. android:layout_height="wrap_content"
  573. android:src="@drawable/icon"
  574. />
  575. </LinearLayout>
  576. 5.编辑框EditView
  577. 编辑框在实际开发中用到的非常普遍比如登录 输入账号 密码等等。
  578. public class EditTextActivity extends Activity {
  579. Context mContext = null;
  580. @Override
  581. protected void onCreate(Bundle savedInstanceState) {
  582. setContentView(R.layout.editview);
  583. mContext = this;
  584. //帐号
  585. final EditText editText0 = (EditText)findViewById(R.id.editview0);
  586. //密码
  587. final EditText editText1 = (EditText)findViewById(R.id.editview1);
  588. //确认按钮
  589. Button button = (Button)findViewById(R.id.editbutton0);
  590. button.setOnClickListener(new OnClickListener() {
  591. @Override
  592. public void onClick(View arg0) {
  593. String username = editText0.getText().toString();
  594. String password = editText1.getText().toString();
  595. Toast.makeText(EditTextActivity.this, "用户名:"+username +"密码:"+ password, Toast.LENGTH_LONG).show();
  596. }
  597. });
  598. super.onCreate(savedInstanceState);
  599. }
  600. }
  601. <?xml version="1.0" encoding="utf-8"?>
  602. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  603. android:orientation="vertical" android:layout_width="fill_parent"
  604. android:layout_height="fill_parent">
  605. <TextView android:layout_width="fill_parent"
  606. android:layout_height="wrap_content"
  607. android:textColor="#000000"
  608. android:textSize="18dip"
  609. android:background="#00FF00"
  610. android:text="EditText编辑框测试"
  611. android:gravity="center_vertical|center_horizontal"
  612. />
  613. <EditText
  614. android:id="@+id/editview0"
  615. android:layout_width="fill_parent"
  616. android:layout_height="wrap_content"
  617. android:hint="请输入帐号"
  618. android:phoneNumber="true"
  619. />
  620. <EditText
  621. android:id="@+id/editview1"
  622. android:layout_width="fill_parent"
  623. android:layout_height="wrap_content"
  624. android:hint="请输入密码"
  625. android:password="true"
  626. />
  627. <Button
  628. android:id="@+id/editbutton0"
  629. android:layout_width="fill_parent"
  630. android:layout_height="wrap_content"
  631. android:text="确定"
  632. />
  633. </LinearLayout>
  634. 6.单项选择
  635. 使用RadioGroup 包住若干个RadioButton 来实现单项选择。监听每一个RadioGroup 就可以知道那个单选组中的第一个ID被按下。
  636. public class RadioActivity extends Activity {
  637. Context mContext = null;
  638. @Override
  639. protected void onCreate(Bundle savedInstanceState) {
  640. setContentView(R.layout.radioview);
  641. mContext = this;
  642. //单选组(只有在一个组中的按钮可以单选)
  643. RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radion0);
  644. //单选按钮(第一组)
  645. final RadioButton radioButton0 = (RadioButton)findViewById(R.id.radionButton0);
  646. final RadioButton radioButton1 = (RadioButton)findViewById(R.id.radionButton1);
  647. final RadioButton radioButton2 = (RadioButton)findViewById(R.id.radionButton2);
  648. radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  649. @Override
  650. public void onCheckedChanged(RadioGroup arg0, int checkID) {
  651. if(radioButton0.getId() == checkID) {
  652. Toast.makeText(RadioActivity.this, "您选中了第一组" + radioButton0.getText(), Toast.LENGTH_LONG).show();
  653. }else if(radioButton1.getId() == checkID) {
  654. Toast.makeText(RadioActivity.this, "您选中了第一组" + radioButton1.getText(), Toast.LENGTH_LONG).show();
  655. }else if(radioButton2.getId() == checkID) {
  656. Toast.makeText(RadioActivity.this, "您选中了第一组" + radioButton2.getText(), Toast.LENGTH_LONG).show();
  657. }
  658. }
  659. });
  660. RadioGroup radioGroup0 = (RadioGroup)findViewById(R.id.radion1);
  661. //单选按钮(第二组)
  662. final RadioButton radioButton3 = (RadioButton)findViewById(R.id.radionButton3);
  663. final RadioButton radioButton4 = (RadioButton)findViewById(R.id.radionButton4);
  664. final RadioButton radioButton5 = (RadioButton)findViewById(R.id.radionButton5);
  665. radioGroup0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  666. @Override
  667. public void onCheckedChanged(RadioGroup arg0, int checkID) {
  668. if(radioButton3.getId() == checkID) {
  669. Toast.makeText(RadioActivity.this, "您选中了第二组" + radioButton3.getText(), Toast.LENGTH_LONG).show();
  670. }else if(radioButton4.getId() == checkID) {
  671. Toast.makeText(RadioActivity.this, "您选中了第二组" + radioButton4.getText(), Toast.LENGTH_LONG).show();
  672. }else if(radioButton5.getId() == checkID) {
  673. Toast.makeText(RadioActivity.this, "您选中了第二组" + radioButton5.getText(), Toast.LENGTH_LONG).show();
  674. }
  675. }
  676. });
  677. super.onCreate(savedInstanceState);
  678. }
  679. }
  680. <?xml version="1.0" encoding="utf-8"?>
  681. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  682. android:orientation="vertical" android:layout_width="fill_parent"
  683. android:layout_height="fill_parent">
  684. <TextView android:layout_width="fill_parent"
  685. android:layout_height="wrap_content"
  686. android:textColor="#000000"
  687. android:textSize="18dip"
  688. android:background="#00FF00"
  689. android:text="单项选择测试第一组"
  690. android:gravity="center_vertical|center_horizontal"
  691. />
  692. <RadioGroup
  693. android:id="@+id/radion0"
  694. android:layout_width="fill_parent"
  695. android:layout_height="wrap_content" >
  696. <RadioButton
  697. android:id="@+id/radionButton0"
  698. android:layout_width="fill_parent"
  699. android:layout_height="wrap_content"
  700. android:text="item0"
  701. />
  702. <RadioButton
  703. android:id="@+id/radionButton1"
  704. android:layout_width="fill_parent"
  705. android:layout_height="wrap_content"
  706. android:text="item1"
  707. />
  708. <RadioButton
  709. android:id="@+id/radionButton2"
  710. android:layout_width="fill_parent"
  711. android:layout_height="wrap_content"
  712. android:text="item2"
  713. />
  714. </RadioGroup>
  715. <TextView android:layout_width="fill_parent"
  716. android:layout_height="wrap_content"
  717. android:textColor="#000000"
  718. android:textSize="18dip"
  719. android:background="#00FF00"
  720. android:text="单项选择测试第二组"
  721. android:gravity="center_vertical|center_horizontal"
  722. />
  723. <RadioGroup
  724. android:id="@+id/radion1"
  725. android:layout_width="fill_parent"
  726. android:layout_height="wrap_content" >
  727. <RadioButton
  728. android:id="@+id/radionButton3"
  729. android:layout_width="fill_parent"
  730. android:layout_height="wrap_content"
  731. android:text="item3"
  732. />
  733. <RadioButton
  734. android:id="@+id/radionButton4"
  735. android:layout_width="fill_parent"
  736. android:layout_height="wrap_content"
  737. android:text="item4"
  738. />
  739. <RadioButton
  740. android:id="@+id/radionButton5"
  741. android:layout_width="fill_parent"
  742. android:layout_height="wrap_content"
  743. android:text="item5"
  744. />
  745. </RadioGroup>
  746. </LinearLayout>
  747. 7.多项选择
  748. 使用系统控件Checkbox 监听每一个checkbox 的点击事件就可以确定那几个选项被选择了。
  749. public class CheckboxActivity extends Activity {
  750. //用来储存选中的内容
  751. ArrayList <String>item = new ArrayList<String>();
  752. @Override
  753. protected void onCreate(Bundle savedInstanceState) {
  754. setContentView(R.layout.checkboxview);
  755. CheckBox checkbox0 = (CheckBox)findViewById(R.id.checkboxview0);
  756. CheckBox checkbox1 = (CheckBox)findViewById(R.id.checkboxview1);
  757. CheckBox checkbox2 = (CheckBox)findViewById(R.id.checkboxview2);
  758. CheckBox checkbox3 = (CheckBox)findViewById(R.id.checkboxview3);
  759. Button button = (Button)findViewById(R.id.checkboxbutton);
  760. //对checkbox进行监听
  761. checkbox0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  762. @Override
  763. public void onCheckedChanged(CompoundButton button, boolean arg1) {
  764. String str = button.getText().toString();
  765. if (button.isChecked()) {
  766. item.add(str);
  767. } else {
  768. item.remove(str);
  769. }
  770. }
  771. });
  772. checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  773. @Override
  774. public void onCheckedChanged(CompoundButton button, boolean arg1) {
  775. String str = button.getText().toString();
  776. if (button.isChecked()) {
  777. item.add(str);
  778. } else {
  779. item.remove(str);
  780. }
  781. }
  782. });
  783. checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  784. @Override
  785. public void onCheckedChanged(CompoundButton button, boolean arg1) {
  786. String str = button.getText().toString();
  787. if (button.isChecked()) {
  788. item.add(str);
  789. } else {
  790. item.remove(str);
  791. }
  792. }
  793. });
  794. checkbox3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  795. @Override
  796. public void onCheckedChanged(CompoundButton button, boolean arg1) {
  797. String str = button.getText().toString();
  798. if (button.isChecked()) {
  799. item.add(str);
  800. } else {
  801. item.remove(str);
  802. }
  803. }
  804. });
  805. button.setOnClickListener(new OnClickListener() {
  806. @Override
  807. public void onClick(View arg0) {
  808. String str = item.toString();
  809. Toast.makeText(CheckboxActivity.this, "您选中了" + str, Toast.LENGTH_LONG).show();
  810. }
  811. });
  812. super.onCreate(savedInstanceState);
  813. }
  814. }
  815. <?xml version="1.0" encoding="utf-8"?>
  816. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  817. android:orientation="vertical" android:layout_width="fill_parent"
  818. android:layout_height="fill_parent">
  819. <TextView android:layout_width="fill_parent"
  820. android:layout_height="wrap_content"
  821. android:textColor="#000000"
  822. android:textSize="18dip"
  823. android:background="#00FF00"
  824. android:text="多项选择测试"
  825. android:gravity="center_vertical|center_horizontal"
  826. />
  827. <CheckBox
  828. android:id="@+id/checkboxview0"
  829. android:layout_width="fill_parent"
  830. android:layout_height="wrap_content"
  831. android:text="item0"
  832. />
  833. <CheckBox
  834. android:id="@+id/checkboxview1"
  835. android:layout_width="fill_parent"
  836. android:layout_height="wrap_content"
  837. android:text="item1"
  838. />
  839. <CheckBox
  840. android:id="@+id/checkboxview2"
  841. android:layout_width="fill_parent"
  842. android:layout_height="wrap_content"
  843. android:text="item2"
  844. />
  845. <CheckBox
  846. android:id="@+id/checkboxview3"
  847. android:layout_width="fill_parent"
  848. android:layout_height="wrap_content"
  849. android:text="item3"
  850. />
  851. <Button
  852. android:id="@+id/checkboxbutton"
  853. android:layout_width="fill_parent"
  854. android:layout_height="wrap_content"
  855. android:text="确定"
  856. />
  857. </LinearLayout>
  858. <?xml version="1.0" encoding="utf-8"?>
  859. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  860. android:orientation="vertical" android:layout_width="fill_parent"
  861. android:layout_height="fill_parent">
  862. <TextView android:layout_width="fill_parent"
  863. android:layout_height="wrap_content"
  864. android:textColor="#000000"
  865. android:textSize="18dip"
  866. android:background="#00FF00"
  867. android:text="多项选择测试"
  868. android:gravity="center_vertical|center_horizontal"
  869. />
  870. <CheckBox
  871. android:id="@+id/checkboxview0"
  872. android:layout_width="fill_parent"
  873. android:layout_height="wrap_content"
  874. android:text="item0"
  875. />
  876. <CheckBox
  877. android:id="@+id/checkboxview1"
  878. android:layout_width="fill_parent"
  879. android:layout_height="wrap_content"
  880. android:text="item1"
  881. />
  882. <CheckBox
  883. android:id="@+id/checkboxview2"
  884. android:layout_width="fill_parent"
  885. android:layout_height="wrap_content"
  886. android:text="item2"
  887. />
  888. <CheckBox
  889. android:id="@+id/checkboxview3"
  890. android:layout_width="fill_parent"
  891. android:layout_height="wrap_content"
  892. android:text="item3"
  893. />
  894. <Button
  895. android:id="@+id/checkboxbutton"
  896. android:layout_width="fill_parent"
  897. android:layout_height="wrap_content"
  898. android:text="确定"
  899. />
  900. </LinearLayout> </span>