文本框(TextView)与编辑框(EditText)详解


重点一:TextView

TextView直接继承了View,TextView还是EditText、Button个UI组件的父类,TextVeiw的作用就是在界面上显示文本。其实从功能上来看,TextVeiw其实就是一个文本编辑器,只是Android关闭了它的文字编辑功能。现在开发者用到的都是它的子类:EditText。

  1. TextView继承和被继承的图片
  2. android多行文本框 安卓文本框控件_android多行文本框

  3. 其中常用的xml属性
    -其详细讲解请看文章:

  • 例子一:简单的几个布局,附带效果图
  • 布局
<!-- 设置字号为20pt,文本框结尾处绘制图片  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我很烦"
        android:textSize="20sp"/>
<!-- ellipsize="middle"设置中间省略,textAllCaps="true"所有字母大写    -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:text="我的天空,abcabcabcabcabc是谁的模样abcabcabc是谁的模样"
        android:ellipsize="middle"
        android:textAllCaps="true"/>
<!--autoLink="email"  这里可以添加链接-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:text="邮件是 kongyeeku@163.com"
        android:autoLink="email"/>
<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:text="号码是:13800000000"
        android:autoLink="phone"/>
<!--<!– 设置文字颜色、大小,并使用阴影 –>-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="我带着阴影"
        android:shadowColor="#00f"
        android:shadowDx="10.0"
        android:shadowDy="8.0"
        android:shadowRadius="3.0"
        android:textColor="#f00"
<!--测试密码框 –password="true"就是你输入的内容不可见;-->
    <TextView android:id="@+id/passwd"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="10dp"
              android:text="你好啊"
              android:password="true"/>
  • 图片效果

android多行文本框 安卓文本框控件_EditView_02


  • 例子二:为TextiView添加背景
    例如:
<!--下面用到的是xml中的 background 属性,可以自己定义颜色也可以是一张图片; 例如android:background="@drawable/ic_launcher",当然用得最多的还是自己定义的样式-->

    <TextView android:id="@+id/sss"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="你好啊"
        android:background="#465715"/>

效果图:

android多行文本框 安卓文本框控件_android_03


重点二:EditText

EditText:是特别常用的编辑框,用户可以在里面输入自己需要输入的字符
注意:其中xml请看上面的xml表格里的各种属性。

1、直接看一个最原始的输入框效果图

android多行文本框 安卓文本框控件_Text_04

它用到的是最基础的一些xml属性,下面的布局运行起来就是上面的效果

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入账号"
        android:layout_margin="10dp"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入电话号码"
        android:layout_margin="10dp"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入性别"
        android:layout_margin="10dp"
        />

2、但很多时候我们用到的都是通过自己自定义的效果

例如下面效果:

android多行文本框 安卓文本框控件_Text_05

实现:这里需要先定义样式,怎么定义后面博客会写

第一步定义样式:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@android:color/transparent" />
    <corners android:radius="4dp" />
    <stroke
        android:width="2px"
        android:color="#e74c4c" />
</shape>

第二步:在布局中引用

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入性别"
        android:background="@drawable/tv_background_red"
        android:layout_margin="10dp"
        />

第三步:在代码中定义并运行项目

public class TextViewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textview);
    }
}

其实在开发中,几乎每时每刻都会用到这两个空间,用的多了就熟悉了。写的很乱,因为上班中写的。还请见谅!
样式怎么定义,会在后续讲解!

此处的布局只要复制到自己布局即可,无需代码引用。所以就不提供dome了!谢谢