在平常的开发中单选按键和下拉按键是非常常用的2个点击事件。首先介绍下单选按键

1:单选按键,单选的主键是radiogroup 这个主键也是很重要的

首先介绍下主键的布局

<?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:id="@+id/myRadioListenerTV"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20px"

android:text="您选择的性别是:"/>

<RadioGroup android:id="@+id/myRadioListenerRG"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checkedButton="@+id/myRadioListenerMale">

<RadioButton android:id="@+id/myRadioListenerMale"

android:text="男"/>

<RadioButton android:id="@+id/myRadioListenerFemale"

android:text="女"/>

</RadioGroup>

</LinearLayout>

在单选RadioGroup 中里面的选择项主要是由RadioButton 组成,这和一般的主键区别不大 radiogroup里面的属性就不一一介绍了。

private RadioGroup radioGroup;

private TextView textView;

private RadioButton btMale,btFemale;

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.myradiolistener);

this.radioGroup=(RadioGroup)super.findViewById(R.id.myRadioListenerRG);

this.textView=(TextView)super.findViewById(R.id.myRadioListenerTV);

this.btMale=(RadioButton)super.findViewById(R.id.myRadioListenerMale);

this.btFemale=(RadioButton)super.findViewById(R.id.myRadioListenerFemale);

this.radioGroup.setOnCheckedChangeListener(new CheckedChanegdListener());

}

public RadioGroup getRadioGroup() {

returnradioGroup;

}

publicvoid setRadioGroup(RadioGroup radioGroup) {

this.radioGroup = radioGroup;

}

public TextView getTextView() {

returntextView;

}

publicvoid setTextView(TextView textView) {

this.textView = textView;

}

public RadioButton getBtMale() {

returnbtMale;

}

publicvoid setBtMale(RadioButton btMale) {

this.btMale = btMale;

}

public RadioButton getBtFemale() {

returnbtFemale;

}

publicvoid setBtFemale(RadioButton btFemale) {

this.btFemale = btFemale;

}

this.radioGroup.setOnCheckedChangeListener(new CheckedChanegdListener());

就是单选按键事件的重要点,我们不推荐在里面直接写内部类的方式

在单选RadioGroup 主要是实现CheckedChanegdListener方法

这个类是实现OnCheckedChangeListener这个接口的

publicvoid onCheckedChanged(RadioGroup group, int checkedId) {

if(group.getId()==R.id.myRadioListenerRG){

String temp="";

myRadioListener my1=(myRadioListener)group.getContext();

if(my1.getBtMale().getId()==checkedId){

temp="您选择的性别是:"+my1.getBtMale().getText();

}elseif(my1.getBtFemale().getId()==checkedId){

temp="您选择的性别是:"+my1.getBtFemale().getText();

}

my1.getTextView().setText(temp);

}

个参数第一个是当前的单选按键,第二个是被选中的radiobutton的id

group.getId()==R.id.myRadioListenerRG 判断当前是对那个单选按键操作

 

1:下拉列表(Spinner)

下拉列表的的主键是Spinner 这个主键和其他的主键有点区别,主要是数据源的来源,可以在代码中实现,也可以用配置的方式

下面的程序是用代码实现数据源的

<?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:id="@+id/mySpinnerClickTV"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="您选择的颜色是:"/>

<Spinner android:id="@+id/mySpinnerClickSpinner"

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

</LinearLayout>

package com.bruce;

import com.bruce.OnClickListener.ItemClickListenerImpl;

import com.bruce.OnClickListener.ItemSelectedListenerImpl;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.TextView;

publicclass mySpinnerClick extends Activity{

private TextView tv;

private Spinner spinner;

private ArrayAdapter<String> adapterColor=null;

private String [] colorArray={"红","黄","蓝"};

public TextView getTv() {

returntv;

}

publicvoid setTv(TextView tv) {

this.tv = tv;

}

public Spinner getSpinner() {

returnspinner;

}

publicvoid setSpinner(Spinner spinner) {

this.spinner = spinner;

}

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.myspinnerclick);

this.tv=(TextView)super.findViewById(R.id.mySpinnerClickTV);

this.spinner=(Spinner)super.findViewById(R.id.mySpinnerClickSpinner);

this.adapterColor=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,colorArray);

this.adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

this.spinner.setAdapter(adapterColor);

this.spinner.setPrompt("您选择的颜色是:");

this.spinner.setOnItemSelectedListener(new ItemSelectedListenerImpl());

}

}

在下拉列表中主要装载数据的是个

this.adapterColor=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,colorArray);

创建一个ArrayAdapter 并把数据和其放在了一起android.R.layout.simple_spinner_item是怎么在主键中展现

 

his.adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);打开下拉列表是用什么的方式

spinner其自身也有很多的方法比如设置下拉列表头this.spinner.setPrompt("您选择的颜色是:");

其选择事件是this.spinner.setOnItemSelectedListener(new ItemSelectedListenerImpl())

个方法

publicvoid onItemSelected(AdapterView<?> parent, View view, int position,

long id) {

System.out.println(parent.getId());

System.out.println(view.getId());

System.out.println(R.id.mySpinnerClickSpinner);

System.out.println(view.getId() == R.id.mySpinnerClickSpinner);

if(parent.getId()==R.id.mySpinnerClickSpinner){

String value = parent.getItemAtPosition(position).toString();

mySpinnerClick my1 = (mySpinnerClick) view.getContext();

my1.getTv().setText("您选择的颜色是:" + value);

}

publicvoid onNothingSelected(AdapterView<?> arg0) {

// TODO Auto-generated method stub

}

个参数,parent当前下拉列表,view当前视图,position下拉列表id,id选择的下拉列表值

onItemSelected是对下拉列表操作的时候做的事

onNothingSelected 不做任何操作的时候做的事

[置顶] Android事件—单选按键和下拉按键的更多相关文章

  1. Android之RecyclerView轻松实现下拉刷新和加载更多

    今天研究了下RecyclerView的滑动事件,特别是下拉刷新和加载更多事件,在现在几乎所有的APP显示数据列表时都用到了.自定义RecyclerView下拉刷新和加载更多听上去很复杂,实际上并不难, ...

  2. Android UI--自定义ListView(实现下拉刷新+加载更多)

    Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...

  3. [置顶] Android源码分析-点击事件派发机制

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/17339857 概述 一直想写篇关于Android事件派发机制的文章,却一直没 ...

  4. [置顶] Android开发笔记(成长轨迹)

    分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API ...

  5. [置顶] Android图片异步加载之Android-Universal-Image-Loader

    将近一个月没有更新博客了,由于这段时间以来准备毕业论文等各种事务缠身,一直没有时间和精力沉下来继续学习和整理一些东西.最近刚刚恢复到正轨,正好这两天看了下Android上关于图片异步加载的开源项目,就 ...

  6. Android事件分发机制(下)

    这篇文章继续讨论Android事件分发机制,首先我们来探讨一下,什么是ViewGroup?它和普通的View有什么区别? 顾名思义,ViewGroup就是一组View的集合,它包含很多的子View和子 ...

  7. 【转】 [置顶] Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在Android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  8. 【朝花夕拾】Android自定义View篇之(七)Android事件分发机制(下)滑动冲突解决方案总结

    前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/11072989.html],谢谢! 前面两篇文章,花了很大篇幅讲解了Android的事件分发机制 ...

  9. [置顶] Android系统五大布局详解Layout

    我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit等 ...

随机推荐

  1. 鸟哥的私房菜上 xpenguins 设备(ubuntu 12.04)

    看了一个暑假linux,我觉得很辛苦啊,要很好地利用linux并不是的easy... 今天装了一下鸟哥课后给的xpenguins软件,就是桌面特性软件.会有非常多企鹅下落,本以为能够非常轻松的搞定.没 ...

  2. win7通过配置怎么样ODBC数据源

    我的系统是win7最终.找了半天也没有找到控制面板上的卷管理工具--数据源.后来在网上搜索后发现win7事实并非如此开放ODBC数据源管理,打开ODBC数据源管理方法是cmd输入命令框odbcad32 ...

  3. smb_精简安装

    yum install samba vim /etc/samba/smb.conf    [修改下自己要发布的目录  .eg : path = /home/iknow] smbpasswd -a ik ...

  4. Alice&#39;s Chance

    id=1698" style="background-color:rgb(51,255,51)">主题链接 意甲冠军: 爱丽丝要拍电影.有n部电影,规定爱丽丝第i部 ...

  5. 深度-first遍历图--邻接表实现

    在这里,邻接表的实现与深度优先遍历图,使用递归. #include<iostream> using namespace std; #define VERTEXNUM 5//结点数 stru ...

  6. Webserver管理系列:3、Windows Update

    微软的操作系统可以使用用户过程中发现了一些漏洞,因此,他们经常发布一些系统补丁.因此,我们需要自己主动安装更新功能后,打开系统. 默认的更新功能未开启自己主动: 开启自己主动更新功能后.Windows ...

  7. 【百度地图API】圣诞节里不会迷路的麋鹿——驾车导航

    原文:[百度地图API]圣诞节里不会迷路的麋鹿--驾车导航 任务描述: 可能大家还不知道,圣诞老人是爱迷路的老爷爷! 今年圣诞节又要到了,圣诞老人又要出来送礼物了.可是,他灰常的迷路呢! 还好,他有一 ...

  8. 圣魔大战3(Castle Fantisia)艾伦希亚战记改动器/秘籍——究极改动大法

    艾伦西亚战记== 艾伦希亚战记,是一个游戏 武器:UltraEdit(金山游侠自带的文件改动器也能够,仅仅是这个专业) 目标: 存档文件(建议先备份)  知识:save00.dat-save19.da ...

  9. 怎样使Android应用程序获得root权限

    Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 写这篇文章前,首先要感谢 Simon_fu ,他的两篇关于 root 权 ...

  10. 使用collectd与visage收集kvm虚拟机性能实时图形

    软件功能: 通过collectd软件来监控收集kvm虚拟机的性能数据,包含cpu,memory.磁盘IO.网络流量等 通过visage软件将收集到的数据绘制图形. 安装: 系统环境:ubuntu12. ...