Android表单UI及相应控件的事件处理




<!--布局-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"> <TextView
android:id="@+id/textviewtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="平时喜欢做什么事情?" /> <CheckBox
android:id="@+id/checkboxall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/textviewtitle"
android:layout_alignTop="@id/textviewtitle"
android:layout_toRightOf="@id/textviewtitle"
android:text="全选" />
<!--内容的CheckBox-->
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewtitle"
android:layout_marginRight="80dp"
android:text="玩游戏" /> <CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewtitle"
android:layout_toRightOf="@+id/checkbox1"
android:text="学习" /> <CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox1"
android:text="敲代码" /> <CheckBox
android:id="@+id/checkbox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox2"
android:layout_toRightOf="@+id/checkbox1"
android:text="跑步" /> <CheckBox
android:id="@+id/checkbox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox3"
android:text="游泳" /> <CheckBox
android:id="@+id/checkbox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox4"
android:layout_toRightOf="@+id/checkbox1"
android:text="睡觉" /> <TextView
android:id="@+id/textviewinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox5"
android:layout_marginTop="20dp"
android:text="已选择:"/>
</RelativeLayout> <!--java代码-->
package com.dc.work3; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast; import java.util.LinkedList;
import java.util.List; /**
* Created by 怪蜀黍 on 2016/11/4.
*/ public class MainActivity2s extends AppCompatActivity {
private CheckBox checkboxall;
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox checkBox3;
private CheckBox checkBox4;
private CheckBox checkBox5;
private CheckBox checkBox6; private TextView textviewinfo;
private List<String> checkedStr; //操作取消一个时,全选取消,这个变量是是否是用户点击
private boolean checkFoUser=true; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2); checkboxall = (CheckBox) findViewById(R.id.checkboxall);
checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
checkBox4 = (CheckBox) findViewById(R.id.checkbox4);
checkBox5 = (CheckBox) findViewById(R.id.checkbox5);
checkBox6 = (CheckBox) findViewById(R.id.checkbox6);
textviewinfo = (TextView) findViewById(R.id.textviewinfo); checkBox1.setOnCheckedChangeListener(changeListener);
checkBox2.setOnCheckedChangeListener(changeListener);
checkBox3.setOnCheckedChangeListener(changeListener);
checkBox4.setOnCheckedChangeListener(changeListener);
checkBox5.setOnCheckedChangeListener(changeListener);
checkBox6.setOnCheckedChangeListener(changeListener);
checkboxall.setOnCheckedChangeListener(changeListener); checkedStr=new LinkedList<>(); }
public CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.checkbox1:
case R.id.checkbox2:
case R.id.checkbox3:
case R.id.checkbox4:
case R.id.checkbox5:
case R.id.checkbox6:
String str=buttonView.getText().toString();
if(isChecked){
checkedStr.add(str);
}else {
checkedStr.remove(str);
}
checkboxall.setOnCheckedChangeListener(null);
if(checkBox1.isChecked()&&checkBox2.isChecked()&&checkBox3.isChecked()&&checkBox4.isChecked()&&checkBox5.isChecked()&&checkBox6.isChecked()){
//表示如果都选中时,把全选按钮也选中
checkboxall.setChecked(true);
}else {
//否则就全选按钮去不选中,但是这样会触发checkboxall的监听,会把所有的都取消掉
checkboxall.setChecked(false);
}
checkboxall.setOnCheckedChangeListener(changeListener);
break;
case R.id.checkboxall:
if(checkFoUser) {
checkBox1.setChecked(isChecked);
checkBox2.setChecked(isChecked);
checkBox3.setChecked(isChecked);
checkBox4.setChecked(isChecked);
checkBox5.setChecked(isChecked);
checkBox6.setChecked(isChecked);
break;
}
}
StringBuffer sb=new StringBuffer();
for(String str:checkedStr){
sb.append(str+",");
}
if(sb.length()>0){
//设置长度为长度-1,去除最后的“,”
sb.setLength(sb.length()-1);
}
textviewinfo.setText("已选择:"+sb.toString());
}
}; }








<!--布局--> <?xml version="1.0" encoding="utf-8"?>
<!--虽然高度是充满,但是他是包裹的-->
<!--android:scrollbars="none"无滚动条-->
<!--android:overScrollMode="never" 滚动条到头的提示模式-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:overScrollMode="never"> <!--注意,ScrolView只能有一个子控件,要想有多个空间,使用Linear Layout包裹-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--横向滚动条-->
<!--因为是横向滚动条,所以不管你宽度设置多少,他始终是充满的-->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"></HorizontalScrollView> <!--进度条-->
<!--android:indeterminate="true" 设置进度条不确定值的进度条-->
<!--android:progressDrawable="@drawable/progress_bar_drawable"我们自定义的进度条-->
<!--高度
android:maxHeight="2dp"
android:minHeight="2dp"-->
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="4"
android:secondaryProgress="6"
android:progressDrawable="@drawable/progress_bar_drawable"
/> <!--可以拖拽的进度条-->
<!-- android:thumb="@mipmap/ic_launcher"自定义图片-->
<!-- android:thumbOffset="100dp" 偏移量,一般不会用到-->
<!--android:thumbTint="#0ff" 着色,不管什么图片还是默认,颜色都改变-->
<!--鼠标悬浮上边这句话时,他的意思是,21版本才可以使用,当前版本是16-->
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumb="@mipmap/ic_launcher"
android:thumbTint="#0ff"
android:progress="50"/> <!-- android:visibility="invisible" 默认隐藏-->
<!--调整文本框的大小尽量使用边来改变-->
<TextView
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="#cdcdcd"
android:text="0%"
android:textSize="22sp"
android:visibility="invisible"
android:layout_gravity="center_horizontal"/> <!--注意使用时,宽度和高度都使用包裹-->
<!--android:stepSize="1"每次增长1-->
<!--android:isIndicator="true" 是否作为指示器,比如QQ等级-->
<!--style="?android:attr/ratingBarStyleSmall"-->
<!--hdpi是mdpi的1.5倍,xmdpi是m的2倍-->
<RatingBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="1.5"
android:stepSize="0.5"
android:progressDrawable="@drawable/rating_bar_drawable"
android:minHeight="21dp"
android:maxHeight="21dp"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="减少"/> <Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加"/>
</LinearLayout> <!--为了看清楚效果,这里我们把高度设为20dp,text设置多点字符,这样就超出了屏幕-->
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
具体功能实现的java代码 package com.dc.work3; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Spinner; /**
* Created by 怪蜀黍 on 2016/11/4.
*/ public class MainActivity_radiobutton extends AppCompatActivity {
private RadioGroup rg; private Spinner spinner; //重写一个参数的处事方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_radiobutton); rg= (RadioGroup) findViewById(R.id.rg1);
rg.setOnCheckedChangeListener(changeLis);
//如果没有任何选中项,返回-1
// rg.getCheckedRadioButtonId();
//选中指定项
// rg.check(R.id.rb1);
//清除选中项
// rg.clearCheck(); ImageView img=(ImageView) findViewById(R.id.iv);
// 设置图片
img.setImageResource(R.mipmap.ic_launcher);
//设置缩放类型
img.setScaleType(ImageView.ScaleType.CENTER); spinner=(Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(itemSelectedListener); }
private AdapterView.OnItemSelectedListener itemSelectedListener =new AdapterView.OnItemSelectedListener() {
@Override//当项选中时 参数1:触发事件的控件 2:选中的视图 3:选中视图的id
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { }
@Override//当没有项选中时
public void onNothingSelected(AdapterView<?> parent) { }
}; private RadioGroup.OnCheckedChangeListener changeLis=new RadioGroup.OnCheckedChangeListener() {
@Override //参数2:选中者的id
public void onCheckedChanged(RadioGroup group, int checkedId) {
//获取选中项的数据
String str=spinner.getSelectedItem().toString();
//获取选中项的下标
int position=spinner.getSelectedItemPosition();
//获取选中项的id
long id= spinner.getSelectedItemId();
Log.e("aaaa",str+"========="+position+"========="+id);
}
};
}


Android表单UI及相应控件的事件处理的更多相关文章
- Bootstrap3 表单-被支持的控件:文本域
支持多行文本的表单控件.可根据需要改变 rows 属性. <textarea class="form-control" rows="3"></ ...
- Bootstrap3 表单-被支持的控件:输入框
输入框包括大部分表单控件.文本输入域控件,还支持所有 HTML5 类型的输入控件: text.password.datetime.datetime-local.date.month.time.week ...
- html表单的各种输入控件
表单的输入控件主要是input和select.其中input可以是多种类型,通过type属性来进行定义,type可以取值是text,radio,checkbox,password,submit,res ...
- BarTender 2016表单中的“秤显示”控件
BarTender 2016中的表单是一个非常实用的工具,它可以实现数据输出提示,查询提示和同一表单的记录选择.这些都离开可供添加的控件,“秤显示”控件也是我们打印尝尝需要涉及的,今天我们就来看看什么 ...
- Appium Android Bootstrap源码分析之控件AndroidElement
通过上一篇文章<Appium Android Bootstrap源码分析之简介>我们对bootstrap的定义以及其在appium和uiautomator处于一个什么样的位置有了一个初步的 ...
- iOS开发UI篇—UIScrollView控件介绍
iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...
- Android support library支持包常用控件介绍(二)
谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...
- Android开发技巧——自定义控件之组合控件
Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...
- (转载) Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框 标签: Android清除功能EditText仿IOS的输入框 2013-09-04 17:33 70865人阅读 ...
随机推荐
- ArcGIS如何将表连接到空间数据上
当我们有一些空间数据和一些业务数据(表),希望把业务数据和空间数据连接起来时,可以采用ArcGIS Desktop进行操作.本文将介绍如何在ArcGIS Destop中将表和空间数据关联起来. Arc ...
- Android JNI开发生成.h头文件问题
在JNI开发中,首先要将建立的anroid类编译成.h文件,编译用到命令javah,由于第一次用,以前对java的编译过程也不怎么了解,所以走了好多弯路,网络没有对这一步的详细介绍,这里讲一下: 通过 ...
- 《微信小程序七日谈》- 第三天:玩转Page组件的生命周期
<微信小程序七日谈>系列文章: 第一天:人生若只如初见: 第二天:你可能要抛弃原来的响应式开发思维: 第三天:玩转Page组件的生命周期: 第四天:页面路径最多五层?导航可以这么玩 前两篇 ...
- ASP.NET跨服务器上传文件的相关解决方案
第一种:通过FTP来上传文件 首先,在另外一台服务器上设置好FTP服务,并创建好允许上传的用户和密码,然后,在ASP.NET里就可以直接将文件上传到这台 FTP 服务器上了.代码如下: <%@ ...
- [C#] CSharp 基本语法
CSharp Language Specification 一.基础 1.规范: 除常量外,所有变量用驼峰命名方式,其它用帕斯卡命名方式. 2.编译: 首先由csc.exe将cs文件编译成MSIL.当 ...
- 移动端前端常见的触摸相关事件touch、tap、swipe等整理
前端的很多事件在PC端和浏览器端可公用,但有些事件却只在移动端产生,如触摸相关的事件 本文整理了移动端常见的一些事件,包括原生支持的click.touch.tap.swipe事件,也有定义型的gest ...
- jquery选择器(原创)<二>
jquery选择器,选择接着学: 前面学习了基本选择器中的CSS选择器,现在学层级选择器: 1.子元素选择器 子元素选择器,用于在给定的父元素下,查找这个父元素下面的所有的子元素,语法格式,如下: $ ...
- 在ASP.NET MVC环境中使用加密与解密
在.NET Framework 4.5的NET框架中,在程序中加密与解密很方便.现在均学习ASP.NET MVC程序了,因此Insus.NET也在此写个学习的例子.在需要时可以参考与查阅. 写一个Ut ...
- Python入门笔记(9):元组
一.元组特性 1.类似列表,但不可变类型,正因如此,它可以做一个字典的key2.当处理一组对象时,这个组默认是元组类型(老写错"元祖")3.所有的多对象,逗号分隔的,没有明确用符号 ...
- 【C#进阶系列】06 类型和成员基础
这些东西是基础中的基础,基本上是本书都会讲这个.但是很多东西到处都有,所以只捡了以下的这些写下来. 关于类型的可见性和可访问性 也就是public,internal这种东西,但是还是有个东西要提一下, ...