版本:Android4.3 API18 学习整理:liuxinming

TextView

概述

TextView直接继承了View(EditText、Button两个UI组件类的父类)
TextView还派生了一个CheckedTextView,CheckedTextView增加了一个checked状态
可以通过setChecked(boolean)和isChecked()方法来改变、访问该组件的checked状态
还可以通过setCheckMarkDrawable()方法来设置它的勾选图标。

TextView类及子类的类图



XML属性

TextView提供了大量的XMl属性,这些XML属性大部分也适用于它的子类(EditText,Button等)
具体XML属性参考


EditText

概述

          EditText 与 TextView非常相似,它与TextView公用了绝大部分XML属性和方法。
          和TextView区别:EditText可以接受用户输入

子类

         从上面类图关系可以看出,EditText派生了两个子类
        1、AutoCompleteTextView :带有自动完成功能的EditText,该类通常要与Adapter结合使用。
        2、ExtractEditText :它并不是UI组件,而是EditText组件的底层服务类,负责提供全屏输入法支持。

奉上测试案例:用户输入界面

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="16sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写登录账户"
android:selectAllOnFocus="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="16sp"
/>
<!-- android:inputType="numberPassword" 表明只能接受数字密码 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
/>
</TableRow>
<TableRow >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="16sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
</TableRow>
<TableRow >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="生日:"
android:textSize="16sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="date"
/>
</TableRow>
<TableRow >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textSize="16sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写您的电话号码"
android:selectAllOnFocus="true"
android:inputType="phone"
/>
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
/>
</TableLayout>

调试效果:




Button

概述

            根据上述类图关系可以看出Button继承了TextView类,它主要是在UI界面上生成一个按钮,该按钮可以提供用户单击
            当用户单击时,按钮会触发一个onClick事件
            可以通过android:background属性为按钮增加背景颜色和图片

RadioButton,CheckBox

RadioButton CheckBox ToggleButton(状态开关按钮) Switch(开关) 继承了Button类,因此可以使用Button支持的各种属性和方法
RadioButton CheckBox 它们多了一个可选中的功能,因此都可额外指定一个android:checked属性,用于指定初始时是否被选中
RadioButton 只能选中一个 类似 HTML 中的 <input type="radio" 单选框表单
CheckBox 可以选中多个 类似 HTML 中的 <input type="checkbox" 复选框

单选按钮、复选框实例

XML布局
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="16sp"
/>
<!-- 这里定义一组单选按钮 -->
<RadioGroup android:id="@+id/button01"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
>
<!-- 定义两个单选按钮 -->
<RadioButton android:id="@+id/boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"
/>
<RadioButton android:id="@+id/gril"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
/>
</RadioGroup>
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢的美女"
android:textSize="16sp"
/>
<!-- 定义一个垂直的线性布局 -->
<LinearLayout android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- 定义三个复选框 -->
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大S"
android:checked="true"
/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小S"
/>
</LinearLayout>
</TableRow>
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableLayout>

java代码

package com.example.edittext;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView; public class MainActivity extends Activity {
RadioGroup button01;
TextView show; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取界面上button01,show两个组件对象
button01 = (RadioGroup) findViewById(R.id.button01);
show = (TextView) findViewById(R.id.show);
//为RadioGroup组件的OnCheck事件绑定事件监听器
button01.setOnCheckedChangeListener(new OnCheckedChangeListener()
{ @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//根据用户勾选的单选按钮来动态改变tip字符串的值
String tip = checkedId == R.id.boy ? "男人":"女人";
//修改 show 组件中的文本
show.setText(tip);
} });
} @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;
} }

效果




ToggleButton,Switch

ToggleButton,Switch通常用于切换程序中的某种状态

AnalogClock,DigitalClock

DigitalClock本身继承了TextView---也就是说它本身就是文本框,只是它里面显示的内容总是当前时间
AnalogClock继承了View组件,它重写了View的OnDraw方法,它会在View上绘制模拟时钟
区别:
DigitalClock显示数字时钟,可以显示当前的秒数
AnalogClock显示模拟时钟,不会显示当前秒数

Chronometer

Chronometer(计时器),继承TextView,显示一段文本,但它不显示当前时间,显示从某个起始时间开始,一共过去了多长时间
提供了一个android:format 属性,用于指定计时器的计时格式。

支持常用方法

1、setBase(long base) :设置计时器的起始时间
2、setFormat(String format):设置显示时间格式
3、start():开始计时
4、stop():停止计时
5、setOnChronometerTickListener(Chronometer.OnChronometerTickListenerlistener):为计时器绑定事件监听器,当计时改变时触发该监听器。

具体用法参考官方API:

欢迎Android , php 同学加群 QQ :224784042 交流
学习的结果,依靠于每天的持之以恒!!不是特忙,特加班情况,会每天更新一篇Android学习笔记,欢迎大家互相交流,学习,共同进步。
偶菜鸟一枚!!!!!!
晚安!


Android开发8:UI组件TextView,EditText,Button的更多相关文章

  1. Android开发 ---基本UI组件3:单选按钮、多选按钮、下拉列表、提交按钮、重置按钮、取消按钮

    Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个用户注册按钮 <?xml version="1.0" encoding=&q ...

  2. Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制

    Android开发 ---基本UI组件4 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding=" ...

  3. Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关

    Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...

  4. Android经常使用UI组件 - TextView

    TextView是Android里面用的最多的UI组件,一般使用在须要显示一些信息的时候,其不能输入,仅仅能初始设定或者在程序中改动. 实例:TextViewDemo 执行效果: 代码清单: 布局文件 ...

  5. Android开发 ---基本UI组件6 :只定义一个listView组件,然后通过BaseAdapter适配器根据数据的多少自行添加多个ListView显示数据

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  6. Android开发---基本UI组件1:自动拨电话,自动上网,输入框不换行、只输数字、只输文本、只输密码

    1.activity_main.xml 描述:构建一个按钮 <?xml version="1.0" encoding="utf-8"?> <L ...

  7. Android开发 ---基本UI组件7 :分页功能、适配器、滚动条监听事件

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  8. Android开发 ---基本UI组件8:九宫格布局、setOnItemClickListener()项被选中监听事件

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  9. Android开发 ---基本UI组件5:监听下拉选项,动态绑定下拉选项、全选/反选,取多选按钮的值,长按事件,长按删除,适配器的使用,提示查询数据,activity控制多按钮

    效果图: 效果描述: 1.当点击 1 按钮后,进入选择城市的页面,会监听到你选中的城市名称:动态为Spinner绑定数据 2.当点击 2 按钮后,进入自动查询数据页面,只要输入首字母,就会动态查找以该 ...

随机推荐

  1. lostash 正则

     (?:\s+?)  0个或者多个空格

  2. hdu 4497 GCD and LCM 质因素分解+排列组合or容斥原理

    //昨天把一个i写成1了 然后挂了一下午 首先进行质因数分解g=a1^b1+a2^b2...... l=a1^b1'+a2^b2'.......,然后判断两种不可行情况:1,g的分解式中有l的分解式中 ...

  3. hdu 1253 胜利大逃亡_三维

    第一次做三维的题,这题跑g++超时了,c++过了. #include<iostream> #include<cstdio> #include<queue> usin ...

  4. 从ora10g 刷数据到 8I基本操作步骤

    从ora10g 刷数据到 8I基本操作步骤 master :oracle 10g snapshot site: oralce 8i 在oracle 8i 中物化视图称为快照,oracle 8i建快照的 ...

  5. mongodb导出命令

    ./mongoexport -d admin -c col -o col.json 找到了 导出所有数据库的 http://www.jb51.net/article/52498.htm

  6. 【ThinkPHP学习】ThinkPHP自己主动转义存储富文本编辑器内容导致读取出错

    RT. ThinkPHP的conf文件里的Convention.php有一个配置选项 'DEFAULT_FILTER' => 'htmlspecialchars', // 默认參数过滤方法 用于 ...

  7. Flash Recovery Area 的备份

    Flash Recovery Area 的备份 备份命令是Flash recovery Area,该命令是Oracle 10g以后才有的.10g引进了flash recovery area,同时在rm ...

  8. uva 10655 - Contemplation! Algebra

    ---恢复内容开始--- Given the value of a+b and ab you will have to find the value of an+bn 给出a+b和a*b的值,再给出n ...

  9. 总结一下.net framework适合装在哪些系统中

    最近在客户这部署,环境是windows server2003 service pack2.因为vs2012开发环境是.net framework4.5,所以发布iis的时候,尝试在sever2003上 ...

  10. EXT属性

    Extjs & Ext.Net 弹出整个浏览器对话框的方法 top.Ext.Msg.alert("值"); top.Ext.Msg.confirm("值" ...