android用户界面-组件Widget-常用组件

简介:

用户会员注册实例

介绍控件

文本框TextView

编辑框EditText

密码文本框EditText

单选按钮RadioButton

复选框CheckBox

开关按钮ToggleButton

下拉列表Spinner

实例:

注册页面

/Chapter04_UI_CommonWidget/src/com/amaker/test/MainActivity.java

 

 
  1. 代码  
  2.  
  3. package com.amaker.test;  
  4.  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.Button;  
  12. import android.widget.CheckBox;  
  13. import android.widget.EditText;  
  14. import android.widget.RadioButton;  
  15. import android.widget.Spinner;  
  16. import android.widget.ToggleButton;  
  17.  
  18. public class MainActivity extends Activity {  
  19.       
  20.     private Button register,cancel;  
  21.     private ToggleButton marriged;  
  22.     private RadioButton male,female;  
  23.     private EditText username,password;  
  24.     private Spinner position;  
  25.     private CheckBox reading,swimming;  
  26.       
  27.     @Override 
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.main);  
  31.           
  32.         username = (EditText)findViewById(R.id.username);  
  33.         password = (EditText)findViewById(R.id.password);  
  34.           
  35.         male = (RadioButton)findViewById(R.id.male);  
  36.         female = (RadioButton)findViewById(R.id.female);  
  37.           
  38.         reading = (CheckBox)findViewById(R.id.reading);  
  39.         swimming = (CheckBox)findViewById(R.id.swimming);  
  40.           
  41.         marriged = (ToggleButton)findViewById(R.id.marriged);  
  42.           
  43.         position = (Spinner)findViewById(R.id.position);  
  44.           
  45.         String[] str = {"CEO","CFO","PM"};  
  46.           
  47.         ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item,str);  
  48.           
  49.         position.setAdapter(aa);  
  50.           
  51.         register = (Button)findViewById(R.id.register);  
  52.         cancel = (Button)findViewById(R.id.cancel);  
  53.           
  54.         register.setOnClickListener(new OnClickListener() {  
  55.             public void onClick(View v) {  
  56.                 Bundle b = new Bundle();  
  57.                 b.putString("username""用户名称:"+username.getText().toString());  
  58.                 b.putString("password""用户密码:"+password.getText().toString());  
  59.                   
  60.                 if(male.isChecked()){  
  61.                     b.putString("gender""性别:男");  
  62.                 }else{  
  63.                     b.putString("gender""性别:女");  
  64.                 }  
  65.                 String temp = "爱好:";  
  66.                 if(reading.isChecked()){  
  67.                     temp+="阅读";  
  68.                 }  
  69.                 if(swimming.isChecked()){  
  70.                     temp+=" ";  
  71.                     temp+="游泳";  
  72.                 }  
  73.                   
  74.                 b.putString("hobby", temp);  
  75.                   
  76.                 if(marriged.isChecked()){  
  77.                     b.putString("marriged""婚否:已婚");  
  78.                 }else{  
  79.                     b.putString("marriged""婚否:未婚");  
  80.                 }  
  81.                   
  82.                 b.putString("position","职位:"+ position.getSelectedItem().toString());  
  83.                   
  84.                 Intent intent = new Intent(MainActivity.this,ResultActivity.class);  
  85.                   
  86.                 intent.putExtra("data", b);  
  87.                   
  88.                 startActivity(intent);  
  89.             }  
  90.         });  
  91.           
  92.     }  

注册结果页面

/Chapter04_UI_CommonWidget/src/com/amaker/test/ResultActivity.java

 

 
  1. 代码  
  2.  
  3. package com.amaker.test;  
  4.  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. import android.app.Activity;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.ListView;  
  12.  
  13. public class ResultActivity extends Activity{  
  14.     private ListView listView;  
  15.       
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.           
  19.         setContentView(R.layout.result);  
  20.         listView = (ListView) findViewById(R.id.ListView01);  
  21.           
  22.         Intent intent = this.getIntent();  
  23.           
  24.         Bundle b = intent.getBundleExtra("data");  
  25.           
  26.         System.out.println(b.getString("username"));  
  27.           
  28.         List list =  new ArrayList();  
  29.           
  30.         list.add(b.getString("username"));  
  31.         list.add(b.getString("password"));  
  32.         list.add(b.getString("position"));  
  33.           
  34.         list.add(b.getString("gender"));  
  35.         list.add(b.getString("hobby"));  
  36.         list.add(b.getString("marriged"));  
  37.           
  38.         ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);  
  39.           
  40.         listView.setAdapter(adapter);  
  41.           
  42.     }  

布局文件

/Chapter04_UI_CommonWidget/res/layout/main.xml

 

 
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     android:orientation="vertical" 
  6.     android:layout_width="fill_parent" 
  7.     android:layout_height="fill_parent" 
  8.     > 
  9.       
  10.       
  11.  
  12. <TableLayout   
  13.     android:id="@+id/TableLayout01"   
  14.     android:layout_width="wrap_content"   
  15.     android:layout_height="wrap_content" 
  16.     android:stretchColumns="1" 
  17.     > 
  18.       
  19.       
  20.     <TableRow   
  21.     android:id="@+id/TableRow01"   
  22.     android:layout_width="wrap_content"   
  23.     android:layout_height="wrap_content"> 
  24.         <TextView   
  25.         android:text="用户名称"   
  26.         android:id="@+id/TextView01"   
  27.         android:layout_width="wrap_content"   
  28.         android:layout_height="wrap_content"></TextView> 
  29.           
  30.         <EditText   
  31.         android:text=""   
  32.         android:id="@+id/username"   
  33.         android:layout_width="wrap_content"   
  34.         android:layout_height="wrap_content" 
  35.           
  36.         ></EditText> 
  37.     </TableRow> 
  38.       
  39.     <TableRow   
  40.     android:id="@+id/TableRow02"   
  41.     android:layout_width="wrap_content"   
  42.     android:layout_height="wrap_content"> 
  43.         <TextView   
  44.         android:text="用户密码"   
  45.         android:id="@+id/TextView02"   
  46.         android:layout_width="wrap_content"   
  47.         android:layout_height="wrap_content"></TextView> 
  48.           
  49.         <EditText   
  50.         android:text=""   
  51.         android:id="@+id/password"   
  52.         android:layout_width="wrap_content"   
  53.         android:layout_height="wrap_content" 
  54.         android:password="true" 
  55.           
  56.         ></EditText> 
  57.     </TableRow> 
  58.       
  59.     <TableRow   
  60.     android:id="@+id/TableRow03"   
  61.     android:layout_width="wrap_content"   
  62.     android:layout_height="wrap_content"> 
  63.         <TextView   
  64.         android:text="性别"   
  65.         android:id="@+id/TextView03"   
  66.         android:layout_width="wrap_content"   
  67.         android:layout_height="wrap_content"></TextView> 
  68.           
  69.         <RadioGroup   
  70.         android:id="@+id/gender_g"   
  71.         android:layout_width="wrap_content"   
  72.         android:layout_height="wrap_content"> 
  73.           
  74.         <RadioButton   
  75.         android:text="男"   
  76.         android:id="@+id/male"   
  77.         android:layout_width="wrap_content"   
  78.         android:layout_height="wrap_content"></RadioButton> 
  79.           
  80.         <RadioButton   
  81.         android:text="女"   
  82.         android:id="@+id/female"   
  83.         android:layout_width="wrap_content"   
  84.         android:layout_height="wrap_content"></RadioButton> 
  85.           
  86.           
  87.         </RadioGroup> 
  88.     </TableRow> 
  89.       
  90.       
  91.     <TableRow   
  92.     android:id="@+id/TableRow04"   
  93.     android:layout_width="wrap_content"   
  94.     android:layout_height="wrap_content"> 
  95.         <TextView   
  96.         android:text="婚否"   
  97.         android:id="@+id/TextView04"   
  98.         android:layout_width="wrap_content"   
  99.         android:layout_height="wrap_content"></TextView> 
  100.           
  101.           
  102.     <ToggleButton   
  103.     android:text="@+id/ToggleButton01"   
  104.     android:id="@+id/marriged"   
  105.     android:layout_width="wrap_content"   
  106.     android:layout_height="wrap_content"></ToggleButton> 
  107. </TableRow> 
  108.       
  109.     <TableRow   
  110.     android:id="@+id/TableRow05"   
  111.     android:layout_width="wrap_content"   
  112.     android:layout_height="wrap_content" 
  113.     > 
  114.         <TextView   
  115.         android:text="爱好"   
  116.         android:id="@+id/hobby"   
  117.         android:layout_width="wrap_content"   
  118.         android:layout_height="wrap_content"></TextView> 
  119.           
  120.         <CheckBox   
  121.         android:text="阅读"   
  122.         android:id="@+id/reading"   
  123.         android:layout_width="wrap_content"   
  124.         android:layout_height="wrap_content" 
  125.         android:layout_column="1" 
  126.         ></CheckBox> 
  127.         <CheckBox   
  128.         android:text="游泳"   
  129.         android:id="@+id/swimming"   
  130.         android:layout_width="wrap_content"   
  131.         android:layout_height="wrap_content" 
  132.         android:layout_column="1" 
  133.         ></CheckBox> 
  134.           
  135.           
  136.     </TableRow> 
  137.       
  138.       
  139.     <TableRow   
  140.     android:id="@+id/TableRow06"   
  141.     android:layout_width="wrap_content"   
  142.     android:layout_height="wrap_content"> 
  143.         <TextView   
  144.         android:text="职务"   
  145.         android:id="@+id/TextView05"   
  146.         android:layout_width="wrap_content"   
  147.         android:layout_height="wrap_content"></TextView> 
  148.           
  149.         <Spinner   
  150.         android:id="@+id/position"   
  151.         android:layout_width="wrap_content"   
  152.         android:layout_height="wrap_content"></Spinner> 
  153.     </TableRow> 
  154.       
  155.       
  156.     <TableRow   
  157.     android:id="@+id/TableRow07"   
  158.     android:layout_width="wrap_content"   
  159.     android:layout_height="wrap_content"> 
  160.         <Button   
  161.         android:text="取消"   
  162.         android:id="@+id/cancel"   
  163.         android:layout_width="wrap_content"   
  164.         android:layout_height="wrap_content"></Button> 
  165.           
  166.         <Button   
  167.         android:text="注册"   
  168.         android:id="@+id/register"   
  169.         android:layout_width="wrap_content"   
  170.         android:layout_height="wrap_content"></Button> 
  171.           
  172.     </TableRow> 
  173.       
  174. </TableLayout> 
  175. </LinearLayout> 

