003 Android之线性布局与基础控件
文章目錄
- Android快速入門三步
- 布局介紹
- LinearLayout布局屬性
- 代碼示例
- 基礎控件
- TextView和EditText
- 使用TextView與EditText
- ImageView
- ImageView屬性
- 使用ImageView
- Button與ImageButton
- Button與ImageButton的使用
- RadioButton和CheckBox
- 四種按鈕響應方式
- onClick事件
- 監聽事件實現方式-內部類實現接口
- 監聽事件實現方式-匿名內部類方式
- 監聽事件實現方式-外部類方式
- 電話本小例子
- 界面設計
- 完成打電話功能
- 完成短信發送功能
- SmsManager
Android快速入門三步
布局介紹
- 當界面有多個控件時,需要按照需求將他們擺放
- 布局就是用來安排他們內部控件所在位置
- 這時布局也稱為容器,可以稱為父控件
- 布局內部的控件被稱為布局的子控件
- 一個布局也可以稱為另一個布局的子控件
Android基礎布局
對于安卓開發來說,以上的所有布局都需要能熟練掌握,但是對于安卓逆向來說,只需要會最簡單的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"><Buttonandroid:id="@+id/btn1"android:text="開始游戲"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn2"android:text="游戲設置"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid: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
實際效果如圖:
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"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:layout_gravity="center"android:src="@mipmap/ic_launcher"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ImageViewandroid: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的使用
<Buttonandroid:id="@+id/btn1"android:text="我是一個按鈕"android:layout_width="match_parent"android:layout_height="wrap_content" /><ImageButtonandroid:src="@drawable/1"android:layout_width="match_parent"android:layout_height="wrap_content"/>RadioButton和CheckBox
代碼示例:
<RadioGroupandroid:layout_width="wrap_content"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/rbtn1"android:text="單選框1"android:layout_width="wrap_content"android:layout_height="wrap_content" /><RadioButtonandroid:id="@+id/rbtn2"android:text="單選框2"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RadioGroup><CheckBoxandroid: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() {@Overridepublic 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() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {Toast.makeText(MainActivity.this,"復選框改變",Toast.LENGTH_LONG).show();}四種按鈕響應方式
了解了基礎控件的使用,接下來還需要掌握四種按鈕的響應事件
onClick事件
<Buttonandroid:id="@+id/btn1"android:onClick="onClick1"android:text="我是一個按鈕"android:layout_width="match_parent"android:layout_height="wrap_content" />添加一個onClick屬性,綁定onClick1方法
按Alt+Enter鍵
會自動在MainAvtivity中創建響應事件,在函數里面編寫響應代碼即可
監聽事件實現方式-內部類實現接口
首先創建按鈕控件
<Buttonandroid:id="@+id/btn2"android:text="按鈕2"android:layout_width="match_parent"android:layout_height="wrap_content" />接著在入口類中編寫代碼,首先獲取按鈕對象
Button btn2=findViewById(R.id.btn2);然后設置監聽事件
//1.2 設置監聽事件//參數是一個接口 傳入的參數應該是實現了這個接口的對象btn2.setOnClickListener();由于setOnClickListener的參數是一個接口,所以我們需要一個實現了這個接口的對象
創建類,實現View.OnClickListener接口
public class MyOnClickListener implements View.OnClickListener{@Overridepublic void onClick(View view) {//這里是響應代碼Toast toast=Toast.makeText(MainActivity.this,"按鈕2",Toast.LENGTH_LONG);}}最后傳入實現了接口的對象
btn2.setOnClickListener(new MyOnClickListener());監聽事件實現方式-匿名內部類方式
首先獲取控件對象
Button btn3=findViewById(R.id.btn3);接著設置監聽事件
btn3.setOnClickListener(new View.OnClickListener() {@Overridepublic 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方法
@Overridepublic void onClick(View view) {}即可綁定按鈕事件
電話本小例子
界面設計
接著我們通過一個電話本的例子,深刻了解整個安卓開發過程。首先設置打電話的一個文本框和按鈕
//文本框<EditTextandroid:id="@+id/edit1"android:inputType="phone"android:hint="請在此輸入電話號碼"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn1"android:text="打電話"android:onClick="OnCall"android:layout_width="match_parent"android:layout_height="wrap_content" />接著設置發短信的文本框和按鈕
<EditTextandroid:id="@+id/edit2"android:inputType="textMultiLine"android:minLines="6"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn2"android:text="發短信"android:onClick="OnSendMsg"android:layout_width="match_parent"android:layout_height="wrap_content" />并分別給兩個按鈕綁定OnClick事件,界面效果如圖:
完成打電話功能
實現思路:
接著完成打電話的OnClick函數。程序本身無法做到打電話這個功能,需要向系統發出請求。android中與其他應用打交道,需要用到Intent。打電話這個操作,本質上就是與系統中的電話應用進行通信。
我們需要做的就是使用Intent對象完成打電話這個動作,并且將電話號碼發送給電話應用,具體步驟如下:
代碼如下:
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);//打包數據 放入IntentUri uri=Uri.parse("tel:"+phonenum);intent.setData(uri);//根據Intent啟動ActivitystartActivity(intent);}接著在Manifest.xml文件中加上電話權限相關代碼
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>這樣就完成了打電話的完整功能。
完成短信發送功能
實現思路:
編寫短信發送和電話撥號有所不同,Android SDK提供了短信發送器用于操作短信
SmsManager類用于管理發送短信。
有了這個知識之后,我們可以把短信發送器分為幾步:
需要注意的是,發送短信也需要權限
SmsManager
SmsManager用于管理短信,發送短信,獲取這個類的對象需要調用類的靜態成員方法getDefault()
SmsManager類中其他幾個方法:
| devideMessage(String text) | 拆分短信,一條短信最大70個中文 |
| getDefault() | 獲取短信管理器對象 |
| sendMultipartTextMessage | 發送多條短信 |
| sendTextMessage | 發送短信 |
完成短信發送步驟如下:
這樣就完成了發短信的功能了,實際效果如圖:
總結
以上是生活随笔為你收集整理的003 Android之线性布局与基础控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 002 Android之hellowor
- 下一篇: 004 Android之其他控件