Android入門基礎知識總結之常用控制元件篇
臨近考試,做一下簡單的總結,鞏固一下;
不能面面俱到,舉的都是一些常用的基礎例子;
二、常用控制元件篇
本文介紹了TextView
,EditText
,Button
,RadioButton
,CheckBox
,ImageView
,Dialog
等常用互動控制元件;
TextView(文字框)
TextView文字框是個非常常用的控制元件,用於顯示一些文字資訊,繼承於View
;
常用屬性
控制元件屬性 | 功能描述 | 舉例或取值 |
---|---|---|
android:text | 設定文字內容;推薦使用@string/ 取值; |
android:text=“demo” |
android:textSize | 設定文字大小,推薦使用sp 作為單位; |
android:textSize=“40sp” |
android:textStyle | 設定文字的樣式; | normal(普通)、bold(加粗)、italic(傾斜) |
android:textColor | 設定文字顏色; | android:textStyle="#DECCDF" |
android:gravity | 文字顯示佈局位置,可以使用| 設定多個值; |
center、center_vertical、center_horizontal |
android:maxLength | 設定文字最大長度; | android:maxLength=“10” |
android:maxLines | 設定文字最大行數; | android:maxLines=“2” |
… | … | … |
EditText(編輯框)
EditText文字編輯框用於接收使用者輸入資訊,繼承於TextView
;
除了TextView的屬性之外,還有一些常用屬性如下表:
常用屬性
控制元件屬性 | 功能描述 | 舉例或取值 |
---|---|---|
android:hint | 設定沒有輸入時的提示文字內容,當輸入時提示文字消失; | android:hint=“請輸入密碼” |
android:inputType | 限定輸入格式; | none、textPassword、date |
android:scrollHorizontally | 設定文字超出寬度後,是否出現橫拉條 | true、false |
android:singleLine | 設定單行顯示 | true、false |
… | … | … |
隱藏下劃線
EditText
預設是有輸入的下劃線,如果想要隱藏,可以設定
android:background="@null"
即可;
Button(按鈕)
Button按鈕是開發中必不可少的一個控制元件,繼承於TextView;
Button可以響應使用者的點選事件,是程式更加流暢完整;
常用的Button點選事件監聽響應有3種:
常用點選事件監聽方法
1.onClick屬性
在xml佈局檔案中定義Button的時候,可以設定android:onClick
屬性,它的值是一個字串,對應Activity要定義相同名稱的方法,否則找不到繫結的點選事件;
定義Button
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"
android:onClick="btn1_click"
/>
定義方法
/**
* 方法名一定要和對應Button按鈕的android:onClick的值相同
*/
public void btn1_click(View view) {
Toast.makeText(this,"btn1被點選",Toast.LENGTH_SHORT).show();
}
這樣Android系統就會找到相應的按鈕點選響應方法,觸發點選事件;
2.匿名內部類
除了設定onClick
屬性,我們還可以使用匿名內部類的方法來作為監聽器,實現對點選事件的監聽;
定義Button
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2"
/>
初始化Button,並點新增點選事件監聽
在Activity
裡
/*
* 定義一個Button
*/
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_layout);
/*
* 與控制元件繫結
*/
btn2 = findViewById(R.id.btn2);
/*
* 使用匿名內部類實現點選事件
*/
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "onClick: btn2被點選");
}
});
}
兩種方式都適合按鈕較少的情況,匿名內部類的方式更加常用;
但當按鈕比較多的時候,為每個按鈕都使用匿名內部類就顯得麻煩了,為了解決這個問題,我們可以使用第三種點選事件;
3.當前Activity實現OnClickListener介面
定義Button
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"
/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn1"
android:text="@string/btn2"
/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn2"
android:text="@string/btn3"
/>
實現OnClickListener介面
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_layout);
}
/**
* 重寫OnClickListener的onClick方法
* @param v
*/
@Override
public void onClick(View v) {
}
}
初始化Button,並新增點選事件監聽
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn1;
private Button btn2;
private Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_layout);
init();
}
/*
* 自定義初始化函式
*/
private void init(){
/*
* 與控制元件繫結
*/
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
/*
* 設定監聽器
* 因為Activity實現了OnClickListener介面,所以這裡this表示為OnClickListener的引用
*/
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
/**
* 重寫OnClickListener的onClick方法
* @param v
*/
@Override
public void onClick(View v) {
/*
* switch case語句根據ID來判斷哪個按鈕被點選
*/
switch (v.getId()){
case R.id.btn1:
Toast.makeText(this,"btn1被點選",Toast.LENGTH_SHORT).show();
break;
case R.id.btn2:
Toast.makeText(this,"btn2被點選",Toast.LENGTH_SHORT).show();
break;
case R.id.btn3:
Toast.makeText(this,"btn3被點選",Toast.LENGTH_SHORT).show();
break;
}
}
}
三種方式各有優劣,比較常用的是匿名內部類和實現OnClickListener介面;
還有一種是建立內部類的方式,這個不太常用,有興趣可以自行了解;
RadioButton(單選按鈕)
在開發設計中,我們經常會用到單選選項,這時就要用到RadioButton
了;
它是單選按鈕,需要和RadioGroup
配合使用,一個RadioButton
組可以容納多個RadioButton
按鈕,並把它們組合在一起,來實現單選狀態;
定義一個性別單選框
<RadioGroup
android:id="@+id/group1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/btn_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/man"
/>
<RadioButton
android:id="@+id/btn_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/woman"
/>
</RadioGroup>
常用屬性設定
1.排列方向
單選組預設是垂直排列的,如果想改變排列方向,可以設定android:orientation
屬性
水平排列:horizontal
垂直排列:vertical
<RadioGroup
android:id="@+id/group1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/relayout1"
android:orientation="horizontal"
>
2.預設選中
如果想新增預設選中狀態,可以使用android:checked
屬性;
比如預設選中男
,則可以將android:checked
設定成true
即可;
<RadioButton
android:id="@+id/btn_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/man"
/>
單選框點選事件的監聽
有時候我們需要監聽單選框的選擇狀態來響應不同的操作,那麼就可以使用RadioGroup.OnCheckedChangeListener
介面,使用方法類似於Button按鈕;
這裡我們也推薦使用匿名內部類的方式;
在Activity裡
/*
* 定義一個RadioGroup
*/
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_layout);
/*
* 與控制元件繫結
*/
radioGroup = findViewById(R.id.group1);
/*
* 使用匿名內部類實現點選事件
*/
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
/*
* 根據Id判斷選擇的結果
*/
if (checkedId == R.id.btn_man) {
textView.setText("選擇結果是:男");
} else {
textView.setText("選擇結果是:女");
}
}
});
}
CheckBox(複選框)
說完了單選框,接下來就要說一說複選框了;
CheckBox
複選框也是在我們開發時經常使用的一個控制元件,它繼承於CompoundButton
,而CompoundButton
又繼承Button
;
舉個簡單例子,介紹CheckBox
的基本的定義、屬性以及點選事件的監聽;
佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/all"
/>
<RelativeLayout
android:id="@+id/relayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox1"
>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/apple"
/>
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox2"
android:text="@string/banana"
/>
<CheckBox
android:id="@+id/checkbox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox3"
android:text="@string/pear"
/>
</RelativeLayout>
</RelativeLayout>
效果如下
Activity檔案
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
/**
* 實現CompoundButton.OnCheckedChangeListener介面
*/
public class MainActivity extends AppCompatActivity implements
CompoundButton.OnCheckedChangeListener{
private static final String TAG = "MainActivity";
/*
* 定義控制元件
*/
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox checkBox3;
private CheckBox checkBox4;
private RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_layout);
init();
}
/*
* 自定義初始化函式
*/
private void init(){
/*
* 繫結控制元件
*/
checkBox1 = findViewById(R.id.checkbox1);
checkBox2 = findViewById(R.id.checkbox2);
checkBox3 = findViewById(R.id.checkbox3);
checkBox4 = findViewById(R.id.checkbox4);
/**
* 這個為包裹2、3、4複選框的父佈局,方便後面遍歷
*/
relativeLayout = findViewById(R.id.relayout1);
/*
* 為複選框設定監聽器
*/
checkBox1.setOnCheckedChangeListener(this);
checkBox2.setOnCheckedChangeListener(this);
checkBox3.setOnCheckedChangeListener(this);
checkBox4.setOnCheckedChangeListener(this);
}
/*
* 重寫onCheckedChanged方法
* @param buttonView
* @param isChecked
*/
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.checkbox1:
if (isChecked) {
if (!isAllChecked()){
setAllCheck();
}
} else {
if (isAllChecked()) {
removeAllChecked();
}
}
break;
case R.id.checkbox2:
if (isAllChecked() && !checkBox1.isChecked()){
checkBox1.setChecked(true);
} else if(!isAllChecked() && checkBox1.isChecked()){
checkBox1.setChecked(false);
}
break;
case R.id.checkbox3:
if (isAllChecked() && !checkBox1.isChecked()){
checkBox1.setChecked(true);
} else if(!isAllChecked() && checkBox1.isChecked()){
checkBox1.setChecked(false);
}
break;
case R.id.checkbox4:
if (isAllChecked() && !checkBox1.isChecked()){
checkBox1.setChecked(true);
} else if(!isAllChecked() && checkBox1.isChecked()){
checkBox1.setChecked(false);
}
break;
default:
break;
}
}
/*
* 自定義函式,判斷是否全被選中
* @return
*/
private boolean isAllChecked(){
/**
* 獲得當前佈局裡所有子控制元件的個數
*/
int n = relativeLayout.getChildCount();
/**
* 遍歷這3個複選框,檢視狀態
*/
for (int i = 0; i < n; i++