TextView组件:
直接之类:Button.CheckedTextView,Chronometer,DigitalClock,EditText
间接子类:AutoCompleteTextView,CheckBox,CompounButton,ExtractEditText,MultiAutoCompleteTextView,
RadioButton,ToggleButton
android:autoText 设置是否当文本为URL链接/email、电话号码、map时,文本显示为可点击的链接,可选值(none,web,email,phone,map,all)
android:autoText 如果设置,将自动执行输入值的拼写纠正,在显示输入法并输入的时候起作用。
android:captalize 设置英文字母大写类型,需要弹出输入法才能看得见。
android:cursorVisble 设置光标为显性/隐藏,默认显性。
android:digits 设置允许输入哪些字符。
android:drawableBotton 在text的下方输入一个drawable,如图片,如果指定一个颜色的话会吧text的背景设置为该颜色,并且同时和background使用时覆盖后者。
android:editable 设置是否可编辑。
android:gravity 设置文本位置。如果设成“center”,文本居中显示。
android:hint Text为空时显示的文字提示信息,可以通过textColorHint设置提示信息的颜色,此属性在EditView中使用,这里也可以使用。
android:inputType 设置文本的类型,用于输入法显示合适的键盘类型。
android:textColor 设置文本颜色。
android:textColorLink 文字连接的颜色。
android:Size 设置文字大小。
android:padding 边距。
android:layout_margin 外边距。
下面看一个通过用这些属性写的简单的例子:
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:padding="30dp" 设置边距
android:textSize="30dp" 设置字体大小
android:layout_margin="50dp" 设置外边距
android:text="@string/hello" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" 设置居中
android:text="@string/hello"/>
</LinearLayout>
这是其显示效果:
下面在看一个可以实现连接的例子:
Strings.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.ifeng.com</string>
<string name="email">小强的邮箱:sss@111.com</string>
<string name="phoneNumber">电话号码:15933936875</string>
<string name="mapUrl">620 Eighth Avenue New York,NY 10018\n</string>
<string name="autoAll">http://www.ifeng.com</string>
</resources>
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/autoAll"/>
</LinearLayout>
实现效果如图:
点击电话号码:
这样就可以打电话了。
下面我们在写一个利用TextView写的边框:
首先写一个派生类MyBorderTextView继承TextView,写一个构造方法(带有两个参数)覆盖父类的方法。
MyBorderTextView类:
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); //必须放在第一句
}
//覆盖一个onDraw方法,TextView绘制的时候调用
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint paint = new Paint();
//设置边框颜色
paint.setColor(android.graphics.Color.RED);
//绘制边框
canvas.drawLine(0, 0, this.getWidth()-1, 0, paint);
canvas.drawLine(0, 0, 0, this.getHeight()-1, paint);
canvas.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()-1, paint);
canvas.drawLine(0, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1,paint);
}
}
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="15dp"
android:layout_margin="10dp"
android:textColor="#cccccc"
android:text="@string/hello"
/>
</LinearLayout>
实现效果:
EditText组件:
直接之类:AutoCompleteTextView,ExtractEditText
间接子类:MultiAutoCompleteTextView
因为EditText是一个TextView子类所以很多属性和TextView属性相同。所以说几个特殊的。
android:drawableTop 在text的正上方输入一个drawable,在EditView中,在文本框里,而且上出不了。
android:editable 设置是否可编辑,仍然可以获取光标,但是无法输入。
android:numeric 如果被设置,该TextView有一个数字输入法。有如下值设置:integer正整数、signed带符号整数、decima带小数点浮点数。
android:phoneNumber 设置电话号码的输入方式。
下面看一个限定输入文字的例子:
Xml文件内容:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:digits="01234" /> 只能输入01234
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:digits="abcd" /> 只能输入abcd
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="decimal|signed" />
EditText中回车键的使用
为EditText对象的注册OnKeyListener事件,实现onKey()方法
<EditText
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="text1" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Button" />
et.setOnKeyListener(this);
…
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
btn.setText(et.getText());
et.setVisibility(View.GONE);
btn.setVisibility(View.VISIBLE);
}
return true;
}
效果:
回车:
自动完成输入内容的组件
AutoCompleteTextView
MultiCompleteTextView
<AutoCompleteTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/autotx"
/>
<MultiAutoCompleteTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mautotx"
/>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hide);
btn = (Button) this.findViewById(R.id.button1);
et = (EditText) this.findViewById(R.id.text1);
et.setOnKeyListener(this);
autotx = (AutoCompleteTextView) this.findViewById(R.id.autotx);
String[] s={"a","abc","ab","b","bc","bdad"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,s);
autotx.setAdapter(adapter);
mautotx = (MultiAutoCompleteTextView) this.findViewById(R.id.mautotx);
mautotx.setAdapter(adapter);
mautotx.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}