目录
- 常用控件之textview
- 常用控件之button
- 常用控件之edittext
- 常用控件之imageview
- 常用控件之CheckBox
- 常用控件之radiobutton
- 常用控件之Toast
- 常用控件之log
- 常用控件之listview
- 常用控件之GridView
- 常用控件之ContextMenu
- 常用控件之加载进度条ProgressBar
- 常用控件之可拖动进度条SeekBar
- 常用控件之Notification
- 下拉列表spinner的使用
- 标题栏actionbar使用导航
- 二级可折叠列表
- alertdialog非阻塞式弹窗
- PopupWindow阻塞式弹窗
- recycleview进阶列表使用
- 卡片效果cardview
- 横向滑动页面viewpager
- 滑动tab控件tablayout
- 加载网页的webview
- 碎片化布局fragment
- 开关按钮ToggleButton使用
- 时钟组件AnalogClock、DigitalClock使用
- 计时器Chronometer使用
- 图片切换器ImageSwitcher使用
- 日历选择器calendarView使用
- 时间选择器datepicker使用
常用控件之textview
显示文字的控件
设置字体颜色
android:textColor="#000"
表示文字的颜色。
【提示】颜色可以随便写一个“#000”形式的属性值,再通过点击左边显示行号旁边的颜色显示方块,弹出来颜色选择器对颜色进行选择。
当设置的颜色为系统提供的Color资源内的颜色时,如“@color/colorAccent”,将不能通过此方法改变颜色值。(点击无效)
设置字体大小
android:textSize="20sp"
表示文字的大小。建议字体单位为sp,默认情况下,1sp和1dp的大小是一样的。【提示】在Android手机中是可以通过系统设置调整字体的大小,sp会随着手机设置字体的大小变化而变化,而dp不会变。(某些特殊的情况下会用dp作为单位表示字体大小
显示html文本
//TextVie显示html 字体颜色为红色 需要注意不支持html标签的style属性
String html="<font color ='red'>TextVie显示html 字体颜色为红色</font><br/>";
tv3.setText(Html.fromHtml(html));
表示TextView中的文字相对于TextView的对齐方式。
设置背景颜色
android:background="#ccc"
表示TextView的背景颜色。(对于颜色有关可以参考前期随笔:Android颜色配置器 )
为TextView中的文字设置链接
android:autoLink="web"
none:表示不进行任何匹配,默认。
web:表示匹配Web Url:http://www.baidu.com会成为可单击跳转的超链接。
email:表示匹配邮件地址:邮件地址为584224xxx@163.com会成为可单击的超链接
phone:表示匹配电话号码:点击号码10086会跳到拨号界面
map:表示匹配地图地址
all表示匹配所有
代码
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://www.baidu.com"
android:background="#ccc"
android:gravity="center"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:autoLink="web"/>
效果
提示
①文本中要写链接的完整路径“http:···“
②文字中除了链接的地址也可以加上其他文字。
③文本中不能出现”&“符号会提示Unescaped & or nonterminated character/entity reference
(具体也不是很懂,大概是因为很多特殊符号都是用”&“开头表示的吧,如”&)
行数属性
设置行数
android:lines="2" 不管多大都显示两行
android:ellipsize="end"
android:maxLines="2" 超过两行只显示两行
android:ellipsize="end"
代码
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="三月,醉一场青春的流年。慢步在三月的春光里,走走停停,看花开嫣然,看春雨绵绵,感受春风拂面,春天,就是青春的流年。
青春,是人生中最美的风景。青春,是一场花开的遇见;青春,是一场痛并快乐着的旅行;青春,
是一场轰轰烈烈的比赛;青春,是一场鲜衣奴马的争荣岁月;青春,是一场风花雪月的光阴。"
android:maxLines="2"
android:ellipsize="end"/>
效果
提示
①line和maxLine属性要结合ellipsize=“end”使用,多的部分用省略号显示
②ellipsize省略号显示的位置。start开头,middle中间(貌似只有end对应多行,其他的针对单行)
TextView内容只显示单行。对于部分显示···
android:singleLine="true"
【属性值】true或者false。如果设置为true为单行输入。
代码
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="三月,醉一场青春的流年。慢步在三月的春光里,走走停停,看花开嫣然,
看春雨绵绵,感受春风拂面,春天,就是青春的流年。青春,是人生中最美的风景。青春,是一场花开的遇见;青春,是一场痛并快乐着的旅行;
青春,是一场轰轰烈烈的比赛;青春,是一场鲜衣奴马的争荣岁月;青春,是一场风花雪月的光阴。"
android:singleLine="true"
/>
效果
提示
①文字过长建议用资源文件储存。/res/values/string
②也可以用line,maxLine属性设置只有单行,但是没有后面省略号。
省略号设置
省略号在中间显示
android:ellipsize="middle"
android:lines="1"
代码
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="三月,醉一场青春的流年。慢步在三月的春光里,走走停停,看花开嫣然,看春雨绵绵,感受春风拂面,
春天,就是青春的流年。青春,是人生中最美的风景。青春,是一场花开的遇见;
青春,是一场痛并快乐着的旅行;青春,是一场轰轰烈烈的比赛;
青春,是一场鲜衣奴马的争荣岁月;青春,是一场风花雪月的光阴。"
android:maxLines="1"
android:ellipsize="middle"/>
效果
提示:设置middle值时,只针对单行有效,如果设置多行,将没有效果(亲测)
一行只能显示多少字符,其余省略号代替
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxEms="5"
android:singleLine="true"
android:text="一行最多只能显示5个字符,多余的...表示" />
文本可选择复制
android:textIsSelectable="true"
【属性值】true或者false。如果设置为true为可选择复制,默认为false。
设置自定义字体
①添加assets文件夹,把字体文件放到文件夹中,在Project视图下查看
②java代码设置字体
把字体文件放进assets中的fonts文件夹中
定义一个Typeface,后面把assets下面的完整路径填进去
设置字体加粗倾斜
textStyle:三种选择
normal(无任何效果,常规)、bold(文字加粗、italic(字体倾斜)
设置字体设置对齐方式
常用的对齐方式有:
center:居中对齐,位于容器横向和纵向的中央
left:向左对齐,位于容器左边
right:向右对齐,位于容器右边
bottom:向底对齐,位于容器底部
top:向顶对齐,位于容器顶部
center_vertical:位置置于容器的纵向中央部分
center_horizontal:位置置于容器的横向中央部分
使用两个值叠加定位,用“|”进行分割,此下为right|top的效果
设置文字阴影效果
shadowColor属性用来设置阴影颜色,颜色可以再colors.xml中预先配置;
shadowRadius属性设置模糊程度,数值越大,阴影就越模糊;
shadowDx属性设置在水平方向上的偏移量,数值越大,则阴影越向右移动;
shadowDy属性设置在垂直方向上的偏移量,数值越大,则阴影越向下移动。
在字体旁边设置图片
drawableTop(drawableLeft、drawableRight、drawableBottom):在TextView的上(左、右、下方放置一个drawable(图片等))
其他几个也一样的用法,无非是放置的位置不同罢了。
android:drawablePadding:是设置text与drawable(图片等)的间隔,一般都与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用。
设置字符串中个别字样式
// 1. TextView的样式类Span的使用详解
SpannableString spannableString = new SpannableString("TextView的样式类Span的使用详解") ;
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED);
//0到10的字符设置红色背景
spannableString.setSpan(backgroundColorSpan, 0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv7.setText(spannableString);
设置字符创中个别字体可点击
// 2.ClickableSpan: 点击事件相关的Span
// 注意:在使用ClickableSpan的时候,在单击链接时凡是有要执行的动作,都必须设置MovementMethod对象。
SpannableString spannableClickString = new SpannableString("TextView设置点击事件Span") ;
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this,"TextView设置点击事件Span", Toast.LENGTH_LONG).show();
}
};
spannableClickString.setSpan(clickableSpan,11,15, Spannable.SPAN_EXCLUSIVE_INCLUSIVE) ;
tv8.setMovementMethod(LinkMovementMethod.getInstance());
tv8.setText(spannableClickString);
字体旁图片大小无法修改问题
关键代码
drawable.setBounds(left, top, right, bottom);
textView.setCompoundDrawables(leftDrawable,rightDrawable,topDrawable,bottomDrawable);
用法
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<ImageView
android:id="@+id/iv"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:text="设置"
android:textSize="20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/usercenter_grey_arrow"/>
</RelativeLayout>
<TextImageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:drawableLeft="@mipmap/ic_launcher"
android:drawablePadding="20dp"
android:drawableRight="@mipmap/usercenter_grey_arrow"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="设置"
android:textSize="20dp"
app:drawableLeftHeight="30dp"
app:drawableLeftWidth="30dp"
app:drawableRightHeight="20dp"
/>
<TextImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="@mipmap/ic_launcher"
android:drawableLeft="@mipmap/ic_launcher"
android:drawableRight="@mipmap/ic_launcher"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center"
android:text="你还可以这样"
app:drawableBottomHeight="100dp"
app:drawableBottomWidth="100dp"
app:drawableLeftHeight="15dp"
app:drawableLeftWidth="15dp"
app:drawableRightHeight="50dp"
app:drawableRightWidth="50dp"
app:drawableTopHeight="30dp"
app:drawableTopWidth="30dp"
/>
</LinearLayout>
属性值设定,在values的styles下添加
<declare-styleable name="TextImageView">
<attr name="drawableLeftWidth" format="dimension"/>
<attr name="drawableLeftHeight" format="dimension"/>
<attr name="drawableTopWidth" format="dimension"/>
<attr name="drawableTopHeight" format="dimension"/>
<attr name="drawableRightWidth" format="dimension"/>
<attr name="drawableRightHeight" format="dimension"/>
<attr name="drawableBottomWidth" format="dimension"/>
<attr name="drawableBottomHeight" format="dimension"/>
</declare-styleable>
自定义view源码
/**
* @author Dangzt
* @version 1.0
* @since 2017/5/27
*/
public class TextImageView extends android.support.v7.widget.AppCompatTextView {
private int mLeftWidth;
private int mLeftHeight;
private int mTopWidth;
private int mTopHeight;
private int mRightWidth;
private int mRightHeight;
private int mBottomWidth;
private int mBottomHeight;
public TextImageView(Context context) {
super(context);
}
public TextImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public TextImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextImageView);
mLeftWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableLeftWidth, 0);
mLeftHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableLeftHeight, 0);
mTopWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableTopWidth, 0);
mTopHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableTopHeight, 0);
mRightWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableRightWidth, 0);
mRightHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableRightHeight, 0);
mBottomWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableBottomWidth, 0);
mBottomHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableBottomHeight, 0);
typedArray.recycle();
setDrawablesSize();
}
private void setDrawablesSize() {
Drawable[] compoundDrawables = getCompoundDrawables();
Timber.d("Drawable size" + compoundDrawables.length + "value = " + compoundDrawables + "");
for (int i = 0; i < compoundDrawables.length; i++) {
switch (i) {
case 0:
setDrawableBounds(compoundDrawables[0], mLeftWidth, mLeftHeight);
break;
case 1:
setDrawableBounds(compoundDrawables[1], mTopWidth, mTopHeight);
break;
case 2:
setDrawableBounds(compoundDrawables[2], mRightWidth, mRightHeight);
break;
case 3:
setDrawableBounds(compoundDrawables[3], mBottomWidth, mBottomHeight);
break;
default:
break;
}
}
setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
}
private void setDrawableBounds(Drawable drawable, int width, int height) {
if (drawable != null) {
double scale = ((double) drawable.getIntrinsicHeight()) / ((double) drawable.getIntrinsicWidth());
Timber.d("width/height" + drawable.getIntrinsicWidth() + "," + drawable.getIntrinsicHeight());
Timber.d("scale = %s", scale);
drawable.setBounds(0, 0, width, height);
Rect bounds = drawable.getBounds();
//高宽只给一个值时,自适应
if (bounds.right != 0 || bounds.bottom != 0) {
Timber.d("before" + bounds.right + "," + bounds.bottom);
if (bounds.right == 0) {
bounds.right = (int) (bounds.bottom / scale);
drawable.setBounds(bounds);
}
if (bounds.bottom == 0) {
bounds.bottom = (int) (bounds.right * scale);
drawable.setBounds(bounds);
}
Timber.d("after" + bounds.right + "," + bounds.bottom);
}
}
}
}
动态修改strings资源字符串
设置点击改变背景颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/textview_click_background" android:state_focused="true"/>
<item android:drawable="@color/textview_click_background" android:state_pressed="true"/>
<item android:drawable="@color/textview_default"/>
</selector>
<TextView
android:id="@+id/tv9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/selector_textview"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="TextView设置点击背景" />
//必须要给TextView加上点击事件点击之后才能改变背景颜色
findViewById(R.id.tv9).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"点击了TextView9", Toast.LENGTH_LONG).show();
}
});
给textview设置部分边框
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This is the main color -->
<item>
<shape>
<!-- 边框颜色 -->
<solid android:color="#00FF00"/>
</shape>
</item>
<!-- 给View的上 左 右设置8dp的边框 -->
<item android:top="8dp" android:left="8dp" android:right="8dp" >
<shape>
<!-- View填充颜色 -->
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
设置跑马灯效果
<!-- 跑马灯效果 -->
<TextView
android:id="@+id/tv12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="跑马灯效果 学好android开发就关注公众号 android开发666 经常推送原创文章"/>
常用控件之button
设置按钮的控件
1.文字大小、颜色
2.自定义背景形状
新建两个自定义的shape:
一、Shape的属性(rectangle、oval、line、ring )
分别为矩形、椭圆、线性、环形
二、shape的子属性(corners、gradient、padding、size、solid、stroke)
1.Corners:标签是用来字义圆角的
2、solid:solid用以指定内部填充色
3.gradient用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式,它的优先级高于solid。
4.stroke:这是描边属性,可以定义描边的宽度,颜色,虚实线等
5.size和padding: size:是用来定义图形的大小的,padding:用来定义内部边距
应用自定义的Shape:
3.自定义按压效果
在drawanble新建一个selector的根布局:
常见属性介绍:
android:state_selected 选中
android:state_focused 获得焦点
android:state_pressed 点击
android:state_enabled 设置是否响应事件,指所有事件
自定义一个点击的按钮:
在布局文件里:
运行应用程序,看效果:
按下效果:
4.防止连点的两种方法
public abstract class NoDoubleListener implements View.OnClickListener {
public static final int MIN_CLICK_DELAY_TIME = 3000;
private static final String TAG ="NoDoubleListener" ;
private long lastClickTime = 0;
@Override
public void onClick(View v) {
long currentTime = Calendar.getInstance().getTimeInMillis();
if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
lastClickTime = currentTime;
onNoDoubleClick(v);
}else {
Log.w(TAG,"点击过快");
}
}
protected abstract void onNoDoubleClick(View v);
}
public class ClickUtil {
public static final int DELAY = 1000;
private static long lastClickTime = 0;
public static boolean isNotFastClick() {
long currentTime = System.currentTimeMillis();
if (currentTime - lastClickTime > DELAY) {
lastClickTime = currentTime;
return true;
} else {
return false;
}
}
}
常用控件之edittext
设置输入框的控件
1. 基本属性
android:autoLink//设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)。
android:autoText//如果设置,将自动执行输入值的拼写纠正。
android:bufferType//指定getText()方式取得的文本类别。
android:capitalize//设置英文字母大写类型。
android:cursorVisible//设定光标为显示/隐藏,默认显示。
android:digits//设置允许输入哪些字符。如“1234567890.+-*/% ()”
android:drawableBottom//在text的下方输出一个drawable。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。
android:drawableLeft//在text的左边输出一个drawable。
android:drawablePadding//设置text与drawable的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。
android:drawableRight//在text的右边输出一个drawable。
android:drawableTop//在text的正上方输出一个drawable。
android:editable//设置是否可编辑。
android:editorExtras//设置文本的额外的输入数据。
android:ellipsize//设置当文字过长时,该控件该如何显示。有如下值设置:“start”—?省略号显示在开头;“end”——省略号显示在结尾;“middle”—-省略号显示在中间;“marquee” ——以跑马灯的方式显示(动画横向移动)
android:freezesText//设置保存文本的内容以及光标的位置。
android:gravity//设置文本位置,如设置成“center”,文本将居中显示。
android:hintText//为空时显示的文字提示信息。
android:textColorHint//设置提示信息的颜色。
android:imeOptions//附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。
android:imeActionId//设置IME动作ID。
android:imeActionLabel//设置IME动作标签。
android:includeFontPadding//设置文本是否包含顶部和底部额外空白,默认为true。
android:inputMethod//为文本指定输入法,需要完全限定名(完整的包名)。
android:inputType//设置文本的类型,用于帮助输入法显示合适的键盘类型。
android:linksClickable//设置链接是否点击连接,即使设置了autoLink。
android:marqueeRepeatLimit//在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。
android:ems//设置TextView的宽度为N个字符的宽度。
android:maxEms//设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。
android:minEms//设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。
android:maxLength//限制显示的文本长度,超出部分不显示。
android:lines//设置文本的行数,设置两行就显示两行,即使第二行没有数据。
android:maxLines//设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。
android:minLines//设置文本的最小行数,与lines类似。
android:lineSpacingExtra//设置行间距。
android:lineSpacingMultiplier//设置行间距的倍数。如“1.2”
android:numeric//如果设置“true”,输入法自动变为数字输入键盘,同时仅允许0-9的数字输入。
android:password//以小点“.”显示文本
android:phoneNumber//设置为电话号码的输入方式。
android:privateImeOptions//设置输入法选项。
android:scrollHorizontally//设置文本超出TextView的宽度的情况下,是否出现横拉条。
android:selectAllOnFocus//如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。
android:shadowColor//指定文本阴影的颜色,需要与shadowRadius一起使用。
android:shadowDx//设置阴影横向坐标开始位置。
android:shadowDy//设置阴影纵向坐标开始位置。
android:shadowRadius//设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。
android:singleLine//设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。
android:textStyle//设置字形;bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2;可以设置一个或多个,用“|”隔开。
android:typeface//设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3
android:maxHeight//设置文本区域的最大高度。
android:minHeight//设置文本区域的最小高度。
android:maxWidth//设置文本区域的最大宽度。
android:minWidth//设置文本区域的最小宽度。
android:textAppearance//设置文字外观。如"?android:attr/textAppearanceLargeInverse" 这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。
2. 限制输入内容
通过虚拟键盘来控制输入框中内容的类型,可以在xml文件中加入android:inputType;
inputType 有下面这些值:
date //日期键盘
datetime //日期时间格式
none //无格式
number //数字格式
numberDecimal //可以带小数点的浮点格式
numberPassword //数字密码格式
numberSigned //有符号数字格式
phone //拨号键盘
text //文本格式
textAutoComplete //自动完成
textAutoCorrect //纠正单词的拼写错误
textCapCharacters //所有字符大写
textCapSentences //仅第一个字母大写
textCapWords //单词首字母大写
textEmailAddress //电子邮件地址格式
textEmailSubject //邮件主题格式
textFilter //文本筛选格式
textImeMultiLine //输入法多行
textLongMessage //长消息格式
textMultiLine //多行输入
textNoSuggestions //不提示
textPassword //密码格式
textPersonName //人名格式
textPhonetic //拼音输入格式
textPostalAddress //邮政格式
textShortMessage //短消息格式
textUri //URI格式
textVisiblePassword //密码可见格式
textWebEditText //作为网页表单的文本格式
textWebEmailAddress //作为网页表单的电子邮件地址格式
textWebPassword //作为网页表单的密码格式
time //时间键盘
3. 改变输入法中回车按钮的显示内容
根据编辑框输入完成后要执行的业务逻辑指定软键盘右下角Action按钮的样式和行为,可以在xml文件中加入android:imeOptions;
imeOptions 有下面一些常用值:
actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED;
actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE;
actionGo执行 “开始” ,对应常量EditorInfo.IME_ACTION_GO;
actionSearch 执行 “搜索”,对应常量EditorInfo.IME_ACTION_SEARCH;
actionSend执行 “发送”,对应常量EditorInfo.IME_ACTION_SEND;
actionNext 执行 “下一个”,对应常量EditorInfo.IME_ACTION_NEXT;
actionPrevious 执行 “上一个”,对应常量IME_ACTION_PREVIOUS;
actionDone 执行 “完成”,对应常量EditorInfo.IME_ACTION_DONE。
4. 焦点处理
焦点事件监听
et_text.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// 此处为得到焦点时的处理内容
} else {
// 此处为失去焦点时的处理内容
}
}
});
获取失去焦点
et.setFocusable(true);
et.setFocusableInTouchMode(true);
et.requestFocus();
et.clearFocus();//失去焦点
et.requestFocus();//获取焦点
5. 默认不弹出软键盘
① 在 AndroidMainfest.xml中设置windowSoftInputMode属性
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustUnspecified|stateHidden"/>
② 在OnCreate()函数中,加上下面的代码:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
③ 让 EditText失去焦点
editText.clearFocus();
6. 设置默认输入法
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT); //中文键盘
editText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_URI); //英文键盘
editText.setInputType(InputType.TYPE_CLASS_NUMBER);//数字键盘
7. 强制显示隐藏软键盘
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //隐藏软键盘
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); //显示软键盘
8. 始终不弹出软键盘
① 在XML文件中,Edittext父布局上进行如下设置:
android:focusable="true"
android:focusableInTouchMode="true"
② 在Java代码中,添加下面属性:
editText.setInputType(InputType.TYPE_NULL);
9. 显示隐藏密码
① 在XML文件中设置:
Android:password="true" // 以”.”形式显示密码文本
android:inputType="textPassword" //不可见密码
android:inputType="textVisiblePassword" //可见密码
② 在Java代码中设置:
et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码
et.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码
10. 输入状态监听
et_text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//s 输入框中改变前的字符串信息
//start 输入框中改变前的字符串的起始位置
//count 输入框中改变前后的字符串改变数量一般为0
//after 输入框中改变后的字符串与起始位置的偏移量
Log.d("TAG","beforeTextChanged--------------->");
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//s 输入框中改变后的字符串信息
//start 输入框中改变后的字符串的起始位置
//before 输入框中改变前的字符串的位置 默认为0
//count 输入框中改变后的一共输入字符串的数量
Log.d("TAG","onTextChanged--------------->");
}
@Override
public void afterTextChanged(Editable s) {
//s 输入结束呈现在输入框中的信息
Log.d("TAG","afterTextChanged--------------->");
}
});
11. 光标设置
指定光标位置
EditText et = (EditText) findViewById(R.id.editText);
et.setSelection(et.getText().toString().length());
光标显示隐藏
xml布局:
android:cursorVisible="true"//显示
android:cursorVisible="false"//隐藏
Java代码:
edit.setCursorVisible(true);//显示
edit.setCursorVisible(false);//隐藏
设置光标样式
1)在drawable中创建et_cursor.xml文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#666666" />
<size android:width="1dp" />
</shape>
2)在Edittext中引用et_cursor.xml文件
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:textCursorDrawable="@drawable/et_cursor"/>
12. 单行设置
xml布局:
android:singleLine="true"
高版本SDK中android:singleLine是过期属性,虽然还可以用,但是尽量不要使用过期属性。建议使用新属性替换:
android:maxLines="1"
android:inputType="text"
Java代码:
setSingleLine
edit.setSingleLine();
//或者
edit.setSingleLine(true);
setMaxLines
edit.setMaxLines(1);
edit.setInputType(InputType.TYPE_CLASS_TEXT);
※ EditText 在设置单行时,由于android:singleLine="true"属性已过时,提示用使用android:maxLines="1"代替,但是如果只设置maxLines是没有效果的。这是因为android:inputType属性默认值为none的缘故。只要将android:inputType属性设置为其他值,配合maxLines一起使用就可以实现单行显示。同理 TextView 亦是如此。
13.限制输入字数
TextWatcher mTextWatcher = new TextWatcher() {
private CharSequence temp;
private int editStart ;
private int editEnd ;
@Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
temp = s;
}
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
mTextView.setText(s);
}
@Override
public void afterTextChanged(Editable s) {
editStart = mEditText.getSelectionStart();
editEnd = mEditText.getSelectionEnd();
if (temp.length() > 10) {
Toast.makeText(TextWatcherDemo.this,
"你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)
.show();
s.delete(editStart-1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
}
};
常用控件之imageview
显示图片的控件
1. foreground、src 和 background 属性区别
1) background 指的是背景, foreground 指的是前景,而 src 指的是内容;三者可以同时使用;
2) src 填入图片时,是按照图片大小直接填充,并不会进行拉伸;而使用 background 和
foreground 填入图片,则是会根据ImageView给定的宽度来进行拉伸;
3) background 和 foreground 是所有view都有的属性,总是缩放到view的大小,不受 scaleType 影
响;而 src 是ImageView特有属性,它会受到 scaleType 的影响。
举个栗子:
<ImageView
android:layout_width="260dp"
android:layout_height="260dp"
android:foreground="#99000000"
android:background="#ff0000"
android:src="@mipmap/ic_launcher"/>
效果图:
从图上可以看出,虽然 foreground 和 src 都为前景,但是 foreground 却在 src 之上。从层级上比
较: foreground > src > background
2. adjustViewBounds属性用法
ImageView为我们提供了 adjustViewBounds 属性,用于设置缩放时是否保持原图长宽比。
关于ImageView的自适应,一般是根据图片的宽高比进行等比缩放展示。下面重点讲解一下如何正确使用 ImageView的 adjustViewBounds 属性实现图片的自适应展示。
adjustViewBounds 官方API:
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect
ratio of its drawable.
Must be a boolean value, either "true" or "false".
大致意思是:当 adjustViewBounds=“ture” 的时候,可以通过调整 ImageView的界限来保持图片的
宽高比例。
- AdjustViewBounds使用介绍
1)当ImageView的 layout_width 和 layout _height 都为固定值时;
XML布局实现:
<ImageView android:id="@+id/image"
android:layout_width="320dp"
android:layout_height="260dp"
android:layout_gravity="center"
android:adjustViewBounds="ture"
android:background="#ff0000"
android:src="@mipmap/test_bg"/>
Java代码实现:
ViewGroup.LayoutParams lp = imageView.getLayoutParams();
lp.width = 320;
lp.height = 260;
imageView.setLayoutParams(lp);
imageView.setAdjustViewBounds(true);
展示效果
从效果图来看,当 layout_width 和 layout height 都为固定值
时, adjustViewBounds=“ture” 是不起作用的。ImageView将始终是设定值的宽高,图片按比例被
直接填充到ImageView控件中。
2)当ImageView的 layout width 和 layout_ height 只有一个为固定值时;
XML布局实现
<ImageView android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="#ff0000"
android:src="@mipmap/test_bg"/>
展示效果:
- 从效果图来看,当ImageView的 layout_ width 和 layout_ height 只有一个为固定值时,图片的宽/高将会与ImageView的 layout_ width/layout_ height 的固定值进行比较。
- 若图片 宽/高小于设置固定值时,此时ImageView的 layout_ height/layout_ width将与图片的高/宽相同,图片将会以其高/宽来填充ImageView;
- 若图片 宽/高大于或者等于设置固定值时,此时ImageView将与图片拥有相同的宽高比,图片将会以自身的宽高比填充到ImageView。
3)当ImageView的 layout_ width 和 layout_ height 都为 wrap_content 属性时。
XML布局实现:
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="#ff0000"
android:src="@mipmap/test_bg"/>
java代码实现:
ViewGroup.LayoutParams lp = imageView.getLayoutParams();
lp.width = LayoutParams.WRAP_CONTENT;
lp.height = LayoutParams.WRAP_CONTENT;
imageView.setLayoutParams(lp);
imageView.setAdjustViewBounds(true);
展示效果
从效果图来看,当ImageView的 layout_ width 和 layout_ height 都为 wrap_content 时,
adjustViewBounds 是不起作用的。ImageView将始终与图片拥有相同的宽高比展示。
3. 设置透明度
ImageView 设置透明度主要有以下几种方法:
android:alpha // 0f~1f
setAlpha(float alpha); // 0f~1f
setAlpha(int alpha); // 0~255,已过时
setImageAlpha(int alpha); // API>=16
4. 设置图片方式
1)设置前景
foreground
xml布局中:
android:foreground="@color/blue"
android:foreground="@mipmap/ic_launcher"
android:src="@mipmap/ic_launcher"
Java代码中:
// 设置前景图
image.setForeground(getResources().getDrawable(R.mipmap.ic_launcher));
// 设置前景色
image.setForeground(getResources().getDrawable(R.color.blue));
注意:
关于设置 foreground 无效问题,View源码片段:
case R.styleable.View_foreground:
if (targetSdkVersion >= VERSION_CODES.M || this instanceof FrameLayout) {
setForeground(a.getDrawable(attr));
}
break;
foreground 属性只有在以下两种情况下生效:
(1) Android M版本(6.0)及以上 ;
(2) FrameLayout本身及其子类。
src
xml布局中:
android:src="@mipmap/ic_launcher"
Java代码中:
// 1. setImageDrawable(Drawable drawable)
image.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); //不会变形
// 2. setImageBitmap(Bitmap bm)
Stringpath=Environment.getExternalStorageDirectory()+File.separator+”test.jpg”;
Bitmap bm = BitmapFactory.decodeFile(path);
image.setImageBitmap(bm);//不会变形
// 3. setImageResource(int resId)
image.setImageResource(R.drawable.ic_launcher);//不会变形
2)设置背景
xml布局中:
android:background="@mipmap/ic_launcher"
android:background="@color/blue"
Java代码中:
// 1. setBackground(Drawable background)
image.setBackground(getResources().getDrawable(R.drawable.ic_launcher));//变形
// 2. setBackgroundResource(int resid)
image.setBackgroundResource(R.drawable.ic_launcher);//变形
// 3. setBackgroundDrawable(Drawable background)
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));//变形
// setBackgroundColor(int color)
image.setBackgroundColor(getResources().getColor(R.color.blue));
5. ScaleType属性
android:scaleType 用于设置显示的图片如何缩放或者移动以适应ImageView的大小,Java代码中可以通过 imageView.setScaleType(ImageView.ScaleType.CENTER); 来设置。 可选值如下:
MATRIX / matrix:用矩阵的方式绘制,从ImageView的左上角开始绘制原图,不缩放图片, 超过ImageView部分作裁剪处理;
CENTER / center:保持原图的大小,显示在ImageView的中心。当原图的尺寸大于ImageView的
尺寸,超过部分裁剪处理;
CENTER_CROP / centerCrop:保持横纵比缩放图片,直到完全覆盖ImageView为止(指的是ImageView的宽和高都要填满),原图超过ImageView的部分作裁剪处理;
CENTER_INSIDE / centerInside:将图片的内容完整居中显示,通过按比例缩小原图尺寸的宽高等于或小于ImageView的宽高。如果原图的尺寸本身就小于ImageView的尺寸,则原图的尺寸不作任何处理,居中显示在ImageView;
FIT_XY / fitXY:把原图宽高进行不保持原比例放缩,直到填充满ImageView为止;
FIT_START / fitStart:把原图按比例放缩使之等于ImageView的宽高,缩放完成后将图片放在ImageView的左上角;
FIT_CENTER / fitCenter:把原图按比例放缩使之等于ImageView的宽高使之居中显示,缩放后放于中间;
FIT_END / fitEnd:把原图按比例放缩到ImageView的宽高,缩放完成后将图片放在ImageView的右下角。
常用控件之CheckBox
设置复选框的控件
CheckBox详解
如果只修改选中框的样式,设置button属性背景为selector就可以
修改CheckBox选择框的大小颜色
修改选中框和文字的顺序
修改checkbox文字选中框顺序
常用控件之radiobutton
设置单选框的控件
radiobutton详解
常用控件之Toast
用于提示的控件
toast常用属性
常用控件之log
用于打印日志的控件
在Android Studio 中,在方法的外面输入logt,然后按下TAB,IDE会自动为你补全,该快捷键是生成一个String类型的TAG常量,内容为类名。
打印五种日志,可以分别输入logi、logd、logv、logw、loge,最后按下TAB,会自动补全。
重写Android Log的输出,只在debug的时候输出,release 的版本不输出
常用控件之listview
用于显示一个列表
listview使用总结
listview条目高度要在item的布局里嵌套一层设置高度
多条目使用
listview实现刷新加载
listview局部刷新
常用控件之GridView
GridView的使用
为GridView设置点击效果
常用控件之ContextMenu
弹出菜单控件
选项菜单contextmenu用法
menu四个参数,第一个是组数,第二个是下标,第三个是0,第四个是标题
常用控件之加载进度条ProgressBar
加载进度条
ProgressBar使用详解
常用控件之可拖动进度条SeekBar
可拖动进度条
SeekBar 的使用和简析
常用控件之Notification
通知栏控件