/Chapter04_UI_CommonWidget/res/layout/result.xml

 

 
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     android:orientation="vertical" 
  6.     android:layout_width="fill_parent" 
  7.     android:layout_height="fill_parent" 
  8.     > 
  9.     <ListView   
  10.     android:id="@+id/ListView01"   
  11.     android:layout_width="wrap_content"   
  12.     android:layout_height="wrap_content"></ListView> 
  13. </LinearLayout> 

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080698

相关文章
|
2月前
|
设计模式 Android开发
[Android 四大组件] --- BroadcastReceiver
[Android 四大组件] --- BroadcastReceiver
33 0
|
3月前
|
Android开发 开发者
什么是Android Jetpack,它包括哪些组件?
什么是Android Jetpack,它包括哪些组件?
42 0
|
4月前
|
数据库 Android开发
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
54 0
|
4月前
|
XML Java Android开发
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信)
91 0
|
2天前
|
数据库 Android开发 开发者
安卓应用开发:构建高效用户界面的策略
【4月更文挑战第24天】 在竞争激烈的移动应用市场中,一个流畅且响应迅速的用户界面(UI)是吸引和保留用户的关键。针对安卓平台,开发者面临着多样化的设备和系统版本,这增加了构建高效UI的复杂性。本文将深入分析安卓平台上构建高效用户界面的最佳实践,包括布局优化、资源管理和绘制性能的考量,旨在为开发者提供实用的技术指南,帮助他们创建更流畅的用户体验。
|
14天前
|
存储 数据库 Android开发
构建高效安卓应用:采用Jetpack架构组件优化用户体验
【4月更文挑战第12天】 在当今快速发展的数字时代,Android 应用程序的流畅性与响应速度对用户满意度至关重要。为提高应用性能并降低维护成本,开发者需寻求先进的技术解决方案。本文将探讨如何利用 Android Jetpack 中的架构组件 — 如 LiveData、ViewModel 和 Room — 来构建高质量的安卓应用。通过具体实施案例分析,我们将展示这些组件如何协同工作以实现数据持久化、界面与逻辑分离,以及确保数据的即时更新,从而优化用户体验并提升应用的可维护性和可测试性。
|
1月前
|
数据可视化 测试技术 Android开发
安卓应用开发:打造高效用户界面的五大技巧
【2月更文挑战第30天】在竞争激烈的应用市场中,一个流畅且直观的用户界面(UI)对于安卓应用的成功至关重要。本文将探讨五个关键的UI设计技巧,这些技巧旨在提升用户体验并优化性能。我们将深入分析布局优化、资源管理、动画效果、响应式设计和测试流程等方面,并提供实用的代码示例和最佳实践,帮助开发者构建既美观又高效的安卓应用。
|
1月前
|
编解码 测试技术 Android开发
安卓应用开发:构建高效用户界面的实用指南
【2月更文挑战第29天】在移动应用开发的世界中,创建一个流畅、直观且响应迅速的用户界面(UI)对于吸引和保持用户至关重要。本篇文章旨在向安卓开发者展示如何通过优化布局设计、使用现代UI框架以及利用Android Studio提供的工具来构建高效的用户界面。我们将深入探讨如何减少内存消耗、提升渲染性能,并确保应用在不同设备和屏幕尺寸上的兼容性。跟随本文的指导,你将能够提高应用的整体用户体验,从而在竞争激烈的市场中脱颖而出。
|
2月前
|
数据可视化 Android开发
[Android 四大组件] --- Service
[Android 四大组件] --- Service
24 0
|
2月前
|
Android开发
[Android 四大组件] --- Activity
[Android 四大组件] --- Activity
22 1