1.常用UI组件
布局文件里可以用属性设置相应组件的属性值;Java端也可以使用组件的方法设置。
①dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。
②px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。
③pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用.
④sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。
TextView (文本标签)
作用:显示文本内容;很少使用交互
id指定id
width指定宽度
height指定高度
text指定文本内容
textcolo指定文本颜色
textsize指定字体大小
RadioGroup 单选择框组
RadioButton 单选择框
创建布局部分代码显示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
EditText (编辑框)
Android页面内容输入(类似于web中的表单)
代码如下:
<EditText
Android:id="@+id/et_test1_number"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:hint=’“请输入手机号”
Android:inputType=“phone”>
Button(普通按钮)
代码如下:
<Android:id="@+id/btu"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“按钮”
ImageView
显示图片(HTML img)
代码如下:
<android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:src="@drawable/ic_launcher_forground"//设置前景图片
android:background_height="@drawable/ic_launcher_background"//设置背景图片
CheckBox
页面多选按钮,:题选择;爱好。多个选项使用。
代码如下:
<checkBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="篮球"
android:checked="true"/> //默认是否勾选
RadioButton (单选按钮)
必须放到一个按钮组中
代码如下:
<RadioButton
android:layout_width="wrap_parent"
android:layout_height="wrap_content"
android:cheached="true"
android:text="男" />
</RadioButton>
可选参数如下:
文本类型,多为大写、小写和数字符号:
android:inputType=“none”
android:inputType=“text”
android:inputType=“textCapCharacters”
android:inputType=“textCapWords”
android:inputType=“textCapSentences”
android:inputType=“textAutoCorrect”
android:inputType=“textAutoComplete”
android:inputType=“textMultiLine”
android:inputType=“textImeMultiLine”
android:inputType=“textNoSuggestions”
android:inputType=“textUri”
android:inputType=“textEmailAddress”
android:inputType=“textEmailSubject”
android:inputType=“textShortMessage”
android:inputType=“textLongMessage”
android:inputType=“textPersonName”
android:inputType=“textPostalAddress”
android:inputType=“textPassword”
android:inputType=“textVisiblePassword”
android:inputType=“textWebEditText”
android:inputType=“textFilter”
android:inputType=“textPhonetic”
数值类型:
android:inputType=“number”
android:inputType=“numberSigned”
android:inputType=“numberDecimal”
android:inputType=“phone”//拨号键盘
android:inputType=“datetime”
android:inputType=“date”//日期键盘
android:inputType=“time”//时间键盘
6.3简述RadioGroup和RadioButton的使用
在项目中有身份选择的选项,需要用到RadioGroup和RadioButton。这里使用了layout_gravity让RadioGroup居中显示,使用了orientation属性,horizontal让内部的RadioButton呈水平摆放,vertical就是垂直摆放。
代码如下:
<RadioButton
android:id="@+id/admin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/admin"/>
<RadioButton
android:id="@+id/tech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/tech"/>
<RadioButton
android:id="@+id/market"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/market"/>
<RadioButton
android:id="@+id/guest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/guest"/>
</RadioGroup>
<RadioButton
android:id="@+id/admin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/admin"/>
<RadioButton
android:id="@+id/tech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/tech"/>
<RadioButton
android:id="@+id/market"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/market"/>
<RadioButton
android:id="@+id/guest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/guest"/>
</RadioGroup>
RadioGroup的监听:
mRadioGroup.setOnCheckedChangeListener(new CheckListener());
class CheckListener implements RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.admin:
//执行具体操作
break;
case R.id.tech:
//执行具体操作
break;
case R.id.market:
//执行具体操作
break;
case R.id.guest:
//执行具体操作
break;
default:
break;
}
}
}