[java] view plain copy print ?
  1. <spanstyle="font-size:18px">1.文本框TextView
  2. TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView,第一种是通过xml布局文件呈现,第二种是通过代码来呈现,由此可见Android的界面开发真的是非常灵活。
  3. publicclassTextViewActivityextendsActivity{
  4. @Override
  5. protectedvoidonCreate(BundlesavedInstanceState){
  6. setContentView(R.layout.textview);
  7. LinearLayoutll=(LinearLayout)findViewById(R.id.textviewll);
  8. TextViewtextView=newTextView(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. <?xmlversion="1.0"encoding="utf-8"?>
  25. <LinearLayoutxmlns: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. <TextViewandroid: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. publicclassWebViewActivityextendsActivity{
  42. WebViewwebView=null;
  43. staticfinalStringMIME_TYPE="text/html";
  44. staticfinalStringENCODING="utf-8";
  45. @Override
  46. protectedvoidonCreate(BundlesavedInstanceState){
  47. setContentView(R.layout.webview);
  48. webView=(WebView)findViewById(R.id.webview);
  49. webView.loadDataWithBaseURL(null,"<ahref='http://blog.csdn.net/xys289187120'>欢迎访问雨松MOMO的博客</a>",MIME_TYPE,ENCODING,null);
  50. super.onCreate(savedInstanceState);
  51. }
  52. }
  53. <?xmlversion="1.0"encoding="utf-8"?>
  54. <LinearLayoutxmlns: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. <TextViewandroid: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. <WebViewandroid: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. publicclassMenuActivityextendsActivity{
  73. @Override
  74. protectedvoidonCreate(BundlesavedInstanceState){
  75. setContentView(R.layout.menuview);
  76. super.onCreate(savedInstanceState);
  77. }
  78. @Override
  79. publicbooleanonCreateOptionsMenu(Menumenu){
  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. returnsuper.onCreateOptionsMenu(menu);
  87. }
  88. @Override
  89. publicbooleanonOptionsItemSelected(MenuItemitem){
  90. Dialog(item.getItemId());
  91. returnsuper.onOptionsItemSelected(item);
  92. }
  93. privatevoidDialog(intmessage){
  94. newAlertDialog.Builder(this).setMessage(
  95. "您单击第【"+message+"】项Menu菜单项.").show();
  96. }
  97. }
  98. <?xmlversion="1.0"encoding="utf-8"?>
  99. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  100. android:orientation="vertical"android:layout_width="fill_parent"
  101. android:layout_height="fill_parent">
  102. <TextViewandroid: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. publicclassButtonActivityextendsActivity{
  114. ContextmContext=null;
  115. @Override
  116. protectedvoidonCreate(BundlesavedInstanceState){
  117. setContentView(R.layout.buttonview);
  118. mContext=this;
  119. //普通按钮
  120. Buttonbutton0=(Button)findViewById(R.id.buttonview0);
  121. //设置按钮文字颜色
  122. button0.setTextColor(Color.BLUE);
  123. //设置按钮文字大小
  124. button0.setTextSize(30);
  125. //设置按钮监听点击事件
  126. button0.setOnClickListener(newOnClickListener(){
  127. @Override
  128. publicvoidonClick(Viewarg0){
  129. Toast.makeText(ButtonActivity.this,"您点击了‘这是一个按钮’",Toast.LENGTH_LONG).show();
  130. }
  131. });
  132. //带图片的按钮
  133. ImageButtonbutton1=(ImageButton)findViewById(R.id.buttonview1);
  134. //设置按钮监听点击事件
  135. button1.setOnClickListener(newOnClickListener(){
  136. @Override
  137. publicvoidonClick(Viewarg0){
  138. Toast.makeText(ButtonActivity.this,"您点击了一个带图片的按钮",Toast.LENGTH_LONG).show();
  139. }
  140. });
  141. super.onCreate(savedInstanceState);
  142. }
  143. }
  144. <?xmlversion="1.0"encoding="utf-8"?>
  145. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  146. android:orientation="vertical"android:layout_width="fill_parent"
  147. android:layout_height="fill_parent">
  148. <TextViewandroid: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. publicclassEditTextActivityextendsActivity{
  172. ContextmContext=null;
  173. @Override
  174. protectedvoidonCreate(BundlesavedInstanceState){
  175. setContentView(R.layout.editview);
  176. mContext=this;
  177. //帐号
  178. finalEditTexteditText0=(EditText)findViewById(R.id.editview0);
  179. //密码
  180. finalEditTexteditText1=(EditText)findViewById(R.id.editview1);
  181. //确认按钮
  182. Buttonbutton=(Button)findViewById(R.id.editbutton0);
  183. button.setOnClickListener(newOnClickListener(){
  184. @Override
  185. publicvoidonClick(Viewarg0){
  186. Stringusername=editText0.getText().toString();
  187. Stringpassword=editText1.getText().toString();
  188. Toast.makeText(EditTextActivity.this,"用户名:"+username+"密码:"+password,Toast.LENGTH_LONG).show();
  189. }
  190. });
  191. super.onCreate(savedInstanceState);
  192. }
  193. }
  194. <?xmlversion="1.0"encoding="utf-8"?>
  195. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  196. android:orientation="vertical"android:layout_width="fill_parent"
  197. android:layout_height="fill_parent">
  198. <TextViewandroid: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. publicclassRadioActivityextendsActivity{
  230. ContextmContext=null;
  231. @Override
  232. protectedvoidonCreate(BundlesavedInstanceState){
  233. setContentView(R.layout.radioview);
  234. mContext=this;
  235. //单选组(只有在一个组中的按钮可以单选)
  236. RadioGroupradioGroup=(RadioGroup)findViewById(R.id.radion0);
  237. //单选按钮(第一组)
  238. finalRadioButtonradioButton0=(RadioButton)findViewById(R.id.radionButton0);
  239. finalRadioButtonradioButton1=(RadioButton)findViewById(R.id.radionButton1);
  240. finalRadioButtonradioButton2=(RadioButton)findViewById(R.id.radionButton2);
  241. radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  242. @Override
  243. publicvoidonCheckedChanged(RadioGrouparg0,intcheckID){
  244. if(radioButton0.getId()==checkID){
  245. Toast.makeText(RadioActivity.this,"您选中了第一组"+radioButton0.getText(),Toast.LENGTH_LONG).show();
  246. }elseif(radioButton1.getId()==checkID){
  247. Toast.makeText(RadioActivity.this,"您选中了第一组"+radioButton1.getText(),Toast.LENGTH_LONG).show();
  248. }elseif(radioButton2.getId()==checkID){
  249. Toast.makeText(RadioActivity.this,"您选中了第一组"+radioButton2.getText(),Toast.LENGTH_LONG).show();
  250. }
  251. }
  252. });
  253. RadioGroupradioGroup0=(RadioGroup)findViewById(R.id.radion1);
  254. //单选按钮(第二组)
  255. finalRadioButtonradioButton3=(RadioButton)findViewById(R.id.radionButton3);
  256. finalRadioButtonradioButton4=(RadioButton)findViewById(R.id.radionButton4);
  257. finalRadioButtonradioButton5=(RadioButton)findViewById(R.id.radionButton5);
  258. radioGroup0.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  259. @Override
  260. publicvoidonCheckedChanged(RadioGrouparg0,intcheckID){
  261. if(radioButton3.getId()==checkID){
  262. Toast.makeText(RadioActivity.this,"您选中了第二组"+radioButton3.getText(),Toast.LENGTH_LONG).show();
  263. }elseif(radioButton4.getId()==checkID){
  264. Toast.makeText(RadioActivity.this,"您选中了第二组"+radioButton4.getText(),Toast.LENGTH_LONG).show();
  265. }elseif(radioButton5.getId()==checkID){
  266. Toast.makeText(RadioActivity.this,"您选中了第二组"+radioButton5.getText(),Toast.LENGTH_LONG).show();
  267. }
  268. }
  269. });
  270. super.onCreate(savedInstanceState);
  271. }
  272. }
  273. <?xmlversion="1.0"encoding="utf-8"?>
  274. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  275. android:orientation="vertical"android:layout_width="fill_parent"
  276. android:layout_height="fill_parent">
  277. <TextViewandroid: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. <TextViewandroid: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. publicclassCheckboxActivityextendsActivity{
  343. //用来储存选中的内容
  344. ArrayList<String>item=newArrayList<String>();
  345. @Override
  346. protectedvoidonCreate(BundlesavedInstanceState){
  347. setContentView(R.layout.checkboxview);
  348. CheckBoxcheckbox0=(CheckBox)findViewById(R.id.checkboxview0);
  349. CheckBoxcheckbox1=(CheckBox)findViewById(R.id.checkboxview1);
  350. CheckBoxcheckbox2=(CheckBox)findViewById(R.id.checkboxview2);
  351. CheckBoxcheckbox3=(CheckBox)findViewById(R.id.checkboxview3);
  352. Buttonbutton=(Button)findViewById(R.id.checkboxbutton);
  353. //对checkbox进行监听
  354. checkbox0.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  355. @Override
  356. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  357. Stringstr=button.getText().toString();
  358. if(button.isChecked()){
  359. item.add(str);
  360. }else{
  361. item.remove(str);
  362. }
  363. }
  364. });
  365. checkbox1.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  366. @Override
  367. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  368. Stringstr=button.getText().toString();
  369. if(button.isChecked()){
  370. item.add(str);
  371. }else{
  372. item.remove(str);
  373. }
  374. }
  375. });
  376. checkbox2.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  377. @Override
  378. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  379. Stringstr=button.getText().toString();
  380. if(button.isChecked()){
  381. item.add(str);
  382. }else{
  383. item.remove(str);
  384. }
  385. }
  386. });
  387. checkbox3.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  388. @Override
  389. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  390. Stringstr=button.getText().toString();
  391. if(button.isChecked()){
  392. item.add(str);
  393. }else{
  394. item.remove(str);
  395. }
  396. }
  397. });
  398. button.setOnClickListener(newOnClickListener(){
  399. @Override
  400. publicvoidonClick(Viewarg0){
  401. Stringstr=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. publicclassTextViewActivityextendsActivity{
  411. @Override
  412. protectedvoidonCreate(BundlesavedInstanceState){
  413. setContentView(R.layout.textview);
  414. LinearLayoutll=(LinearLayout)findViewById(R.id.textviewll);
  415. TextViewtextView=newTextView(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. <?xmlversion="1.0"encoding="utf-8"?>
  432. <LinearLayoutxmlns: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. <TextViewandroid: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. publicclassWebViewActivityextendsActivity{
  449. WebViewwebView=null;
  450. staticfinalStringMIME_TYPE="text/html";
  451. staticfinalStringENCODING="utf-8";
  452. @Override
  453. protectedvoidonCreate(BundlesavedInstanceState){
  454. setContentView(R.layout.webview);
  455. webView=(WebView)findViewById(R.id.webview);
  456. webView.loadDataWithBaseURL(null,"<ahref='http://blog.csdn.net/xys289187120'>欢迎访问雨松MOMO的博客</a>",MIME_TYPE,ENCODING,null);
  457. super.onCreate(savedInstanceState);
  458. }
  459. }
  460. <?xmlversion="1.0"encoding="utf-8"?>
  461. <LinearLayoutxmlns: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. <TextViewandroid: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. <WebViewandroid: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. publicclassMenuActivityextendsActivity{
  480. @Override
  481. protectedvoidonCreate(BundlesavedInstanceState){
  482. setContentView(R.layout.menuview);
  483. super.onCreate(savedInstanceState);
  484. }
  485. @Override
  486. publicbooleanonCreateOptionsMenu(Menumenu){
  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. returnsuper.onCreateOptionsMenu(menu);
  494. }
  495. @Override
  496. publicbooleanonOptionsItemSelected(MenuItemitem){
  497. Dialog(item.getItemId());
  498. returnsuper.onOptionsItemSelected(item);
  499. }
  500. privatevoidDialog(intmessage){
  501. newAlertDialog.Builder(this).setMessage(
  502. "您单击第【"+message+"】项Menu菜单项.").show();
  503. }
  504. }
  505. <?xmlversion="1.0"encoding="utf-8"?>
  506. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  507. android:orientation="vertical"android:layout_width="fill_parent"
  508. android:layout_height="fill_parent">
  509. <TextViewandroid: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. publicclassButtonActivityextendsActivity{
  521. ContextmContext=null;
  522. @Override
  523. protectedvoidonCreate(BundlesavedInstanceState){
  524. setContentView(R.layout.buttonview);
  525. mContext=this;
  526. //普通按钮
  527. Buttonbutton0=(Button)findViewById(R.id.buttonview0);
  528. //设置按钮文字颜色
  529. button0.setTextColor(Color.BLUE);
  530. //设置按钮文字大小
  531. button0.setTextSize(30);
  532. //设置按钮监听点击事件
  533. button0.setOnClickListener(newOnClickListener(){
  534. @Override
  535. publicvoidonClick(Viewarg0){
  536. Toast.makeText(ButtonActivity.this,"您点击了‘这是一个按钮’",Toast.LENGTH_LONG).show();
  537. }
  538. });
  539. //带图片的按钮
  540. ImageButtonbutton1=(ImageButton)findViewById(R.id.buttonview1);
  541. //设置按钮监听点击事件
  542. button1.setOnClickListener(newOnClickListener(){
  543. @Override
  544. publicvoidonClick(Viewarg0){
  545. Toast.makeText(ButtonActivity.this,"您点击了一个带图片的按钮",Toast.LENGTH_LONG).show();
  546. }
  547. });
  548. super.onCreate(savedInstanceState);
  549. }
  550. }
  551. <?xmlversion="1.0"encoding="utf-8"?>
  552. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  553. android:orientation="vertical"android:layout_width="fill_parent"
  554. android:layout_height="fill_parent">
  555. <TextViewandroid: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. publicclassEditTextActivityextendsActivity{
  579. ContextmContext=null;
  580. @Override
  581. protectedvoidonCreate(BundlesavedInstanceState){
  582. setContentView(R.layout.editview);
  583. mContext=this;
  584. //帐号
  585. finalEditTexteditText0=(EditText)findViewById(R.id.editview0);
  586. //密码
  587. finalEditTexteditText1=(EditText)findViewById(R.id.editview1);
  588. //确认按钮
  589. Buttonbutton=(Button)findViewById(R.id.editbutton0);
  590. button.setOnClickListener(newOnClickListener(){
  591. @Override
  592. publicvoidonClick(Viewarg0){
  593. Stringusername=editText0.getText().toString();
  594. Stringpassword=editText1.getText().toString();
  595. Toast.makeText(EditTextActivity.this,"用户名:"+username+"密码:"+password,Toast.LENGTH_LONG).show();
  596. }
  597. });
  598. super.onCreate(savedInstanceState);
  599. }
  600. }
  601. <?xmlversion="1.0"encoding="utf-8"?>
  602. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  603. android:orientation="vertical"android:layout_width="fill_parent"
  604. android:layout_height="fill_parent">
  605. <TextViewandroid: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. publicclassRadioActivityextendsActivity{
  637. ContextmContext=null;
  638. @Override
  639. protectedvoidonCreate(BundlesavedInstanceState){
  640. setContentView(R.layout.radioview);
  641. mContext=this;
  642. //单选组(只有在一个组中的按钮可以单选)
  643. RadioGroupradioGroup=(RadioGroup)findViewById(R.id.radion0);
  644. //单选按钮(第一组)
  645. finalRadioButtonradioButton0=(RadioButton)findViewById(R.id.radionButton0);
  646. finalRadioButtonradioButton1=(RadioButton)findViewById(R.id.radionButton1);
  647. finalRadioButtonradioButton2=(RadioButton)findViewById(R.id.radionButton2);
  648. radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  649. @Override
  650. publicvoidonCheckedChanged(RadioGrouparg0,intcheckID){
  651. if(radioButton0.getId()==checkID){
  652. Toast.makeText(RadioActivity.this,"您选中了第一组"+radioButton0.getText(),Toast.LENGTH_LONG).show();
  653. }elseif(radioButton1.getId()==checkID){
  654. Toast.makeText(RadioActivity.this,"您选中了第一组"+radioButton1.getText(),Toast.LENGTH_LONG).show();
  655. }elseif(radioButton2.getId()==checkID){
  656. Toast.makeText(RadioActivity.this,"您选中了第一组"+radioButton2.getText(),Toast.LENGTH_LONG).show();
  657. }
  658. }
  659. });
  660. RadioGroupradioGroup0=(RadioGroup)findViewById(R.id.radion1);
  661. //单选按钮(第二组)
  662. finalRadioButtonradioButton3=(RadioButton)findViewById(R.id.radionButton3);
  663. finalRadioButtonradioButton4=(RadioButton)findViewById(R.id.radionButton4);
  664. finalRadioButtonradioButton5=(RadioButton)findViewById(R.id.radionButton5);
  665. radioGroup0.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  666. @Override
  667. publicvoidonCheckedChanged(RadioGrouparg0,intcheckID){
  668. if(radioButton3.getId()==checkID){
  669. Toast.makeText(RadioActivity.this,"您选中了第二组"+radioButton3.getText(),Toast.LENGTH_LONG).show();
  670. }elseif(radioButton4.getId()==checkID){
  671. Toast.makeText(RadioActivity.this,"您选中了第二组"+radioButton4.getText(),Toast.LENGTH_LONG).show();
  672. }elseif(radioButton5.getId()==checkID){
  673. Toast.makeText(RadioActivity.this,"您选中了第二组"+radioButton5.getText(),Toast.LENGTH_LONG).show();
  674. }
  675. }
  676. });
  677. super.onCreate(savedInstanceState);
  678. }
  679. }
  680. <?xmlversion="1.0"encoding="utf-8"?>
  681. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  682. android:orientation="vertical"android:layout_width="fill_parent"
  683. android:layout_height="fill_parent">
  684. <TextViewandroid: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. <TextViewandroid: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. publicclassCheckboxActivityextendsActivity{
  750. //用来储存选中的内容
  751. ArrayList<String>item=newArrayList<String>();
  752. @Override
  753. protectedvoidonCreate(BundlesavedInstanceState){
  754. setContentView(R.layout.checkboxview);
  755. CheckBoxcheckbox0=(CheckBox)findViewById(R.id.checkboxview0);
  756. CheckBoxcheckbox1=(CheckBox)findViewById(R.id.checkboxview1);
  757. CheckBoxcheckbox2=(CheckBox)findViewById(R.id.checkboxview2);
  758. CheckBoxcheckbox3=(CheckBox)findViewById(R.id.checkboxview3);
  759. Buttonbutton=(Button)findViewById(R.id.checkboxbutton);
  760. //对checkbox进行监听
  761. checkbox0.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  762. @Override
  763. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  764. Stringstr=button.getText().toString();
  765. if(button.isChecked()){
  766. item.add(str);
  767. }else{
  768. item.remove(str);
  769. }
  770. }
  771. });
  772. checkbox1.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  773. @Override
  774. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  775. Stringstr=button.getText().toString();
  776. if(button.isChecked()){
  777. item.add(str);
  778. }else{
  779. item.remove(str);
  780. }
  781. }
  782. });
  783. checkbox2.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  784. @Override
  785. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  786. Stringstr=button.getText().toString();
  787. if(button.isChecked()){
  788. item.add(str);
  789. }else{
  790. item.remove(str);
  791. }
  792. }
  793. });
  794. checkbox3.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  795. @Override
  796. publicvoidonCheckedChanged(CompoundButtonbutton,booleanarg1){
  797. Stringstr=button.getText().toString();
  798. if(button.isChecked()){
  799. item.add(str);
  800. }else{
  801. item.remove(str);
  802. }
  803. }
  804. });
  805. button.setOnClickListener(newOnClickListener(){
  806. @Override
  807. publicvoidonClick(Viewarg0){
  808. Stringstr=item.toString();
  809. Toast.makeText(CheckboxActivity.this,"您选中了"+str,Toast.LENGTH_LONG).show();
  810. }
  811. });
  812. super.onCreate(savedInstanceState);
  813. }
  814. }
  815. <?xmlversion="1.0"encoding="utf-8"?>
  816. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  817. android:orientation="vertical"android:layout_width="fill_parent"
  818. android:layout_height="fill_parent">
  819. <TextViewandroid: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. <?xmlversion="1.0"encoding="utf-8"?>
  859. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  860. android:orientation="vertical"android:layout_width="fill_parent"
  861. android:layout_height="fill_parent">
  862. <TextViewandroid: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>


更多相关文章

  1. Dialog与DialogFragment设置大小位置的区别
  2. Android中使用OnClickListener接口实现按钮点击的低级失误
  3. sdk platform tools is missing please user the sdk manager to
  4. Android(安卓)- 用WebView开发简单的浏览器
  5. Android(安卓)时间选择器 PickerView,的详细使用
  6. android studio 更改 app 包名和包的长度
  7. Android(安卓)Canvas 旋转Rect功能实施
  8. Android中MAT、GIMP查看内存占用
  9. 多级ListView

随机推荐

  1. 深入解读Linux与Android的相互关系
  2. Android通过WebView与JS交互的全面方式
  3. ContentProvider
  4. Android逆向之旅---Android中的sharedUse
  5. Android(安卓)Building System
  6. 【Android(安卓)Dev Guide - 01】 - What
  7. 《疯狂Android讲义》
  8. Android零基础入门第33节:Android事件处理
  9. Android应用程序消息处理机制(Looper、Han
  10. Android(安卓)UI Action Bar之ActionBarS