# 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
原始數據
按行查看
歷史