Button

Android中的Button组件,最常使用的Widget组件之一。

Layout声明:

<Button android:text="@string/account_create_btnCancel_title"

android:id="@+id/btnCAccountCancel" android:layout_width="100px"

android:layout_height="wrap_content"

android:layout_alignParentRight="true" android:visibility="gone">

</Button>

注:

android:text="@string/account_create_btnCancel_title"

string.xml中指定显示的文字。也可以直接显示文字。如:

android:text="@This is Button."

android:id="@+id/btnCAccountCancel":组件ID,以便于在Activity中查询引用。如:

(Button)findViewById(R.id.btnCAccountCancel);

Android:layout_width=”100px”与  android:layout_height="wrap_content":

组件的宽度与高度。这是指定的固定大小,也可以指定以下两个值中的之一:

wrap_content:根据内容自动适应大小。此常量也适用于其他widget组件。

fill_parent: 根据父容器大小进行填充。此常量也适用于其他widget组件。

android:layout_alignParentRight="true":在父容器中靠右放置。

android:visibility="gone":隐藏组件。

常量值:invisiblevisiblegone.只能指定其一。

java文件中使用并为Button指定处理的事件:

Button btnSate =(Button)findViewById(R.Id.btnOK); //获取Button组件。

btnSate.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//TODO:事件需要片的逻辑代码。

}

});

当然,也可以指定多个OnClickListener或其他类型的事件,那就看看是否需要了。

以上几个代码也可以写成以下格式:

findViewById(R.Id.btnOK).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//TODO:事件需要片的逻辑代码。

}

});

EditText

Android中的EditText数据编辑框,相当于C#中的TextBox。刚开始接触时容易和TextView组件混淆,分不清那个是显示,那个是编辑,至少我就犯了这个错误。

Layout声明:

<EditText android:id="@+id/txtCAccountName"

android:layout_width="200px" android:paddingRight="@dimen/padding"

android:textSize="18sp"

android:layout_x="14px"

android:layout_y="41px"

android:layout_height="wrap_content">

</EditText>

注:

android:id,不用说了,组件的唯一标识。

android:layout_width\ android:layout_height:宽度与高度,所有的android组件都是这么指定。参见Button说明

android:layout_y\ android:layout_y:待定

android:paddingRight:与右侧空白大小。当然,还是paddingLeftpaddingToppaddingButtonpaddingAll

除了上面这些,如果要指定EditTextPassword输入框,可用android:password=”true”进行声明。

另外还有在用户输入体验上的标签,不会影响程序的运行,但会给用户输入带来很好的体验,如只需要输入电话号码或都数字等。

android:phoneNumber="true":系统只会打开电话输键盘(电话号码不可用键不会显示.

android:numeric="integer|signed|decimal":系统只会打开数据键盘(三值可选一)

TextView

TextView,用于显示标签文字。

<TextView android:id="@+id/TextView01"

android:text="@string/account_create_name_title"

android:layout_width="wrap_content" android:layout_height="wrap_content">

</TextView>

android:text="@string/account_create_name_title":要显示的标签文字。

TextView txtView=(TextView)findViewById(R.id.TextView01);

txtView.setText("设置文字");

txtView.getText(); //获取显示的文字。

EditTextTextView之扩展 Linkify.

Linkify可以让系统动态获取并判断用户输入的数据规则类型,如电话号码、Email地址、 url地址。代码示例:

android.text.util.Linkify.addLinks(txtNumber, Linkify.PHONE_NUMBERS); //txtNumber:EditText类型

当用户输入的数据类型符合电话号码规则时,系统为自动为数据的内容加上链接,当点击链接时为自动调用系统的拨号程序进行拨号。

加外还有:

lLinkify.EMAIL_ADDRESSES//判断是否email点击链接打开email发送程序。

lLinkify.MAP_ADDRESSES //判断是否url点击打开系统浏览器并打开网页。

当然,也可以同时使用。如

android.text.util.Linkify.addLinks(txtNumber, Linkify.PHONE_NUMBERS|Linkify.EMAIL_ADDRESSES|Linkify.MAP_ADDRESSES);

系统的 SMS 程序就使用了 Linkify

更多相关文章

  1. android hover监控鼠标移动事件
  2. 简单的 Android(安卓)拍照并显示以及获取路径后上传
  3. 移动网站开发中常用的10段JavaScript代码
  4. android-----EditText
  5. 『转』Android(安卓)Intent常见应用
  6. xml-----属性收集
  7. android tips:从资源文件中读取文件流显示
  8. Android:如何显示网络图片
  9. Android(安卓)设计一个可单选,多选的Demo

随机推荐

  1. Linux CentOS 配置Tomcat环境
  2. Linux 上samba服务的配置
  3. Linux基础知识总结
  4. linux环境下写C++操作mysql(二)
  5. Linux生成随机字符串
  6. 在VNC远程桌面环境Xfce4中Tab键失效的解
  7. Linux防火墙配置
  8. Shell脚本编写简明教程
  9. 手把手教你在ubuntu下创建桌面快捷方式
  10. 在Linux里设置用户环境变量的方法