一、单选框

单选框实例程序:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
 
    <!-- checkedButton 表示默认为woman的意思,它能够自动识别下面id为woman的控件 -->
  <RadioGroup 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:checkedButton="@+id/woman"
    android:id="@+id/sex"
      >
      
      <RadioButton
          android:text="@string/man"
          android:id="@+id/man"
          ></RadioButton>
    <RadioButton
        android:text="@string/woman"
        android:id="@id/woman"
         />
      </RadioGroup>
 
</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; }

 

二、复选框

 

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

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

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

.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文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="colors">
<item>red</item>
<item>orange</item>
<item>yellow</item>
<item>green</item>
<item>blue</item>
<item>violet</item>
</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文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/colors"
></TextView> <Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/red"
android:entries="@array/colors"
>
</Spinner>
</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. 15个变态的Google面试题以及答案

    在当前经济形势不景气的情况下,谷歌招聘新员工是一件令人振奋的事,特别是对那些在当前金融风暴中渴望找到安全港的年轻经理们和软件开发商们来说是个好消息. 不过,也不要高兴太早,谷歌在招聘新员工时,更加青睐 ...

  2. BZOJ2132 圈地计划 【最小割】

    题目 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解, 这块土地是一块矩形的区域,可以纵横划分 ...

  3. svn没有"对号"等符号

    [问题描述]调整svn建立好了服务端.安装客户端也检出成功了.但是就是没有对号符号. [解决方案]右键菜单,设置,里面有“图标覆盖”这个选项,把你的文件夹加入进去,然后注销windows用户重新登陆

  4. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---50

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  5. [MySQL] Group Commit理解

    简单的方法理解MySQL Group Commit原理 一个摆渡将乘客从A点传输到B点 MySQL 5.0 行为 在MySQL 5.0中,摆渡会在A点按顺序搭载乘客,并且传送到B点.A点和B点的来回行 ...

  6. Syslog协议日志格式翻译

    通用日志格式规范(参考 RFC5424 Syslog协议) 下面是RFC5424 Syslog协议关于信息格式的定义. Syslog信息的格式定义 # 一条信息的构成 SYSLOG-MSG = HEA ...

  7. php基础 gd图像生成、缩放、logo水印和验证码

    gd库是php最常用的图片处理库之一(另外一个是imagemagick),可以生成图片.验证码.水印.缩略图等等. 图像生成 <?php /* 用windows画图板画图 1.新建空白画布(指定 ...

  8. CCCC L2-003. 月饼[贪心/类似hdu贪心老鼠]

    L2-003. 月饼 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不 ...

  9. Loj #6244. 七选五

    link: https://loj.ac/problem/6244 让我们设B=N-K,再设F(P)为从1~P+B中选P个数(有序的),且这P个数是一个错排(即不存在一个i使得 i=Ai ,其中A是选 ...

  10. 2016集训测试赛(十八)Problem C: 集串雷 既分数规划学习笔记

    Solution 分数规划经典题. 话说我怎么老是忘记分数规划怎么做呀... 所以这里就大概写一下分数规划咯: 分数规划解决的是这样一类问题: 有\(a_1, a_2 ... a_n\)和\(b_1, ...