EditText是程序用于和用户进行交互的控件,它允许用户在控件内输入和编辑内容,并可以在程序中对这些内容进行处理,EditText在进行发短信、发微博等操作,不得不使用Edit Text
【EditText的常用属性】
1.EditText的简单应用
- android:background="@null" 使控件失去背景框
在未输入文字时,会出现提示性文字“请输入”,输入文字时立即消失,当输入的行数超过1时,文本向上滚动
如果使用android:singleLine="true"属性,限定的最大行数将失效,文本内容向左滑动
<EditText
android:hint="请输入"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
/>
2.自定义EditText
(1)在drawable中自定义背景,并将其作为EditText的背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 设置不同的形状 -->
<!-- 矩形 android:shape="rectangle" -->
<!-- 椭圆 android:shape="oval" -->
<!-- 直线android:shape="line" -->
<!-- 圆 android:shape="ring" -->
<!-- 分别设置4个脚的圆角 -->
<!-- android:topLeftRadius="3dp" -->
<!-- android:topRightRadius="3dp" -->
<!-- android:bottomLeftRadius="3dp" -->
<!-- android:bottomRightRadius="3dp" -->
<!-- 统一设置圆角 -->
<corners android:radius="3dp" />
<!-- 渐变色 -->
<gradient
android:startColor="#f00"
android:centerColor="#0f0"
android:endColor="#00f"
/>
<!-- 边框宽度及颜色 -->
<!-- 描边,空的距离android:dashGap="5dp" -->
<!-- 虚线宽android:dashWidth="13dp" -->
<stroke
android:width="10dp"
android:color="#66000000" />
<!-- 间距 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<!-- 宽高,需把android:layout_width和layout_height设置为自适应 才生效 -->
<size
android:height="70dp"
android:width="120sp" />
<!-- 填充颜色 与gradient设置的属性有冲突 -->
<!-- 编辑框的背景颜色 -->
<solid android:color="#FFFFFF" >
</solid>
</shape>
时刻检测EditText中内容的变化
EditText editText = findViewById(R.id.edittext);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//改变之前
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//内容改变
}
@Override
public void afterTextChanged(Editable s) {
//改变之后
}
});