Android开发之路七----UI组件2
来源:互联网 发布:达尔文进化论假说知乎 编辑:程序博客网 时间:2024/06/11 17:08
Android开发之路七----UI组件2
今天我们继续学习UI组件,主要是学习了TextView和EditText这两种组件
TextView组件介绍:
直接的子类:
Button、CheckdTextView、Cheronometer、DigitalClock、EditText
间接的子类:
AutoCompleteTextView、CheckBox、CommpoundButton、MultiAutoCompleteTextView
在其xml文件中有许多的属性今天讲到例如:
Android:autoLink:这是设置文本为URL链接/email/电话号码时,文本显示为可点击的链接,可选的值为(none/web/email/phone/map这属性今天没讲/all)
Android:autoText:这是如果设置,将自动执行输入值的拼写纠正,在显示输入法并输入的时候起作用。
Android:drawableButton:这是在text中的下方输入一个drawable,图片,如果指定一个颜色的话会把text的背景颜色设为该颜色,并且同时和background使用时覆盖后者。
Android:gravity:设置文本位置,可以设置成“left”“center”“right”对应的分别是左,居中,右。
Android:inputType 设置文本的类型
..................................
下面就介绍一下用TextView组件来实现程序这是三个不同的Helloword,不同之处就是利用了TextView中的标签
<?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:text="@string/hello" />
//原始的helloword
<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" />
//加上颜色--textcolor和大小--textSize
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center"
/>
//设置了文本的居中格式
android:gravity="center"
</LinearLayout>
运行后形成的界面就是这个样子的
、
这就是利用标签形成的文本的不同效果。
为文本添加背景图片
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="10dp"
android:textColor="#cccccc"
android:text="@string/hello"
android:background="@drawable/ic_launcher"
/>
利用上面的这段代码就可以实现为你的文本添加背景图片,虽然这是添加背景图片但是这可能会使图片失真,随意的拉伸图片。这就要求我们很好的掌握photoshop的功夫,要把图片p好,那样就不会失真。
为文本加链接
<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="all"
android:text="@string/autoAll"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview"
/>
所形成的界面是这个样子的
所有的链接就加上了。
自定义带边框的TextView
public class BorderTextView extends TextView {
public BorderTextView(Context context, AttributeSet attr) {
super(context,attr);
}
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);
}
}
<cn.class3g.activity.BorderTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="30dp"
android:text="xxxxxxxxxxxxx"
/>
EditText组件的应用
EditText是TextView的子类,继承了TextView的大部分xml属性,所以用法与TextView的用法大致一致。
其中一些特定的字符串
<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: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" />
//显示文本框
为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" />
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;
}
按下回车键之后就会成这样
- Android开发之路七----UI组件2
- Android开发之路六---UI组件
- Android开发之路八----UI组件
- Android开发之路九------UI组件4
- Android开发之UI组件TextView
- Android应用开发学习笔记之开发自定义UI组件
- Android应用开发之UI组件(TextView;EditText)
- Android开发之UI库及组件资源
- Android开发之UI库及组件资源
- Android开发之UI库及组件资源
- Android开发之UI的开源组件
- Android开发之自定义UI组件和属性
- Android开发自定义UI组件
- Android界面编程之七 第六组UI组件:ViewAnimator及其子类
- android-UI组件实例大全(七)------Adapter类One之ListView
- android-UI组件实例大全(七)------Adapter类One之ListView
- Android UI之TextView组件
- Android UI之CheckBox组件
- EventHandler,预定义的委托
- 23种设计模式汇总-------【摘自易学设计模式】
- 排序算法--插入排序
- 文件的链接
- Hibernate继承映射
- Android开发之路七----UI组件2
- Android SWAP分区理解
- Oracle Golden Gate 系列十一 -- 配置 GG DDL 同步 说明 与 示例
- Hibernate 继承关系映射
- 公司报道,新人体会
- 异常学习笔记ExceptionDemo1.java
- Object-c 根据经度纬度计算两点间的距离
- debian学习笔记4,挂载windows下的ntfs硬盘
- 写sql语句的时候 在access数据库里面设置了一个 note字段,但是在 insert的时候 老是不过。。。