Android开发学习 之 五、基本界面控件-1文本控件

 

五、基本界面控件-1文本控件

5.1文本编辑控件

5.1.1 TextView


图5.1.1 TextView

 

android.widget.TextView一般用来文本展示,继承自android.view.View,在android.widget包中。

他的常用子类有Button, CheckedTextView, Chronometer, DigitalClock, EditText。

 

常用属性设置:

 

android:text=""

文字显示

android:autoLink=””

链接类型。Web网址,email邮件,phone电话,map地图。Linkify。

 

 

链接状态时,Web情况可直接调用浏览器进行浏览。Email直接调用手机的Email软件,phone转到拨打电话页面。

 

5.1.2 EditText


图5.1.2EditText(四种用法:普通用法、密码框、输入电话、输入数字)

 

android.widget.EditText为输入框,继承自android.widget.TextView,在android.widget包中。他的常用子类。AutoCompleteTextView和MultiAutoCompleteTextView。ExtractEditText与输入法有关。

 

常用属性设置:

 

android:hint="请输入用户名"

输入框的提示文字

android:password=""

True为密码框

android:phoneNumber=""

True为电话框

android:numeric=""

数字框。Integer正整数, signed整数(可带负号), decimal浮点数。

android:digits

设置允许输入哪些字符。如“1234567890.+-*/%\n()”

 

 

5.1.3 AutoCompleteTextView


图5.1.3AutoCompleteTextView和MultiAutoCompleteTextView

 

android.widget.AutoCompleteTextView带提示的输入框,继承自android.widget.EditText,在android.widget包中。

AutoCompleteTextViewhe和MultiAutoCompleteTextView都是自动提示,一个是单选,一个多选。

 

常用属性设置:

 

android:completionThreshold

输入几个字符时提示

 

AutoCompleteTextView就是一个带自动提示的EditText,当输入字符时,会出现提示窗口,点击选择即可。

首先在layout中定义一个AutoCompleteTextView,然后需要在Activity设置数据源就可以了。

ArrayAdapter的构造方法三个参数为:上下文的Context,每行的textView布局,数据源。

 

Java代码 复制代码  收藏代码
  1. this.autoCompleteTextView = (AutoCompleteTextView) super.findViewById(R.id.autoCompleteTextView);   
  2. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.arrayadapte_textview, CITY_NAMES);   
  3. this.autoCompleteTextView.setAdapter(arrayAdapter);  
this.autoCompleteTextView = (AutoCompleteTextView) super.findViewById(R.id.autoCompleteTextView);ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.arrayadapte_textview, CITY_NAMES);this.autoCompleteTextView.setAdapter(arrayAdapter);
 

 

MultiAutoCompleteTextView和AutoCompleteTextView的类似,也是带有提示的输入框。区别在于MultiAutoCompleteTextView可以连续提示,选择一个提示项后会自动添加一个分隔符,在输入时继续提示。AutoCompleteTextView则属于单选模式的。

MultiAutoCompleteTextView使用时需要设置分隔符类CommaTokenizer。其他与AutoCompleteTextView一样。

 

Java代码 复制代码  收藏代码
  1. this.multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());  
this.multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

 

 

 

5.1.4 TextSwitcher

android.widget.TextSwitcher文字切换。继承自android.widget.ViewSwitcher(ViewGroup),在android.widget包中。

使用方法setInAnimation(Animation),setOutAnimation(Animation)设置动画。

 

例子,设置ViewSwitcher的动画,并使用数字时钟更改ViewSwitcher的字符串

Java代码 复制代码  收藏代码
  1. public class SwitcherActivity extends Activity implements ViewSwitcher.ViewFactory, View.OnClickListener {   
  2.   
  3.     private Button buttonChangeText;   
  4.     private TextSwitcher myTextSwitcher;   
  5.     private DigitalClock myDigitalClock;   
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {   
  9.         super.onCreate(savedInstanceState);   
  10.         super.setContentView(R.layout.switcher);   
  11.   
  12.         this.buttonChangeText = (Button) super.findViewById(R.id.buttonChangeText);   
  13.         this.myTextSwitcher = (TextSwitcher) super.findViewById(R.id.myTextSwitcher);   
  14.         this.myDigitalClock = (DigitalClock) super.findViewById(R.id.myDigitalClock);   
  15.         this.buttonChangeText.setOnClickListener(this);   
  16.         this.myTextSwitcher.setFactory(this);   
  17.   
  18.         this.myTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));   
  19.         this.myTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));   
  20.   
  21.     }   
  22.   
  23.     @Override  
  24.     public View makeView() {   
  25.         TextView textView = new TextView(this);   
  26.         textView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);   
  27.         textView.setTextSize(36);   
  28.         return textView;   
  29.     }   
  30.   
  31.     @Override  
  32.     public void onClick(View v) {   
  33.         this.myDigitalClock.addTextChangedListener(textWatcher);   
  34.     }   
  35.   
  36.     private android.text.TextWatcher textWatcher = new android.text.TextWatcher() {   
  37.   
  38.         @Override  
  39.         public void onTextChanged(CharSequence s, int start, int before, int count) {   
  40.             SwitcherActivity.this.myTextSwitcher.setText(SwitcherActivity.this.myDigitalClock.getText());   
  41.         }   
  42.   
  43.         @Override  
  44.         public void beforeTextChanged(CharSequence s, int start, int count, int after) {   
  45.         }   
  46.   
  47.         @Override  
  48.         public void afterTextChanged(Editable s) {   
  49.         }   
  50.     };   
  51. }  
public class SwitcherActivity extends Activity implements ViewSwitcher.ViewFactory, View.OnClickListener {	private Button buttonChangeText;	private TextSwitcher myTextSwitcher;	private DigitalClock myDigitalClock;	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		super.setContentView(R.layout.switcher);		this.buttonChangeText = (Button) super.findViewById(R.id.buttonChangeText);		this.myTextSwitcher = (TextSwitcher) super.findViewById(R.id.myTextSwitcher);		this.myDigitalClock = (DigitalClock) super.findViewById(R.id.myDigitalClock);		this.buttonChangeText.setOnClickListener(this);		this.myTextSwitcher.setFactory(this);		this.myTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));		this.myTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));	}	@Override	public View makeView() {		TextView textView = new TextView(this);		textView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);		textView.setTextSize(36);		return textView;	}	@Override	public void onClick(View v) {		this.myDigitalClock.addTextChangedListener(textWatcher);	}	private android.text.TextWatcher textWatcher = new android.text.TextWatcher() {		@Override		public void onTextChanged(CharSequence s, int start, int before, int count) {			SwitcherActivity.this.myTextSwitcher.setText(SwitcherActivity.this.myDigitalClock.getText());		}		@Override		public void beforeTextChanged(CharSequence s, int start, int count, int after) {		}		@Override		public void afterTextChanged(Editable s) {		}	};}
 
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:layout_width="fill_parent"  
  4.               android:layout_height="fill_parent"  
  5.               android:orientation="vertical"  
  6.               android:gravity="center_horizontal">  
  7.   
  8.     <Button android:id="@+id/buttonChangeText"  
  9.             android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content"    
  11.             android:text="开始" />  
  12.        
  13.     <DigitalClock android:id="@+id/myDigitalClock"  
  14.                   android:layout_width="fill_parent"  
  15.                   android:layout_height="wrap_content"  
  16.                   android:textSize="36dip"/>  
  17.        
  18.     <TextSwitcher android:id="@+id/myTextSwitcher"  
  19.                   android:layout_width="fill_parent"  
  20.                   android:layout_height="wrap_content" />  
  21.   
  22. </LinearLayout>  
<?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"		      android:gravity="center_horizontal">    <Button android:id="@+id/buttonChangeText"	        android:layout_width="wrap_content"	        android:layout_height="wrap_content" 	        android:text="开始" />		<DigitalClock android:id="@+id/myDigitalClock"			      android:layout_width="fill_parent"			      android:layout_height="wrap_content"			 	  android:textSize="36dip"/>	    <TextSwitcher android:id="@+id/myTextSwitcher"			      android:layout_width="fill_parent"			      android:layout_height="wrap_content" /></LinearLayout>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值