Android Studio教程06-布局,監聽器以及基本控件
阿新 • • 發佈:2019-01-18
pri con 布局文件 apk toc ins 就會 監聽 prot 2. 常用
2.1.
2.2.
目錄
- 2. 監聽器
- 3. 布局
- 3.1. 布局分類
- (1). Linear Layout
- (2). Relative Layout
- (3). ListView
- (4). Grid View
- 3.1. 布局分類
- 4. 其他比較雜的內容
- 4.1. 距離單位的區別px,dp,sp
- 4.2. 控件的外邊距和內邊距
- 1. 什麽是內外邊距
- 2. 如何設置內外邊距
- 5. Android控件
- 5.1.多選按鈕CheckBox
- 1. 如何使用CheckBox
- 2. 常用
onClickListener
和onCheckedChangeListener
監聽器- 2.1.
onClickListener
監聽器 - 2.2.
onCheckedChangeListener
- 2.1.
- 5.1.多選按鈕CheckBox
2. 監聽器
- 一個控件可以設置多個監聽器
- 綁定監聽器的步驟
- 獲取代表控件的對象
- 定義一個類,實現監聽器接口
- 生成監聽器對象
- 為控件綁定監聽器對象
public class MainActivity extends AppCompatActivity { private Button bt; private TextView tv; int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button)findViewById(R.id.bt1); tv = (TextView)findViewById(R.id.hello); //生成監聽器對象 new ButtonListener() //為控件綁定監聽器對象 bt.setOnClickListener bt.setOnClickListener(new ButtonListener()); System.out.println("--MainActivity: OnCreate--"); } // 定義一個類,實現監聽器接口 class ButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { count++; tv.setText(count+""); } } }
3. 布局
- 控件布局方法:就是控制控件在Activity中的位置,大小,顏色以及其他控件樣式屬性的方法
- 如何設置布局
- 在布局文件完成控件布局
- 在Java代碼中完成控件布局
- 在布局文件完成控件布局
3.1. 布局分類
(1). Linear Layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FF0000" android:text="First text view"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00FF00" android:text=" Second Text View"/> </LinearLayout>
(2). Relative Layout
(3). ListView
(4). Grid View
4. 其他比較雜的內容
4.1. 距離單位的區別px,dp,sp
px: 像素分辨率,屏幕是480*800個像素,每個像素可以顯示一個RGB顏色
dpi:屏幕細膩程度
dp:設備無關像素(最主要)
為什麽使用dp?
- sp: 可以縮放的像素:用於指定字體大小
4.2. 控件的外邊距和內邊距
1. 什麽是內外邊距
2. 如何設置內外邊距
5. Android控件
5.1.多選按鈕CheckBox
1. 如何使用CheckBox
private CheckBox eatbox, sleppbox, dotabox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frist_layout);
eatbox = (CheckBox)findViewById(R.id.eatId);
sleppbox = (CheckBox)findViewById(R.id.sleppId);
dotabox = (CheckBox)findViewById(R.id.dotaId);
onBoxClickListener listener = new onBoxClickListener();
eatbox.setOnClickListener(listener);
sleppbox.setOnClickListener(listener);
dotabox.setOnClickListener(listener);
}
配置文件如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/eatId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃飯"/>
<CheckBox
android:id="@+id/sleppId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡覺"/>
<CheckBox
android:id="@+id/dotaId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="data"/>
</LinearLayout>
2. 常用onClickListener
和onCheckedChangeListener
監聽器
2.1. onClickListener
監聽器
class onBoxClickListener implements View.OnClickListener{
// view參數是調用setOnClickListener的對象
// view是checkbox的父類
// view.getId--查看是哪個對象調用的這個方法
@Override
public void onClick(View v) {
// 向下轉型
CheckBox box = (CheckBox)v;
if (v.getId() == R.id.eatId){
System.out.println("eat is clicked");
}
else if (v.getId() == R.id.sleppId){
System.out.println("slepp is clicked");
}
else if (v.getId() ==R.id.dotaId){
System.out.println("dota is clicked");
}
// checkbox 是否選中
if (box.isChecked()){
System.out.println("clicked");
}
else{
System.out.println("Not clicked");
}
}
}
2.2. onCheckedChangeListener
監聽器
CompoundButton
// 選中的時候就會調用這個狀態
class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == R.id.eatId){
System.out.println("eat is clicked");
}
else if (buttonView.getId() == R.id.sleppId){
System.out.println("slepp is clicked");
}
else if (buttonView.getId() ==R.id.dotaId){
System.out.println("dota is clicked");
}
// checkbox 是否選中
if (isChecked){
System.out.println("clicked");
}
else{
System.out.println("Not clicked");
}
}
}
Android Studio教程06-布局,監聽器以及基本控件