无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Android開發(fā)學(xué)習(xí)教程(6)- Android Button用法和屬性

2023-01-26 15:21 作者:考研保研直通車  | 我要投稿

—— 真正的勇士,敢于直面慘淡的人生。 ——魯訊

Button是什么

Button按鈕,是用戶交互中使用最多的組件,在很多應(yīng)用程序中都很常見。當(dāng)用戶單擊按鈕的時候,會有相對應(yīng)的響應(yīng)動作。

Button有什么用

用來響應(yīng)用戶點擊事件,常用的有登錄按鈕、注冊按鈕、撥打電話按鈕等等。

Button的使用

以上一篇章中的TextView項目為例,我們先把TextView改為距離頂部40dp,下面添加一個Button的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml?version="1.0"?encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout?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:layout_width="match_parent"
????android:layout_height="match_parent"
????tools:context=".TestActivity">
????<TextView
????????android:id="@+id/textView"
????????android:layout_width="wrap_content"
????????android:layout_height="wrap_content"
????????android:layout_marginTop="40dp"
????????android:background="#cccccc"
????????android:singleLine="true"
????????android:text="Hello Activity Hello Activity Hello Activity Hello Activity"
????????android:textColor="#FF0000"
????????android:textSize="20sp"
????????app:layout_constraintEnd_toEndOf="parent"
????????app:layout_constraintHorizontal_bias="0.0"
????????app:layout_constraintStart_toStartOf="parent"
????????app:layout_constraintTop_toTopOf="parent"?/>
????<Button
????????android:id="@+id/button"
????????android:layout_width="wrap_content"
????????android:layout_height="wrap_content"
????????android:text="Button"
????????app:layout_constraintEnd_toEndOf="parent"
????????app:layout_constraintStart_toStartOf="parent"
????????app:layout_constraintTop_toBottomOf="@+id/textView"
????????android:layout_marginTop="20dp"?/>
</androidx.constraintlayout.widget.ConstraintLayout>

Button屬性

1
2
3
4
5
6
7
8
android:text:按鈕上顯示的文字內(nèi)容;
android:layout_width:Button的寬度(或者說長度),值為wrap_content意思是自適應(yīng)寬度,就是Button上的文字有多長,Button的寬度就有多長;
android:layout_width:Button的高度,值為wrap_content意思是自適應(yīng)高度,就是Button的文字有多高,Button的高度就有多高;
android:layout_marginTop:控件頂部離上方的距離;
app:layout_constraintEnd_toEndOf="parent":表示Button的右邊與父類控件(也就是ConstraintLayout)的右邊對齊;
app:layout_constraintStart_toStartOf="parent":表示Button的左邊與父類控件(也就是ConstraintLayout)的左邊對齊;
app:layout_constraintTop_toBottomOf="@+id/textView":表示Button的頂部與textView控件的底部對齊;
app:layout_marginTop="20dp":表示Button的頂部距離textView控件的底部20dp;

按鈕的點擊事件

同樣,Button和上一篇章中的TextView一樣,還有顏色、字體大小、背景顏色等屬性,同學(xué)們可以自己去試試效果,這里主要要講的是按鈕的點擊事件,也就是用戶點擊了按鈕之后會發(fā)生什么事件。基于以上項目,在TestActivity設(shè)置按鈕的點擊事件并彈出提示“我被點擊了”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import?android.os.Bundle;
import?android.view.View;
import?android.widget.Button;
import?android.widget.Toast;
public?class?TestActivity?extends?AppCompatActivity {
????@Override
????protected?void?onCreate(Bundle savedInstanceState) {
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.activity_test);
????????Button button = findViewById(R.id.button);
????????button.setOnClickListener(new?View.OnClickListener() {
????????????@Override
????????????public?void?onClick(View v) {
????????????????Toast.makeText(TestActivity.this,?"我被點擊了", Toast.LENGTH_SHORT).show();
????????????}
????????});
????}
}

下一篇章我們講講Button點擊事件的原理

源碼鏈接:https://yunjunet.cn/876730.html

Android開發(fā)學(xué)習(xí)教程(6)- Android Button用法和屬性的評論 (共 條)

分享到微博請遵守國家法律
合川市| 北京市| 团风县| 泌阳县| 雷州市| 石楼县| 凌海市| 黔江区| 华容县| 邵东县| 许昌市| 哈巴河县| 义乌市| 开平市| 祁东县| 佛冈县| 兴义市| 合肥市| 蓝山县| 孙吴县| 林口县| 虎林市| 建德市| 习水县| 吉木乃县| 永川市| 柳河县| 隆昌县| 荆门市| 分宜县| 克什克腾旗| 靖安县| 泸西县| 湛江市| 新源县| 海南省| 绥江县| 伊金霍洛旗| 天峨县| 陇川县| 富源县|