像素和顏色
像素:
- Android支持的像素單位: px(像素)、 in(英寸)、mm(毫米)、pt(鎊)、dp、sp
- 常用單位: px、 dp、 sp
顏色:
- 系統Color類定義的常用顏色取值
- 十六進制顏色取值
View和ViewGroup
所有的控件和佈局都是直接或者間集成自View類
View的常見屬性
- id:組件編號
- layout_width:組件寬度
- layout_height:組件高度
- layout_margin:外邊距
- padding:內邊距
- minWidth:最小寬度
- minHeight:最小高度
- background:背景,可以爲顏色,也可以是圖片
- visibility:是否可見
- layout_gravity:組件與上級對齊方式
ViewGroup容器與View組件的關係圖
LinearLayout佈局
- 線性佈局
- 佈局方式從左到右或者從上到下
常見屬性
- 同View
- orientation:水平佈局或者垂直佈局
TextView組件
主要作用:用於顯示文本
常見屬性
- 同View
- 其他常用屬性:text、textColor、textSize、ellipsize
Botton組件和事件監聽
主要作用:用於響應用戶點擊事件
常見屬性
- 同TextView
事件監聽
- 獲取按鈕
- 實現事件監聽
- 創建處理業務
創建新頁面和頁面跳轉
創建頁面
- 創建Activity類
- 創建佈局文件
- 在ManiFests中註冊頁面
Intent實現頁面跳轉
- 創建Intent類
- 實現跳轉頁面
代碼部分:
part1:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 高度和寬度等使用dp,字體大小使用sp -->
<!-- 100單位的px,在分辨率不同的機子顯示的大小是不一樣的 -->
<!--
LinearLayout:線性佈局
默認的佈局方式是:水平排列
注意點:排列的適合超出屏幕部分並不會折行,還是按照設定的佈局方式進行排列
更改佈局方式:horizontal(水平排列,默認的),vertical(垂直排列)
-->
<!--
layout_width:寬度
取值:match_parent(匹配父控件)、wrap_content(匹配內容)、自定義尺寸
layout_height:高度
取值:match_parent(匹配父控件)、wrap_content(匹配內容)、自定義尺寸
-->
<!--
margin:外邊距,設定組件的外部邊距
layout_margin:上下左右
layout_marginTop:上
layout_marginBottom:下
layout_marginLeft:左
layout_marginRight:右
-->
<!--
padding:內邊距,設定組件的內部邊距
paddingTop:上
paddingBottom:下
paddingLeft:左
paddingRight:右
-->
<!--
visibility:是否可見
取值:gone(不可見且不佔空)、invisible(不可見佔空)、visible(可見、默認值)
-->
<!--
layout_gravity:組件於父組件對齊方式
垂直佈局:只能做水平方向的佈局控制,比如:left、right、center、center_horizontal
水平佈局:只能做垂直方向的佈局控制,比如:top、bottom、center、center_vertical
gravity:對於組件內部元素約束其對齊方式,取值:top、bottom、left、right、center、center_horizontal、center_vertical
-->
<!--
layout_weight:權重比
按份分:
垂直等分:設置高度爲0dp,然後分配權重比
水平等分:設置寬度爲0dp,然後分配權重比
當寬度或高度不爲0dp的時候:先扣除定義的寬度或高度,然後再按照權重比分配剩餘尺寸,然後再加上定義的高度或寬度
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">
<!--
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#C31B1B"
android:layout_weight="1">
</View>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#0F48E6"
android:layout_weight="1">
</View>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#000000"
android:layout_weight="1">
</View>
-->
<!--
TextView:文本控件,用於顯示文本
text:文本內容
textSize:文本的大小
textColor:文本的顏色
lines:行數,指定多少行就是多少行,文本高度就是行數的高度
maxLines:最大行數,最多佔據多少行,文本的高度最大就是設置的行數,若沒有填充滿,高度隨文本高度來定
minLines:最小行數,最少佔據多少行,文本的高度最小就是設置的行數,可以繼續往下填充,高度隨文本高度來定
ellipsize:字符省略方式,start:開頭,middle:中間,end:結尾,none:不需要省略,注意:需要配合singleLine使用
-->
<!--
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="好好學習天天向上好好學習天天向上好好學習天天向上好好學習天天向上好好學習天天向上"
android:textColor="#240A5A"
android:textSize="40sp"
android:maxLines="2"
android:singleLine="true"
android:ellipsize="end">
</TextView>
-->
<!-- 演示layout_gravity -->
<!--
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="好好學習"
android:textColor="#240A5A"
android:textSize="40sp"
android:layout_gravity="center_vertical">
</TextView>
-->
<!-- 演示gravity -->
<!--
<TextView
android:layout_width="300dp"
android:layout_height="300dp"
android:text="好好學習"
android:textColor="#EC5286"
android:textSize="40sp"
android:gravity="center_horizontal|bottom">
</TextView>
-->
<!--
Button:按鈕
id的定義方式:@+id/id的名稱
-->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="猜數字遊戲"
android:textSize="20sp"
android:textColor="#2196F3"
android:background="#FF9800">
</Button>
</LinearLayout>
效果圖:
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<!--
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第二個界面"></TextView>
-->
<!-- 猜數字遊戲 -->
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請開始猜數字"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:textSize="25sp">
</TextView>
<TextView
android:id="@+id/tipsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入數字:"
android:layout_gravity="right"
android:layout_marginTop="6dp"
android:textSize="20sp">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="1"></Button>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="2"></Button>
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="3"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="3"></Button>
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="4"></Button>
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="5"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="7"></Button>
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="8"></Button>
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="9"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="0"></Button>
<Button
android:id="@+id/submitButton"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="2"
android:text="確認"></Button>
</LinearLayout>
</LinearLayout>
效果圖:
part2:
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設置該頁面的佈局內容
/*
* 取用方式的理解:R.layout.activity_main
* R:可以理解爲res,指的是資源
* layout:指代layout文件夾
* activity_main:指代要取用的文件的名稱
* */
setContentView(R.layout.activity_main);
//Intent intent = new Intent(this, Main2Activity.class); //沒有問題,而下面匿名內部類相當於OnClickListener
//1.獲取Button
Button button = findViewById(R.id.button); //導包快捷鍵:Alt+Enter
//2.給Button綁定事件
button.setOnClickListener(new View.OnClickListener() { //匿名內部類形式,OnClickListener爲抽象類(×)或接口(√)
@Override
public void onClick(View v) {
//3.處理業務邏輯
//System.out.println("按鈕被點擊了!!!!!!");
//public Intent(Context packageContext, Class<?> cls):第一個參數是當前頁面(實例),第二個參數是需要跳轉的頁面
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
}
}
Main2Activity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
//隨機生成的數字
private int number;
//提示文字,用於顯示用戶猜的數字
private TextView tipsTextView;
//猜的數字
private String guessNumber;
//結果文字,用於顯示結果信息
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//生成隨機數
Random random = new Random();
number = random.nextInt(100);
//獲取提示文本
tipsTextView = findViewById(R.id.tipsTextView);
//初始化猜的數字
guessNumber = "";
//獲取結果文本
resultTextView = findViewById(R.id.resultTextView);
//獲取頁面所有的元素
findViewById(R.id.button0).setOnClickListener(this); //this指當前實例 Main2Activity
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
findViewById(R.id.button8).setOnClickListener(this);
findViewById(R.id.button9).setOnClickListener(this);
//獲取確認按鈕
findViewById(R.id.submitButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//1.比較數字
if (!guessNumber.equals("")) {
//2.顯示比較結果
int guessNumberInt = Integer.parseInt(guessNumber);
if (guessNumberInt > number) {
resultTextView.setText("猜大了");
guessNumber = "";
tipsTextView.setText("請輸入數字:");
} else if (guessNumberInt < number) {
resultTextView.setText("猜小了");
guessNumber = "";
tipsTextView.setText("請輸入數字:");
} else {
resultTextView.setText("恭喜你,猜正確了!數字是:" + number);
}
}
}
});
}
@Override
public void onClick(View v) {
//1.獲取button數字
Button button = (Button) v;
String buttonText = button.getText().toString();
//System.out.println("獲取到的按鈕的數字是" + buttonText);
//2.將數字添加到提示上
String tips = tipsTextView.getText().toString();
if (tips.equals("請輸入數字:")) {
//第一次輸入:請輸入數字: -> 您輸入的數字是:xxx
tipsTextView.setText("您輸入的數字是:" + buttonText);
} else {
//第二次輸入:您輸入的數字是:x -> 您輸入的數字是:xx
tipsTextView.setText(tips + buttonText);
}
//3.更改猜的數字
guessNumber = guessNumber + buttonText;
}
}
注:
android:gravity和android:layout_gravity區別:
android:gravity屬性是對該view中內容的限定。比如一個button上面的text,你可以設置該text相對於view的靠左,靠右等位置。
android:layout_gravity屬性是用來設置該view相對與父view的位置。比如一個button在linearlayout裏,你想把該button放在linearlayout裏靠左、靠右等位置就可以通過該屬性設置。