Android之路之七(UI组件1——TextView&EditText)

UI组件详解1——TextView&EditText

今天,学习Android的第七天。今天学习的是UI(用户界面)组件中的TextViewEditText

 

TextView(文本视图)之前的学习中已经用到了。也即显示文本的组件。今天,具体的来了解一下它:

首先TextView的属性设置如下:

<TextView 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:id="@+id/one"/>

TextView的直接子类包括Button,CheckedTextView、Chroonometer、DigitalClock、EditText

间接子类有:AutoCompleteTextView、CheckBox、CompoundButton、ExtractEditText、MultiAutoCompleteTextView、RadioButton、ToggleButton

其xml属性有很多,下面来介绍其中的一种最为常用的:android:autoLink,设置是否当前文本为URL连接/email/电话号码/map时,文本显示为可点击的连接。可选值有(none/web/email/phone/map/all

下面举实例来演示:

首先在string.xml文件中定义字符与数值:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, TextViewTestActivity!</string>
    <string name="app_name">TextViewTest</string>
    <string name="webUrl">百度首页:http://www.baidu.com</string>
    <string name="email">谁的邮箱:874154731@qq.com</string>
    <string name="phoneNumber">小强电话:1333333333334</string>
    <string name="mapUrl">620 Eighth Avenue New York,NY 10018 \n</string>
</resources>

然后配置auto_link.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:text="@string/webUrl"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="email"
        android:text="@string/email"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:text="@string/phoneNumber"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="map"
        android:text="@string/mapUrl"/>
    <TextView 
        android:id="@+id/tvHtml"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

然后在TextViewTestActivity.java文件中调用上面的布局文件。启动虚拟机:

 

 

在这里需要注意的是:android:autoLink=“email”:会出现unsupported action,其原因是虚拟机存在bug,并不具备email超链接功能。

在这里,还有种较为方便的实现上述显示的方法:

string.xml文件中添加:

<string name="autoAll">百度首页:http://www.baidu.com 谁的邮箱:874154731@qq.com小强电话:1333333333334</string>

 

auto_link.xml布局文件中添加:

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="all"
        android:text="@string/autoAll" />

TextViewTestActivity.java文件中添加:

TextView tv = (TextView) this.findViewById(R.id.tvHtml);
        String htmlStr = "<font size='30' color='#00FF22'>我爱你</font><b>money</b>" +
        		"<a href='http:www.baidu.com'>百度</a>";        
        tv.setText(Html.fromHtml(htmlStr));

那么启动虚拟机后:

接下来,来实现带有边框的TextView。首先,在MyBorderTextView.java文件中自定义边框的TextView

 

package cn.class3g.activity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyBorderTextView extends TextView{
	//必须实现带两个参数的构造
	public MyBorderTextView(Context context, AttributeSet attrs) {
		super(context, attrs);	
	}
	public void onDraw(Canvas canvas){
		super.onDraw(canvas);
		
		//创建画刷
		Paint paint = new Paint();
		//设置当前颜色
		paint.setColor(android.graphics.Color.GREEN);
		//开画
		canvas.drawLine(0, 0, this.getWidth()-1, 0, paint);
		canvas.drawLine(0, 0, this.getHeight()-1, 0, paint);
		canvas.drawLine(this.getWidth()-1, 0, this.getWidth()-1, getHeight()-1, paint);
		canvas.drawLine(0, this.getHeight()-1, getWidth()-1, getHeight()-1, paint);
	}
}

然后在border_tv.xml布局文件中使用这个边框标签:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <cn.class3g.activity.MyBorderTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="14dp"
        android:layout_margin="5dp"
        android:textColor="#cccccc"
        android:text="去屎去屎!!!!!!!!!!"/>
</LinearLayout>

启动虚拟机:

 

Ok,接下来介绍EditText(编辑文本)组件,当然这在之前也是用到过的。

EditText是TextView的直接子类,而EditText的直接子类有包括:

AutoCompleteTextView、 ExtractEditText

间接子类有:MultiAutoCompleteTextView

首先依然是EditText的基本属性设置:

<EdiTex 
android:id="@+id/id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/
>

 

然后来了解EditText可输入的特定字符的实现:

main.xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EdiText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:digits="0123"/>
    <EdiText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:password="true"/>
    <EdiText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"/>
    <EdiText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number"/>
    <EdiText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numeric="decimal|signed"/>
</LinearLayout>


 

EditTextTestActivity.java中调用main.xml布局文件

启动虚拟机:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值