1. 程式人生 > >Android學習之基本元件

Android學習之基本元件

TextView  展示文字,不可編輯

Button 按鈕,

EditText 文字輸入框

ProgressBar 進度條

一、TextView

android:id  設定TextView的id值,是TextView的唯一標識。在JAVA程式碼塊中,也會是通過該屬性獲取控制元件

android:layout_width  控制元件的寬度,有兩個可選值,分別為match_parent(表示該控制元件的寬度與父檢視的寬度相同)wrap_content(表示根據內容自動設定大小)

android:layout_height 控制元件的高度,可選值與layout_width相同

android:text 表示TextView顯示的值

android:textsize 表示TextView上的文字的大小

android:textColor 字型的顏色

android:gravity 表示TextView上文字的對齊方式,對齊方式有很多如居中(center),右對齊(left)等

android:marginTop等 表示TextView的位置,可以設定上下左右的距離

java程式碼塊通過findViewById()函式獲取該控制元件,可通過settext()函式設定顯示的文字

效果:

二、Button

android:id 設定button按鈕的唯一標識,同時方便Activity進行監聽操作

android:layout_width與android:layout_height 的用法基本上都是一樣,設定控制元件的寬度和高度

android:text 設定按鈕上顯示的字元

功能:點選按鈕,實現跳轉到另一個頁面

private Button btn2;
btn2 = findViewById(R.id.btn_3);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ButtonActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });

效果:

三、EditText

<EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="placeHoder: type something here"
        android:maxLines="3"
        />

android:hint 用來提示輸入框是幹嘛的

android:maxLines 設定輸入框的最大行數

效果:

四、ProgressBar

<ProgressBar
        android:id="@+id/pb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        />

android:max 給進度條設定一個最大值

如何使進度條在資料載入完成後自動消失呢?

這裡可通過設定控制元件的可見性實現進度條的隱藏,具體呼叫的方法為setVisibility()。

效果: