注意:控件顯示一定要有寬高 ,否則就不會顯示 layout_widthlayout_height layout_height
Textview 文本框
id:控件身份標識 android:id="@+id/textView1"
xml:
android:textColor="@android:color/holo_purple"
code:
Button 繼承TextView
事件點擊
xml:
android:onClick="btn"
多個button設置監聽 Activity implement OnclickLisenter
EditText 編輯框
android:hint="提示信息"
android:ems="10" 設置每行的文本長度
<requestFocus /> 獲取焦點
code:
獲得編輯內容
//獲得編輯內容
String string = mEdit.getText().toString().trim();
ImageView
xml:
前景圖片
android:src="@drawable/ic_launcher"
背景圖片
android:background="@drawable/ic_launcher"
android:scaleType="fitXY" 根據控件大小拉伸
code:
ImageButton 繼承ImageView 點擊事件
布局
warp_content 適應內容
march_Parent 填充父類容器 fill_Parent
單位: 尺寸單位:dp 字體:sp
pading : 內容跟控件的距離
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="40dp"
android:paddingBottom="40dp"
margen: 控件跟控件的距離
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
線性布局 linearLayout
android:orientation="vertical" 垂直 "horizontal" 水平
android:layout_weight="1" 權重 瓜分剩余空間
android:gravity="left" 內容在控件中的位置
設置控件在布局中的位置
horizontal: 設置 垂直方向 控件的位置
android:layout_gravity="center_vertical" 布局中垂直居中
android:layout_gravity="bottom" 居于布局底部
android:layout_gravity="top" 居于布局頂部
vertical: 設置 水平方向 控件的位置
android:layout_gravity="center_horizontal"
android:layout_gravity="left" 居于布局左邊
android:layout_gravity="right" 居于布局右邊
相對布局 RelativeLayout
控件跟控件位置關系
android:layout_toRightOf="@+id/button1"
android:layout_toLeftOf="@+id/button1"
android:layout_below="@+id/button1"
android:layout_above="@+id/button1"
控件跟控件之間的對齊 (基準線)
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/button1"
android:layout_alignTop="@+id/button1"
android:layout_alignBottom="@+id/button1"
控件跟布局位置關系
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" 水平居中
android:layout_centerVertical="true" 垂直居中
android:layout_centerInParent="true" 居中
Intent 意圖
連接四大組件之間的橋梁
傳遞數據
顯示意圖: 指明跳轉方向
隱式意圖: 打開符合過濾條件的界面
Activity :用于與用戶進行交互 四大組件之一
創建要素:
1.創建類繼承Acitvity
2.setContentView() 關聯布局
3.在Manifest.xml 清單文件中注冊
界面跳轉:
A-->B
A:
Intent intent=new Intent();
//指明跳轉方向
intent.setClass(this, BActivity.class);
intent.putExtra("name", "name");
startActivity(intent);
B:
Intent intent = getIntent();
String stringExtra = intent.getStringExtra("name");
A--->B--->A
請求碼 區分請求事件
requestCode
結果碼 區分返回的信息
resultCode
Serveice 服務 四大組件之一
沒有界面 長期運行在后臺
應用場景: 下載 音樂播放器
創建:
1.創建類繼承Service
2.在清單文件中進行注冊
生命周期
1.啟動服務方式一
startService
創建到運行
oncreat() ---->onStartCommand()
運行到銷毀 stopService()
onDestroy
注意:
1.通過startService()啟動的服務,只能通過 stopService()停止服務
2.通過startService()啟動的服務,跟Acitivity的關系不密切,即時界面關閉(銷毀)服務還在
3.服務啟動過一次后,在啟動,不會調用oncreat() ,只會調用onStartCommand(),服務只創建一次
自動導包 :ctrl +shift+o
方式二:
綁定服務 binderService
1.綁定
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// 服務綁定失敗時調用
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 服務綁定成功時調用
}
};
2.解綁
創建到運行
oncreat()---->onBind()
運行---->銷毀
onUnBind --->onDestory()
注意
binderService啟動的服務,跟Activity關系密切,界面銷毀,
服務也會銷毀。
通過綁定服務得到服務中的消息
1.在Service中聲明MyBinder類繼承 Binder{}
2.MyBinder類自定義需要調用的方法
3.onBind() 創建一個MyBinder類型的對象并返回它
返回非空的實現IBinder接口的對象
Activity
4.服務綁定成功后
1.在onServiceConnected()獲得onBind()返回的實現IBinder接口的對象索引,
2.調用自定義類里方法就需要IBinder接口的對象索引強制為MyBinder
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 服務綁定成功時調用
MyBinder mbinder=(MyBinder) service;
mTv.setText(mbinder.getPlayInfo());
mTv2.setText(mbinder.getStopInfo());
}
StartService 跟BindService 結合使用
問題:
方法沒有找到 確認 方法名沒有寫錯
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。