按钮控件Button

除了文本视图之外,按钮Button也是一种基础控件。因为Button是由TextView派生而来,所以文本视图 拥有的属性和方法,包括文本内容、文本大小、文本颜色等,按钮控件均能使用。不同的是,Button拥 有默认的按钮背景,而TextView默认无背景;Button的内部文本默认居中对齐,而TextView的内部文本 默认靠左对齐。此外,按钮还要额外注意textAllCaps与onClick两个属性,分别介绍如下:

1.textAllCaps属性 对于TextView来说,text属性设置了什么文本,文本视图就显示什么文本。但对于Button来说,不管 text属性设置的是大写字母还是小写字母,按钮控件都默认转成大写字母显示。比如在XML文件中加入 下面的Button标签:

<Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Hello World"/>

编译运行后的App界面,按钮上显示全大写的“HELLO WORLD”,而非原来大小写混合的“Hello World”。显然这个效果不符合预期,为此需要给Button标签补充textAllCaps属性,该属性默认为true表 示全部转为大写,如果设置为false则表示不转为大写。于是在布局文件添加新的Button标签,该标签补 充了android:textAllCaps="false",具体内容如下所示:

<Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Hello World"
 android:textAllCaps="false"/>

2.onClick属性

表示点击该按钮会触发Java代码中的doClick方法

<Button
 android:id="@+id/btn_click_xml"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:onClick="doClick"
 android:text="直接指定点击方法"
 android:textColor="#000000"
 android:textSize="17sp" />
 <TextView
 android:id="@+id/tv_result"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="这里查看按钮的点击结果"
 android:textColor="#000000"
 android:textSize="17sp" />

 

posted on 2024-10-20 18:59  leapss  阅读(23)  评论(0)    收藏  举报