您的位置:首页 > 产品设计 > UI/UE

Android学习-UI组件1

2011-12-13 22:18 309 查看
一、 TextView 显示文本的组件:
1、之前已经学习过TextView,对它也不只在实例中使用过一次,首先看一下它最基本的用法:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="无处安放的青春乱了谁的流年"
/>
通过以上的属性设置可以设置组件的宽度和高度,组件的显示文字
最为一个重要的组件怎么能只有这几种属性,
其他常用属性的用法;
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00ffaa"
android:padding="20dp"
android:layout_margin="30dp"
android:textSize="30dp"
android:background="@drawable/browser_textfield_disabled_selected"
android:text="无处安放的青春乱了谁的流年" />

如同上文代码,我们也可以设置组件的文字颜色,组件的文字尺寸,还有边距等等。。。。。。



注意:在这里需要特别提醒的是,android-padding和android:layout-margin的不同,
Android-padding属性所设置的是文字距TextView边缘的距离,也可以说是设置的内边距,
Android:layout-margin属性所设置的是TextView组件距其他组件的距离,

另外他们各自的属性里还有android-padding left,right,top等属性来设置距离左右上下的距离;

除了可以在xml文件中设置TextView的属性外还可以在java中设置属性下面是一段在代码里设置的字体颜色的片段:

TextView tv = (TextView) this.findViewById(R.id.haozi);
tv.setTextColor(android.graphics.Color.BLUE);

2、设置TextView的背景的三种方法,;
》》setBackgroundResource : 通过颜色资源ID来设置背景颜色
》》,setBackgroundColor: 通过颜色值设置背景颜色;
》》setBackgroundDrawable:通过Drawable对象设置背景色;

3、在TextView 中显示URL及不同字体的大小,不同颜色的文本;
Test不仅可以显示文本还可以识别文本中的链接,并将这些链接转换成可单击的链接,系统会根据不同类型的连接调用相应的软件进行处理,如果当连接是网址是,单击连接就会调用安卓系统的浏览器进行浏览,

自动识别是指将TextView中的文本连接自动识别出来,这些连接不需要做任何的标记,实现自动识链接的功能需要设置<TextView> 标签的andeoid:autoLink属性,
下面是该属性可设置的值得列表:
AutoLink的值属性值
功能描述
None
不匹配任何链接
Web
匹配web网址
Email
匹配email网址
Phone
匹配电话号码
Map
匹配映射地址
All
匹配所有的链接
如果不设置<TextVIew> 标签的android:autolink属性值就需要使用HTML的<a>标签来显示可单击的链接,如果通过XML布局文件来设置TextView中的值,可以直接在文本中用<a> 标签制定链接及链接文本;如果使用java代码来设置,需要使用android:text.html类的fromHtml方法进行转换,代码:
TextView tv = (TextView) this.findViewById(R.id.haozi); String haozi="<font size='30' color='#00ff00'>我们的爱过了就</font>就不再回来"+"<br><ahref='www.haozi.com'>haozi</a>";
tv.setText(Html.fromHtml(haozi));
4、带边框的TextView、
该组件本身并不支持边框,但是可以对TextView进行扩展来添加边框,可以使用下面两种方法为TextView组件添加边框,
1. 编写一个继承TextView的类的自定义组件,并在onDraw事件方法中画边框
2. 使用android自带的9-patch工具处理过得9-patch格式的图像作为背景图来设置边框,

在onDraw方法中画边框的代码实现:

public class Myborder extends TextView {
//必须覆盖带两个参数的构造
public Myborder(Context context, AttributeSet attrs) {
super(context, attrs);

}
//覆盖父类的方法
public void onDraw(Canvas canvas){
super.onDraw(canvas);

//创建画刷
Paintpaint = 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);

}

使用图片来做背景图的方法:
首先准备好做好带边框的png图片然后用draw9patch工具打开,在图片上边框和左边框的中点画一个像素点保存即可使用,



输入文本的组件 EditText;
EditText是TextView的子类,因此组件具有TextView组件的一切XML方法和属性;
EditText与TextView的区别是EditText组件可以输入文本,而TextView只能显示文本;
下面首先回顾一下前面已经学过的EditView的一些基本用法;
<EditText
android:layout_width="70dp"
android:layout_height="35dp"
android:id="@+id/code_text"
android:layout_toRightOf="@+id/code"

/>
下面我们介绍一下EditText中输入特定字符的用法;

1、 EditView可以通过多种方式指定允许输入的字符,

可以限定为password。 Number等很多种
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:digits="01234" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:digits="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"/>

2、按回车键显示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;
}

按回车运行前:



按回车运行后



3,自动完成输入内容的组件

AutoCompleteTextView

MultiCompleteTextView

实现代码:

public class AutoTestActivity extends Activity {

AutoCompleteTextView autoTx = null;
MultiAutoCompleteTextView mautoTx = null;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.auto);
findViews();

String[] str = {"aaa","fff","bbb","ccc","nnnn"};

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,str);

autoTx.setAdapter(adapter);
//------------------------
mautoTx.setAdapter(adapter);

mautoTx.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

}

private void findViews(){

autoTx = (AutoCompleteTextView) findViewById(R.id.autoTx);
mautoTx = (MultiAutoCompleteTextView) findViewById(R.id.mautoTx);
}

}

Auto.XML文件:
<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"
/>

运行效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: