文章目錄
-
- Android快速入門三步
- 布局介紹
-
- LinearLayout布局屬性
- 代碼示例
- 基礎控件
-
- TextView和EditText
-
- 使用TextView與EditText
- ImageView
-
- ImageView屬性
- 使用ImageView
- Button與ImageButton
-
- Button與ImageButton的使用
- RadioButton和CheckBox
- 四種按鈕響應方式
- onClick事件
-
- 監聽事件實作方式-内部類實作接口
- 監聽事件實作方式-匿名内部類方式
- 監聽事件實作方式-外部類方式
- 電話本小例子
-
- 界面設計
- 完成打電話功能
- 完成短信發送功能
- SmsManager
Android快速入門三步
- 畫界面-操作的是資源(布局 字元串 控件等)
- 響應界面(寫代碼)-操作的是Android封裝的類對象
- 打包運作(編譯代碼生成程式)-SDK工具包
布局介紹
- 當界面有多個控件時,需要按照需求将他們擺放
- 布局就是用來安排他們内部控件所在位置
- 這時布局也稱為容器,可以稱為父控件
- 布局内部的控件被稱為布局的子控件
- 一個布局也可以稱為另一個布局的子控件
Android基礎布局
- LinearLayout(線性布局) 将内容按照一列或者一行擺放
- RelativeLayout(相對) 子控件互相依賴而決定位置
- FrameLayout(幀布局)預設不做加工
- TableLayout(表格) 包含若幹列若幹行組成
- GridLayout(網格) 如同單元格一樣
- ConstraintLayout(限制布局)
對于安卓開發來說,以上的所有布局都需要能熟練掌握,但是對于安卓逆向來說,隻需要會最簡單的LinearLayout就足夠了。
LinearLayout布局屬性
設定方向
android:orientation="horizontal" 設定方向,horizontal代表水準,vertical代表垂直,預設值horizontal
設定寬度和高度
屬性:layout_width="wrap_content" 内容多大就是多大 match_parent 與父控件一緻
屬性:layout_height="wrap_content" 内容多大就是多大 match_parent 與父控件一緻
設定控件位置
android:gravity="right"
設定自身在父布局的權重
android:layout_weight="1"
内邊距和外邊距
padding指的是内邊距:控件内部内容與控件的邊距
margin指的是外邊距 控件與子控件的邊距
代碼示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:text="開始遊戲"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn2"
android:text="遊戲設定"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn3"
android:text="結束遊戲"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
實際效果如圖:

基礎控件
TextView和EditText
TextView常見屬性
屬性 | 說明 |
---|---|
android:id | 控件的ID |
android:layout_width | 控件寬度 |
android:layout_height | 控件的高度 |
android:text | 文本内容 |
android:textSize | 文本大小 |
android:textColor | 文本顔色 |
android:background | 控件背景 |
EditText常見屬性
屬性 | 說明 |
---|---|
android:id | 控件的ID |
android:layout_width | 控件寬度 |
android:layout_height | 控件的高度 |
android:text | 文本内容 |
android:textSize | 文本大小 |
android:textColor | 文本顔色 |
android:background | 控件背景 |
android:hint | 輸入提示的文本 |
android:inputType | 輸入文本類型 |
使用TextView與EditText
- 建立一個工程
- 删除預設生成的activety_main.xml中的内容
- 新添加一個layout
- 新添加一個TextView和EditText并設定屬性,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_margin="5dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="牛人:"
android:textSize="18dp" />
<EditText
android:id="@+id/edt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="請在此輸入您的大名"/>
</LinearLayout>
- 将入口類中預設加載的layout修改為建立的layout
實際效果如圖:
ImageView
ImageView屬性
屬性 | 說明 |
---|---|
android:src="@drawable/ic_launcher" | ImageView的内容圖像 |
android:background="@drawable/ic_launcher" | ImageView背景圖檔 |
android:background="#0000ff" | ImageView的RBG顔色 |
使用ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"
android:background="@drawable/3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Button與ImageButton
Button與ImageButton特征
共有特征:都可以作為一個按鈕産生點選事件
不同點:Button有text屬性,ImageButton沒有;ImageButton有src屬性,Button沒有
Button與ImageButton的使用
<Button
android:id="@+id/btn1"
android:text="我是一個按鈕"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageButton
android:src="@drawable/1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
RadioButton和CheckBox
代碼示例:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rbtn1"
android:text="單選框1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rbtn2"
android:text="單選框2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<CheckBox
android:id="@+id/cbtn1"
android:text="複選框"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
響應事件
RadioButton rbtn1=findViewById(R.id.rbtn1);
//rbtn1.setChecked(true);
rbtn1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(MainActivity.this,"單選框改變",Toast.LENGTH_LONG).show();
}
});
CheckBox chbox=findViewById(R.id.cbtn1);
chbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(MainActivity.this,"複選框改變",Toast.LENGTH_LONG).show();
}
四種按鈕響應方式
了解了基礎控件的使用,接下來還需要掌握四種按鈕的響應事件
onClick事件
<Button
android:id="@+id/btn1"
android:onClick="onClick1"
android:text="我是一個按鈕"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
添加一個onClick屬性,綁定onClick1方法
按Alt+Enter鍵
會自動在MainAvtivity中建立響應事件,在函數裡面編寫響應代碼即可
監聽事件實作方式-内部類實作接口
首先建立按鈕控件
<Button
android:id="@+id/btn2"
android:text="按鈕2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
接着在入口類中編寫代碼,首先擷取按鈕對象
然後設定監聽事件
//1.2 設定監聽事件
//參數是一個接口 傳入的參數應該是實作了這個接口的對象
btn2.setOnClickListener();
由于setOnClickListener的參數是一個接口,是以我們需要一個實作了這個接口的對象
建立類,實作View.OnClickListener接口
public class MyOnClickListener implements View.OnClickListener{
@Override
public void onClick(View view) {
//這裡是響應代碼
Toast toast=Toast.makeText(MainActivity.this,"按鈕2",Toast.LENGTH_LONG);
}
}
最後傳入實作了接口的對象
監聽事件實作方式-匿名内部類方式
首先擷取控件對象
接着設定監聽事件
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast toast=Toast.makeText(MainActivity.this,"按鈕3",Toast.LENGTH_LONG);
toast.show();
}
});
由于監聽事件為匿名類,是以可以直接在括号内寫實作代碼
監聽事件實作方式-外部類方式
擷取按鈕對象并設定響應事件
//3.外部類方式
Button btn4=findViewById(R.id.btn4);
btn4.setOnClickListener(this);
然後重寫Onclick方法
@Override
public void onClick(View view) {
}
即可綁定按鈕事件
電話本小例子
界面設計
接着我們通過一個電話本的例子,深刻了解整個安卓開發過程。首先設定打電話的一個文本框和按鈕
//文本框
<EditText
android:id="@+id/edit1"
android:inputType="phone"
android:hint="請在此輸入電話号碼"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn1"
android:text="打電話"
android:onClick="OnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
接着設定發短信的文本框和按鈕
<EditText
android:id="@+id/edit2"
android:inputType="textMultiLine"
android:minLines="6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn2"
android:text="發短信"
android:onClick="OnSendMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
并分别給兩個按鈕綁定OnClick事件,界面效果如圖:
完成打電話功能
實作思路:
接着完成打電話的OnClick函數。程式本身無法做到打電話這個功能,需要向系統送出請求。android中與其他應用打交道,需要用到Intent。打電話這個操作,本質上就是與系統中的電話應用進行通信。
我們需要做的就是使用Intent對象完成打電話這個動作,并且将電話号碼發送給電話應用,具體步驟如下:
- 建立一個Intent對象
- 設定打電話動作
- 打包資料,放入Intent
- 根據Intent對象啟動對應的Activety
代碼如下:
public void OnCall(View view) {
//擷取電話号碼
EditText text=findViewById(R.id.edit1);
String phonenum=text.getText().toString();
//建立Intent對象
Intent intent=new Intent();
//設定打電話動作
intent.setAction(Intent.ACTION_CALL);
//打包資料 放入Intent
Uri uri=Uri.parse("tel:"+phonenum);
intent.setData(uri);
//根據Intent啟動Activity
startActivity(intent);
}
接着在Manifest.xml檔案中加上電話權限相關代碼
這樣就完成了打電話的完整功能。
完成短信發送功能
實作思路:
編寫短信發送和電話撥号有所不同,Android SDK提供了短信發送器用于操作短信
SmsManager類用于管理發送短信。
有了這個知識之後,我們可以把短信發送器分為幾步:
- 設定按鈕監聽器,在監聽器中調用發短信方法
- 發短信分為
- 擷取号碼和文本内容
- 擷取短信管理器對象
- 拆分文本内容
- 發送短信
需要注意的是,發送短信也需要權限
SmsManager
SmsManager用于管理短信,發送短信,擷取這個類的對象需要調用類的靜态成員方法getDefault()
SmsManager類中其他幾個方法:
方法名 | 功能 |
---|---|
devideMessage(String text) | 拆分短信,一條短信最大70個中文 |
getDefault() | 擷取短信管理器對象 |
sendMultipartTextMessage | 發送多條短信 |
sendTextMessage | 發送短信 |
完成短信發送步驟如下:
- 首先擷取電話号碼和短信内容
//擷取電話号碼
EditText text=findViewById(R.id.edit1);
String phonenum=text.getText().toString();
//擷取短信内容
EditText text2=findViewById(R.id.edit2);
String content=text2.getText().toString();
- 擷取SMS管理器對象
//擷取SMS管理器對象
SmsManager sms=SmsManager.getDefault();
- 拆分短信
//拆分短信
ArrayList<String> str=sms.divideMessage(content);
- 發送短信
//發送短信
sms.sendMultipartTextMessage(phonenum,null,str,null,null);
- 擷取權限
這樣就完成了發短信的功能了,實際效果如圖: