# Android基本UI控件
## *TextView 文本框*
### TextView常用用法
| 主要方法 | 功能描述 |
| :----------: | :--------------------: |
| getText | 获得TextView对象的文本 |
| setText | 设置TextView对象的文本 |
| setTextColor | 设置文本显示的颜色 |
```Java
private TextView textView;
textView = (TextView) findViewById(R.id.textView);
textView.setText("文本内容");
textView.setTextColor(Color.rgb(255,255,255)); //颜色的RGB值
textView.getText().toString();
```
### TextView标签属性
| 属性名称 | 描述 |
| :---------------: | :----------: |
| android:text | 设置显示文本 |
| android:textColor | 设置文本颜色 |
| android:textSize | 设置文本大小 |
```xml
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本内容"
android:textColor="#000"
android:textSize="20sp"
/>
```
---
## *EditText 编辑框*
### EditText常用方法
| 方法 | 功能描述 |
| :------: | :------------------------: |
| getText | 得到EditText对象的文本 |
| setText | 设置文本内容 |
| setHints | 设置文本框为空时显示的文本 |
| getHints | 获取文本框为空时显示的文本 |
```java
private EditText editText;
editText = (EditText) findViewById(R.id.editText);
String text = editText.getText().toString();
editText.setText("文本内容");
editText.setHint("设置文本框为空时显示的文本");
editText.getText();
```
### EditText标签属性
| 属性名称 | 描述 |
| :---------------: | :------------------------: |
| android:textColor | 设置文本颜色 |
| android:textSize | 设置文本大小 |
| android:hint | 设置文本框为空时显示的文本 |
| android:inputType | 限定文本框输入类型 |
```xml
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:hint="设置文本框为空时显示的文本"
android:inputType="number"
/>
```
---
## *ImageView 图片视图*
### ImageView常用方法
| 方法 | 功能描述 |
| :--------------: | :-------------------------------------------: |
| getDrawable | 返回视图的可绘制对象 |
| setImageBitmap | 设置位图为该ImageView内容 |
| setImageResource | 通过资源ID设置可绘制对象为ImageView显示的内容 |
| setAlpha | 设置透明度 |
```Java
private ImageView imageView;
private Bitmap bitmap;
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setAlpha(80); //设置透明度为80%
Drawable drawable = imageView.getDrawable(); //得到drawable对象
imageView.setImageResource(R.drawable.ic_launcher_background);
imageView.setImageBitmap(bitmap);
```
### ImageView标签属性
| 属性名称 | 描述 |
| :---------: | :-------------------------------: |
| android:src | 设置可绘制对象为ImageView显示内容 |
```xml
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"
/>
```
---
## *Button 按钮*
### Button常用方法
略
### Button标签属性
| 属性 | 描述 |
| :---------------: | :----------------------: |
| android:text | 设置按钮文本内容 |
| android:hint | 设置按钮为空时显示的文本 |
| android:textColor | 设置按钮文本颜色 |
| android:textSize | 设置按钮文本大小 |
| android:enabled | 设置按钮是否打开 |
```xml
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮文本"
android:textColor="#000"
android:hint="按钮为空时显示的文本"
android:textSize="20sp"
android:enabled="true"
/>
```
---
## *CheckBox 复选框*
### CheckBox 常用方法
| 方法 | 功能描述 |
| :--------: | :--------------: |
| isChecked | 判断组件是否勾选 |
| setChecked | 设置组件状态 |
```java
private CheckBox checkBox;
checkBox = (CheckBox) findViewById(R.id.checkBox);
Boolean isChecked = checkBox.isChecked(); //判断CheckBox是否选中
checkBox.setChecked(true); //设置CheckBox为选中状态
```
### CheckBox标签属性
| 属性 | 描述 |
| :-------------: | :--------------: |
| android:checked | 设置CheckBox状态 |
```xml
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
/>
```
---
## *RadioGroup和RadioButton 单项选择按钮*
### RadioGroup常用方法
| 方法 | 功能描述 |
| :--------: | :------------------: |
| checked | 单选按钮组的勾选状态 |
| chearCheck | 清除当前选择状态 |
```java
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
private RadioGroup radioGroup;
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb3 = (RadioButton) findViewById(R.id.rb3);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.check(R.id.rb1); //选中id为rb1的单选框
radioGroup.clearCheck(); //清除当前选中状态
```
### RadioGroup标签属性
略
```xml
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
```
---
## *ToggleButton 开关按钮*
### ToggleButton常用方法
| 方法 | 功能描述 |
| :--------: | :------------------: |
| getTextoff | 获取失效状态下的文本 |
| getTextOn | 获取有效状态下的文本 |
| setChecked | 设置开关状态 |
| setTextOff | 设置失效状态下的文本 |
| setTextOn | 设置有效状态下的文本 |
```java
private ToggleButton toggleButton;
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
String textOff = toggleButton.getTextOff().toString(); //获取失效状态下的文本
String textOn = toggleButton.getTextOn().toString();
toggleButton.setTextOff("失效状态下的文本");
toggleButton.setTextOn("有效状态下的文本");
toggleButton.setChecked(true); //设置开关为打开状态
```
### ToggleButton标签属性
| 属性名称 | 描述 |
| :-------------: | :--------------: |
| android:textOff | 未选中时按钮文本 |
| android:textOn | 选中时按钮文本 |
| android:checked | 设置按钮状态 |
```xml
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="选中时按钮文本"
android:textOff="未选中时按钮文本"
android:checked="false"
/>
```
---
## *SeekBar 拖动条*
### SeekBar 常用方法
| 方法 | 功能描述 |
| :---------: | :------------: |
| getMax | 获取范围最大值 |
| getProgress | 获取当前进度值 |
| setMax | 设置范围最大值 |
```Java
private SeekBar seekBar;
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setMax(100); //设置拖动条最大值为100
seekBar.getMax(); //得到当前拖动条的最大值
seekBar.getProgress(); //获取当前进度值
```
### SeekBar标签属性
| 属性名称 | 描述 |
| :--------------: | :------------: |
| android:max | 设置范围最大值 |
| android:progress | 设置当前进度值 |
```xml
android:id="@+id/seekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="250"
android:progress="100"
/>
```
---
## *ProgressBar 进度条*
### ProgressBar 常用方法
| 方法 | 功能描述 |
| :---------: | :------------: |
| getMax | 获取范围最大值 |
| getProgress | 获取当前进度值 |
```java
private ProgressBar progressBar;
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.getMax();
progressBar.getProgress(); //获取当前进度
```
### ProgressBar 标签属性
| 属性名称 | 描述 |
| :--------------: | :--------------------: |
| android:max | 设置进度条最大值 |
| android:progress | 设置进度条的默认进度 |
| style | 进度条样式(详情见下表) |
| 进度条样式 | 描述 |
| :----------------------------------------------: | :--------------: |
| style="?android:attr/progressBarStyleHorizontal" | 水平长形进度条 |
| style="?android:attr/progressBarStyleLarge" | 超大号圆形进度条 |
| style="?android:attr/progressBarStyleSmall" | 标准圆形进度条 |
| style="?android:attr/progressBarStyleSmallTitle" | 标题型圆形进度条 |
```xml
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="250"
android:progress="100"
style="?android:attr/progressBarStyleHorizontal"
/>
```
---
一键复制
编辑
Web IDE
原始数据
按行查看
历史