一、单选框
  1.  
  2. 单选框实例程序:
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1. xmlns:tools="http://schemas.android.com/tools"
  1. android:layout_width="fill_parent"
  1. android:layout_height="fill_parent"
  1. android:orientation="vertical"
  1. tools:context=".MainActivity" >
  1.  
  1. <!-- checkedButton 表示默认为woman的意思,它能够自动识别下面id为woman的控件 -->
  1. <RadioGroup
  1. android:layout_width="fill_parent"
  1. android:layout_height="fill_parent"
  1. android:orientation="vertical"
  1. android:checkedButton="@+id/woman"
  1. android:id="@+id/sex"
  1. >
  1. <RadioButton
  1. android:text="@string/man"
  1. android:id="@+id/man"
  1. ></RadioButton>
  1. <RadioButton
  1. android:text="@string/woman"
  1. android:id="@id/woman"
  1. />
  1. </RadioGroup>
  1.  
  1. </LinearLayout>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

二、复选框

 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1. xmlns:tools="http://schemas.android.com/tools"
  1. android:layout_width="fill_parent"
  1. android:layout_height="fill_parent"
  1. android:orientation="vertical"
  1. tools:context=".MainActivity" >
  1.  
  1. <!-- checkedButton 表示默认为woman的意思,它能够自动识别下面id为woman的控件 -->
  1. <TextView
  1. android:layout_width="fill_parent"
  1. android:layout_height="wrap_content"
  1. android:text="@string/favoriteString"
  1. android:id="@+id/favorite"/>
  1. <RelativeLayout
  1. android:layout_width="fill_parent"
  1. android:layout_height="fill_parent"
  1. >
  1. <CheckBox
  1. android:layout_width="wrap_content"
  1. android:layout_height="wrap_content"
  1. android:text="@string/pingpong"
  1. android:id="@+id/my"
  1. ></CheckBox>
  1. <CheckBox
  1. android:layout_width="wrap_content"
  1. android:layout_height="wrap_content"
  1. android:text="@string/pingpong"
  1. android:layout_toRightOf="@id/my"
  1. android:id="@+id/my2"
  1. ></CheckBox>
  1. </RelativeLayout>
  1.  
  1. </LinearLayout>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

三、ListView

下面我们来学习 ListView 类的常用方法。

� setAdapter(ListAdapter adapter)

为 ListView 绑定一个 Adapter

� setChoiceMode(int choiceMode)

为 ListView 指定一个显示的模式,可选值有三个 CHOICE_MODE_NONE(默认值,没

有单选或多选效果) 、 CHOICE_MODE_SINGLE (单选框效果) 、 CHOICE_MODE_MULTIPLE

(多选框效果) ;

� setOnItemClickListener (AdapterView.OnItemClickListener listener)

为其注册一个元素被点击事件的监听器,当其中某一项被点击时调用其参 数

listener 中的 onItemClick()方法。

  1. package com.buu.listview;
  1. import android.os.Bundle;
  1. import android.app.Activity;
  1. import android.view.Menu;
  1. import android.view.View;
  1. import android.view.View.OnClickListener;
  1. import android.widget.AdapterView.OnItemClickListener;
  1. import android.widget.AdapterView;
  1. import android.widget.ArrayAdapter;
  1. import android.widget.ListAdapter;
  1. import android.widget.ListView;
  1. import android.widget.Toast;
  1.  
  1. public class MainActivity extends Activity {
  1. private String[] options;
  1. ListView lView;
  1.  
  1. @Override
  1. protected void onCreate(Bundle savedInstanceState) {
  1. super.onCreate(savedInstanceState);
  1. setContentView(R.layout.activity_main); //主界面必须先显示,不然listview也就不存在了。
  1. options = getResources().getStringArray(R.array.options);
  1. lView =(ListView) findViewById(R.id.listView1);
  1. // ListAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.main_lv_text, options);
  1. ListAdapter adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, options);
  1. lView.setAdapter(adapter);
  1. lView.setOnItemClickListener(new OnItemClickListener() {
  1. @Override
  1. public void onItemClick(AdapterView<?> parent, View view, int position,
  1. long id) {
  1. // TODO Auto-generated method stub
  1. String option = options[position];
  1. Toast.makeText(MainActivity.this, "你选择的是:"+option, 2000).show();
  1. }
  1. });
  1. }
  1.  
  1. @Override
  1. public boolean onCreateOptionsMenu(Menu menu) {
  1. // Inflate the menu; this adds items to the action bar if it is present.
  1. getMenuInflater().inflate(R.menu.main, menu);
  1. return true;
  1. }
  1.  
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

注意比较下面的方式:

  1. listView = (ListView) findViewById(R.id.listview);
  1. //创建一个ArrayAdapter
  1. ArrayAdapter adapter =
  1. new ArrayAdapter(this, android.R.layout.simple_list_item_1,name);
  1. listView.setAdapter(adapter);
  1. //listView注册一个元素点击事件监听器
  1. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  1. @Override
  1. //当某个元素被点击时调用该方法
  1. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long
  1. arg3) {
  1. Toast.makeText(ListViewActivity.this,name[arg2] ,
  1. Toast.LENGTH_LONG).show();
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

说明:

ArrayAdapter adapter = new ArrayAdapter(Context context, int

textViewResourceId, Object[] objects);

ArrayAdapter构造方法的参数解释:

context :当前的Context对象

textViewResourceId:一个包含了TextView元素的布局文件,用来指定ListView中的每一

项的显示格式。如前面介绍过的,

android.R.layout.simple_list_item_1是Android平台自带的一个

布局文件,里面只包含一个TextView标签。其内容如下:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@android:id/text1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceLarge"

android:gravity="center_vertical"

android:paddingLeft="6dip"

android:minHeight="?android:attr/listPreferredItemHeight"

/>

objects:要显示的数据,为一个数组

� onItemClick(AdapterView<?> parent, View view, int position, long id)

参数介绍:

parent:被点击的ListView对象

view:被点击的那一项

position:被点击的那一项在ListView中的位置

id :被选中的那一行的id

四、下拉列表框(Spinner)

手机的屏幕较小,因此使用下拉列表来进行选择式输入是一个非常好的方式。Spinner

与 ListView 一样,也是 AdapterView 的一个间接子类,是一个显示数据的窗口。

Spinner 类常用的方法如下:

� Spinner.getItemAtPosition(Spinner.getSelectedItemPosition()); 获取下拉列

表框的值

� 调 用 setOnItemSelectedListener() 方 法, 处理 下拉 列表 框被 选择 事件 , 把

AdapterView.OnItemSelectedListener 实例作为参数传入

可以在 Java 代码中通过 Adapter 绑定数据,也可以在布局文件中直接引用在资源文件

中定义的数组。

 

 

编写arrays.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string-array name="colors">
  4. <item>red</item>
  5. <item>orange</item>
  6. <item>yellow</item>
  7. <item>green</item>
  8. <item>blue</item>
  9. <item>violet</item>
  10. </string-array>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

编写main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/colors"
  10. ></TextView>
  11.  
  12. <Spinner
  13. android:id="@+id/spinner"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:prompt="@string/red"
  17. android:entries="@array/colors"
  18. >
  19. </Spinner>
  20. </LinearLayout>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

其中:

说明:

android:prompt="@string/color_prompt"

设置弹出下拉列表的标题。

android:entries="@array/colors"

指定下拉列表中的数据。本例为在arrays.xm文件中定义的colors数组。

widget 常用UI控件介绍的更多相关文章

  1. iOS基础UI控件介绍-Swift版

    iOS基础UI控件总结 iOS基础控件包括以下几类: 1.继承自NSObject:(暂列为控件) UIColor //颜色 UIImage //图像 2.继承自UIView: 只能相应手势UIGest ...

  2. Android 常用UI控件之TabHost(5)Tab栏在底部且在最上层也不盖tab页

    tab栏在底部 <TabHost android:id="@android:id/tabhost" android:layout_width="match_pare ...

  3. 常用Tables控件介绍(一)

    1.DataTables Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 分页,即时搜索和排序 几乎支持任何数据源:DOM, jav ...

  4. Android 常用UI控件之TabHost(4)实现当Tab栏有多个tab时,可以左右滑动

    <!-- <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_wi ...

  5. Android 常用UI控件之TabHost(3)在4.0不显示图标的解决方案

    1,自定义 TabWidget 上每个tab的view 2,用多个图片

  6. Android 常用UI控件之TabHost(2)简单示例

    1,布局 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool ...

  7. Android 常用UI控件之TabHost(1)TabHost的两种布局方式

    TabHost是Android中的tab组件. TabHost布局文件的基本结构 TabHost下有个layout,这个layout中有TabWidget与FrameLayout.TabWidget是 ...

  8. Android 常用UI控件之Tab控件的实现方案

    实现Tab的方式有多种 1,ActionBar有两种模式可以实现,但是已经过期 tab模式tab在顶部,分裂模式tab在底部(同时所有action item都在底部). 2,PagerTitleStr ...

  9. 常用Tables控件介绍(三)

    向datagrid中添加临时记录: 代码: $(function(){ fun={ add:function(){ $.ajaxSettings.async=false; var rows=$('#d ...

随机推荐

  1. [SDOI2015][bzoj3994] 约数个数和 [莫比乌斯反演]

    题面: 传送门 思路: 首先,我们需要证明一个结论:d(i*j)等于sigma(gcd(x,y)==1),其中x为i的约数,y为j的约数 对于nm的每一个质因子pi分别考虑,设n = pi^ai + ...

  2. BZOJ3884 上帝与集合的正确用法 【欧拉定理】

    题目 对于100%的数据,T<=1000,p<=10^7 题解 来捉这道神题 欧拉定理的一般形式: \[a^{m} \equiv a^{m \mod \varphi(p) + [m \ge ...

  3. topK问题解法

    topK问题的最佳解法是堆排,下面介绍用堆排来解决该问题. 堆排解决topK问题的思路,取出前K个数,最重要的就是要减少比较的次数,用堆排维护一个K大小的堆,比如一个小顶堆,则堆顶为堆中最小的值,将堆 ...

  4. ionic2如何调用百度地图

    使用ionic2接入百度地图 在index.html中引入百度地图的js类库 <script type="text/javascript" src="http:// ...

  5. 海拔(bzoj 2007)

    Description YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作一个 正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1) ...

  6. pat 团体天梯 L3-011. 直捣黄龙

    L3-011. 直捣黄龙 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题是一部战争大片 —— 你需要从己方大本营出发,一路 ...

  7. Xcode打包和生成ipa文件

    1.生成Archive文档 a) 需将左上角红色方框里的设备类型选为ios device,不能选择具体的设备类型,否则不能生成Archive文档: b) 中部选择Team的方框,可此时选,也在后续ex ...

  8. MySQL 手动主从同步不锁表

    有时候MySQL主从同步不一致比较严重的时候,需要手动同步. 然而网上看大很多需要锁表的同步的方法基本如下 1.先对主库锁表 FLUSH TABLES WITH READ LOCK; 2.备份数据 m ...

  9. Notepad++ 自动补全,括号自动完成插件,主题和字体设置

    Notepad++ 自动补全成对符号http://rabbit52.com/2012/devel/notepad-autocomplete-brackets QuickText 和 Zen Codin ...

  10. java的架构流行阶段

    第一阶段:SSM 第二阶段:分布式系统改造,平台化初具规模,各项垂直业务系统搭建上线.产品端极大丰富用户投资.大数据平台研究并使用 第三阶段:SOA治理,使用zookeeper作为注册中心,dubbo ...