五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(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用法和屬性的評論 (共 條)

分享到微博請遵守國家法律
耒阳市| 惠州市| 牙克石市| 彰化市| 乌兰察布市| 玉环县| 邹城市| 法库县| 洪雅县| 嘉善县| 巴塘县| 高雄县| 洛宁县| 大埔县| 长垣县| 德钦县| 吕梁市| 当涂县| 郁南县| 商丘市| 万安县| 上高县| 佳木斯市| 横峰县| 许昌市| 吉林省| 友谊县| 南京市| 吉林市| 遵化市| 牟定县| 邵阳县| 清丰县| 永登县| 邵阳县| 杭锦旗| 南宁市| 蒙山县| 呈贡县| 新建县| 普陀区|