1. EditText的功能与用法
EditText可以接受用户输入。其最重要的属性是inputType,用于EditView为指定类型的输入组件。
实例:用户友好的输入界面
Xml代码清单:
<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="16sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写登录账号"
android:selectAllOnFocus="true"
/> "
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="16sp"
/>
<!--android:inputType="numberPassword"表明只能接受数字密码 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="16sp"
/>
<!--android:inputType="number"表明是数值输入框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="生日:"
android:textSize="16sp"
/>
<!-- inputType="date"表明是日期输入框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="date"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textSize="16sp"
/>
<!-- inputType="phone"表明是电话号码输入框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"
/>
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
/>
</TableLayout>
效果图:
2. 按钮(Button)组件
Button继承了TextView,它可供用户点击,触发onClick事件。按钮可通过android:background属性来添加背景颜色和背景图片。也可以使用自定义Drawable对象实现按钮的背景颜色、背景图片随用户动作动态改变。
实例:按钮、圆形按钮、带文字的图片按钮
Xml代码清单:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- 文字带阴影的按钮 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文字带阴影的按钮"
android:textSize="12pt"
android:shadowColor="#aa5"
android:shadowRadius="1"
android:shadowDx="5"
android:shadowDy="5"
/>
<!-- 普通文字按钮 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pp"
android:text="普通按钮"
android:textSize="10pt"
/>
<!-- 带文字的图片按钮 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
android:textSize="11px"
android:text="带文字的图片按钮"
/>
</LinearLayout>
上面的界面布局中第一个为普通按钮,但为该按钮的文字指定了阴影——配置方法与TextView配置阴影方法相同。
第二个按钮通过background属性配置了背景图片,该按钮会显示背景图片形状的按钮。
第三个按钮通过指定android:background属性为@drawable/button_select,该属性引用了一个Drawable资源,该资源对应的xml文件如下:
<?xmlversion="1.0"encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<!-- 指定按钮按下是的图片 -->
<itemandroid:state_pressed="true"
android:drawable="@drawable/red"
></item>
<!-- 指定按钮松开时的图片 -->
<itemandroid:state_pressed="false"
android:drawable="@drawable/purple"
/>
</selector>
效果图:
PS:从上面按钮来看,当按钮内容太多时,android会自动缩放整张图片,以保证背景图片能覆盖整个按钮,使图片效果不好。
可通过只缩放图片部分,保证视觉效果。这要借助9Patch图片实现,它是一种特殊的PNG图片,以.9.png结尾,它在原始图片四周各添加一个宽度为1像素的线条,这四条线条决定了该图片的缩放规则、内容显示规则。
Android为制作9Patch图片提供了draw9patch工具,该工具位于Android SDK安装路径的tools目录下,双击draw9patch.bat文件,可启动该工具。
3. 单选按钮(RadioButton)与复选框(CheckBox)
它们多了一个可选中功能,可额外指定一个android:checked属性。一组RadioButton只能选中其中一个,通常要与RadioGroup一起使用,用于定义一组单选按钮。
实例:利用单选按钮、复选框获取用户信息
Xml代码清单:
<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textColor="#0ff"
android:textColorLink="#ff0"
android:textSize="16px"/>
<!-- 定义一组单选按钮 -->
<RadioGroupandroid:id="@+id/rg"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
>
<!-- 定义两个单选按钮,
android:checked默认勾选-->
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:text="男"
android:checked="true"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/famale"
android:text="女"
/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢的颜色:"
android:textSize="16px"
/>
<!-- 定义一个垂直的线性布局 -->
<LinearLayout
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- 定义三个复选框 -->
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:checked="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色"
/>
</LinearLayout>
</TableRow>
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0f0"
android:textSize="20px"/>
</TableLayout>
为了监听单选按钮、复选框的勾选状态的改变,可以为他们添加事件监听器。下面Activity为RadioGroup添加了事件监听器:
packagecom.hqsA.checkbuttont;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.widget.RadioGroup;
importandroid.widget.RadioGroup.OnCheckedChangeListener;
importandroid.widget.TextView;
public classCheckButtonT extends Activity {
RadioGroup rg;
TextView show;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_button_t);
//获取界面上 rg,show两个主件
rg = (RadioGroup)findViewById(R.id.rg);
show = (TextView)findViewById(R.id.show);
//为RadioGroup组件的OnCheck事件绑定事件监听器
rg.setOnCheckedChangeListener(newOnCheckedChangeListener()
{
@Override
public voidonCheckedChanged(RadioGroup Group,int CheckedId)
{
//根据用户勾选的单选按钮来动态改变tip字符串的值
String tip= CheckedId == R.id.male ?
"先生,您好!" : "女士,您好!";
//修改show组件中的文本
show.setText(tip);
}
});
}
}
效果图: