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人阅读 ...
随机推荐
- C#基础总结之三循环控制-for-数组-乘法表-arraylist
#region 第三天 作业 乘法表 ////正三角 //for (int i = 1; i < 10; i++) //{ // for (int j = 1; j <= i; j++) ...
- ArcGIS提取影像边界
基于ArcGIS有多重办法可以提取影像边界,比如常用的有以下几种方式: a.System Toolboxes --> 3D Analyst Tools --> Conversion --& ...
- Unity3D 敌人AI 和 动画( Animator )系统的实例讲解
在这个实例中,我们要做一些敌人AI的简单实现,其中自动跟随和动画是重点,我们要达到的目标如下: 1.敌人能够自动跟随主角 2.敌人模型一共有四个动作:Idle(空闲) Run(奔跑) Attack(攻 ...
- SQL Server 2012:SQL Server体系结构——一个查询的生命周期(第3部分)(完结)
一个简单的更新查询 现在应该知道只读取数据的查询生命周期,下一步来认定当你需要更新数据时会发生什么.这个部分通过看一个简单的UPDATE查询,修改刚才例子里读取的数据,来回答. 庆幸的是,直到存取方法 ...
- 继续谈论XSS
这篇文章基于上篇谈论XSS ,想说下自己工作过程中遇到的xss的问题. 易出现XSS的场景 1 jsonp 说说jsonp 中也有说过,jsonp其实是很容易出现安全问题的.由于jsonp的callb ...
- 浅谈mysql的两阶段提交协议
前两天和百度的一个同学聊MySQL两阶段提交,当时自信满满的说了一堆,后来发现还是有些问题的理解还是比较模糊,可能是因为时间太久了,忘记了吧.这里再补一下:) 5.3.1事务提交流程 MySQL的事务 ...
- OS X(EI Capitan)常用快捷键整理
刚上班就配了台RMBP,虽然触摸板确实好用,但是对鼠标的支持太差导致无法使用鼠标,而某些功能用触摸板还是不很方便 于是使用快捷键就势在必行了,参考了苹果官方文档和一些其他资料,整理出常用快捷键清单 准 ...
- [Tool] Fiddle2基本使用
Fiddler2已经成网页调试必备的工具. 简述下快捷命令: ?url =statu.=method @host bpafter url bpv method start stop 参考的文章: Fi ...
- Sprint第三个冲刺(第五天)
一.Sprint介绍 实验截图: 任务进度: 二.Sprint周期 看板: 燃尽图:
- Win10安装.net framework 4.0失败提示已是操作系统一部分如何解决
有位用户因为工作需求,所以想在win10系统电脑中安装microsoft .net framework 4.0.可是在安装过程中却失败了,还遇到提示"Microsoft.net framew ...