一、TextView
属性设置
<TextView
android:id="@+id/tvHtml"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
注意:setText()或setTextColor()方法的参数是一个int值还是一个资源地址
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, TextViewTest1Activity!</string>
<string name="app_name">TextViewTest1</string>
<string name="webUrl">凤凰网:http://www.ifeng.com</string>
<string name="email">小强的邮箱:sss@111.com</string>
<string name="phoneNumber">电话号码:13333333333</string>
<string name="mapUrl">620 Eighth Avenue New York, NY 10018 \n</string>
<string name="autoAll">
凤凰网:http://www.ifeng.com 小强的邮箱:sss@111.com 电话号码: 1333333335
</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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/autoAll" />
<TextView
android:id="@+id/tvHtml"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
在TextViewTestActivity.java文件中调用上面的布局文件。启动虚拟机:
实现带有边框的TextView。首先,在MyBorderTextView.java文件中自定义边框的TextView:
自定义带边框的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);
}
//覆盖父类的onDraw方法
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, 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);
}
}
在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="15dp"
android:layout_margin="10dp"
android:textColor="#cccccc"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="15dp"
android:text="@string/hello"
android:background="@drawable/tv_back"
android:gravity="center"
android:textColor="#FFFFFF"
/>
</LinearLayout>
启动虚拟机:
二、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" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:digits="0123"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
/>
<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:inputType="number"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="decimal|signed"
/>
</LinearLayout>
在EditTextTestActivity.java中调用main.xml布局文件
启动虚拟机:
EditText中回车键的使用
Onkey_layout.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" >
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
android:visibility="gone"/>
</LinearLayout>
在EditTextTestActivity.java文件中为EditText对象的OnKeyListener事件,实现onKey()方法,同时调用onkey_layout.xml:
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
public class EditTextTestActivity extends Activity implements OnKeyListener{
Button btn=null;
EditText et=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onkey_layout);
findViews();
}
private void findViews(){
btn = (Button) this.findViewById(R.id.button);
et = (EditText) this.findViewById(R.id.edit);
et.setOnKeyListener(this);//注册
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER){
btn.setText(et.getText());
et.setVisibility(View.GONE);
btn.setVisibility(View.VISIBLE);
}
return false;
}
}
启动虚拟机:
点击出车键弹出一个Button:
自动完成输入内容的组件
AutoCompleteTextView、MultiCompleteTextView
两者之间的区别在于后者可以自动匹配多组相近字符,前者只可匹配一组相近字符
auto_complete.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" >
<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"
/>
</LinearLayout>
AutoCompleteActivity.java
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
public class AutoCompleteActivity extends Activity {
AutoCompleteTextView autoTx = null;
MultiAutoCompleteTextView mautoTx = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auto_comlete);
findViews();
String[] str = {"abc","add","axy","aaaa", "bcd","bbbd","bdcc"};
ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line,str);
autoTx.setAdapter(adapter);
//---------MulitAutoCompleteTextView
mautoTx.setAdapter(adapter);
mautoTx.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());//中间以逗号分隔
}
private void findViews(){
autoTx = (AutoCompleteTextView) findViewById(R.id.autoTx);
mautoTx = (MultiAutoCompleteTextView) findViewById(R.id.mautoTx);
}
}
启动虚拟机: