Android widget 组件(一) Button、 EditText、 TextView
来源:互联网 发布:苹果7连不上4g网络吗 编辑:程序博客网 时间:2024/06/07 05:03
Button
Android中的Button组件,最常使用的Widget组件之一。
Layout声明:
<Buttonandroid: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":隐藏组件。
常量值:invisible、visible、gone.只能指定其一。
在java文件中使用并为Button指定处理的事件:
Button btnSate =(Button)findViewById(R.Id.btnOK); //获取Button组件。
btnSate.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
//TODO:事件需要片的逻辑代码。
}
});
当然,也可以指定多个OnClickListener或其他类型的事件,那就看看是否需要了。
以上几个代码也可以写成以下格式:
findViewById(R.Id.btnOK).setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
//TODO:事件需要片的逻辑代码。
}
});
EditText
Android中的EditText数据编辑框,相当于C#中的TextBox。刚开始接触时容易和TextView组件混淆,分不清那个是显示,那个是编辑,至少我就犯了这个错误。
Layout声明:
<EditTextandroid: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:与右侧空白大小。当然,还是paddingLeft、paddingTop、paddingButton和paddingAll。
除了上面这些,如果要指定EditText为Password输入框,可用android:password=”true”进行声明。
另外还有在用户输入体验上的标签,不会影响程序的运行,但会给用户输入带来很好的体验,如只需要输入电话号码或都数字等。
android:phoneNumber="true":系统只会打开电话输键盘(电话号码不可用键不会显示.
android:numeric="integer|signed|decimal":系统只会打开数据键盘(三值可选一)。
TextView
TextView,用于显示标签文字。
<TextViewandroid: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();//获取显示的文字。
EditText、TextView之扩展 Linkify.
Linkify可以让系统动态获取并判断用户输入的数据规则类型,如电话号码、Email地址、 url地址。代码示例:
android.text.util.Linkify.addLinks(txtNumber, Linkify.PHONE_NUMBERS); //txtNumber:EditText类型
当用户输入的数据类型符合电话号码规则时,系统为自动为数据的内容加上链接,当点击链接时为自动调用系统的拨号程序进行拨号。
加外还有:
l Linkify.EMAIL_ADDRESSES //判断是否email点击链接打开email发送程序。
l Linkify.MAP_ADDRESSES //判断是否url点击打开系统浏览器并打开网页。
当然,也可以同时使用。如
android.text.util.Linkify.addLinks(txtNumber, Linkify.PHONE_NUMBERS|Linkify.EMAIL_ADDRESSES|Linkify.MAP_ADDRESSES);
系统的SMS程序就使用了Linkify。
- Android widget 组件(一) Button、 EditText、 TextView
- Android学习(一)TextView,ImageView,Button,EditText,控件
- 常用的Android Widget组件学习①--Button and TextView
- Android开发8:UI组件TextView,EditText,Button
- android组件 TextView Button
- Android-TextView及其子类(TextView、EditText、Button)
- Android---2---TextView、Button、EditText
- android widget组件之Button
- 基本界面组件~TextView~EditText~Button
- android.widget.textview cannot be casted to android.widget.button
- Android常见的几个控件(TextView,EditText,Button)
- Android的常见控件(TextView、EditText、Button、Menu)使用
- Android学习笔记-EditText&TextView&Button&菜单栏
- Android学习笔记-EditText&TextView&Button&菜单栏
- Android第四讲——常用的基本控件(一)TextView、EditText、Button、RadioButton、CheckButton、ImageView
- 基本的UI组件(TextView、EditText、Button、ImageView、SeekBar)学习
- Android0821<五>(TextView、EditText、Button)
- TextView、Button、EditText
- marquee标签
- Java解析XML之导入dom4j
- 深度学习资料
- Atitti cto的日常流程与职责attilax总结
- 走进 Redis 的世界
- Android widget 组件(一) Button、 EditText、 TextView
- Isomorphic Strings
- Smallest Rectangle Enclosing Black Pixels
- 【Java学习】——SSH总结
- Atitit 我们的devops战略与规划 规范 推荐标准
- 第三方登录
- CSS中行内元素和块级元素区别
- 比酒量
- MySQL无法重启问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored