布局練習的差不多了,那就來看看控件,然后簡單介紹一下幾個基礎控件,他們所包含的屬性,還有實際運用場景。
一、TextView
//android:text 屬性,指的是控件的文本內容
<TextView
android:id="@+id/tv_1"
android:text="我是文本框1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
//所對應java代碼為 屬性所對應的java代碼基本上都為:setXXXX 這里用text屬性舉例, 獲取某個屬性的值基本為:getXXX
//下面所有部分,默認已經找到控件
textView = findViewById(R.id.tv_1);
textView.setText("我是按鈕1");//設置控件文本內容
既然有文字了,那肯定有文字顏色,文字大小,文字樣式
<TextView
android:id="@+id/tv_1"
android:text="我是文本框1"
android:textSize="18sp"
android:textColor="@color/colorAccent"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
// android:textColor 文字顏色
//android:textSize 文字大小,單位盡量用sp dp也行得通
//android:textStyle 文字樣式,取值:bold(加粗)、italic(斜體)、normal(默認)
//下面展示下分別的效果
加粗:
image.png
斜體:
image.png
默認:(當然如果不寫,也就是默認)
image.png
既然是個控件,那么我們也許有需求要改變它的樣式,比如 這樣的需求
image.png
這里就用到了我們的背景:
//這里為了方便,我先把文本顏色改成白色,為了文章的可讀性強一些,接下來只會貼關鍵性代碼
//屬性:android:background 可以是背景,可以圖片,也可以自定義的
android:background="@color/colorAccent"
先來看看效果:(為了效果明顯一點,我把背景改為黑色)
image.png
這里我加上margin和padding屬性,為了好看些,margin外邊距和padding內邊距前面已經說過了哈。這里再貼一下相關代碼,實踐一下。
//外邊距
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
//內邊距
android:paddingLeft="10dp"
android:padding="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
細心的人可能發現了哈,這個跟上面的圖不太一樣,角是尖的,不是圓的。 這里再擴展一個寫背景的方法。 在目錄:app\src\main\res\drawable 中,新建一個資源l文件 并且選為根標簽為shape
image.png
然后
image.png
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
</shape>
接下來往里寫內容:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 背景顏色-->
<solid android:color="@color/colorAccent"/>
<!-- 邊框顏色 邊框寬度-->
<stroke android:color="@color/colorYellow" android:width="4dp"/>
<!-- 圓角弧度-->
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="4dp"
android:topLeftRadius="10dp" android:topRightRadius="4dp"/>
</shape>
預覽布局如圖:
image.png
然后我們放到xml里面看效果
//注意這里的引用方式為 drawable
android:background="@drawable/textview_bg"
image.png
然后根據自己的需求和樣式自己實現就ok了。
然后再補充一個屬性叫做
android:visibility
//使用
android:visibility="visible"http://取值:visible、gone、invisible
//他們的含義分別是:
//visible:看得見
//gone:找不到了(不在布局中占位)
//invisible:看不見 (會在布局中占位)
下來分別看一下效果
Visible.png
Gone.png
invisible.png
接下來說一下Button
二、Button
說Button之前,我們先來看看它的源碼。
//Button的代碼量并不多,我們看到它繼承于TextView,那么就基本可以知道,TextView的屬性它全部可以用。
//至于區別在于,Button在用戶觸摸的時候會有一個按下去的效果。下圖展示
@RemoteView
public class Button extends TextView {
....
}
按鈕被觸摸前:
image.png
按鈕被觸摸后:
image.png
喲西,圖上不是很明顯,可以自己寫一下看看。 Button除了默認樣式和textview不太一樣,別的api和屬性都是幾乎通用的。這里Button就不說過多了。
三、EditText
yep! 翻譯過來就是 編輯文本,像我們使用某信、某Q的時候,我們平時聊天的時候輸入文本的地方,就是它了。 我們和上面一樣,來瞅瞅它的源碼。
public class EditText extends TextView {
...
}
喲西,發現這貨也是繼承于TextView,那跟button一樣咯,api和屬性基本通用,不過這里說幾個edittext常用到的屬性:
android:hint="提示內容"http://默認提示內容,
android:inputType="textPassword"http://輸入類型,可以是text、number、password 等等,值很多可以自行根據需求使用
android:enabled="false"http://是否可以編輯
關于inputType的值,羅列一下:
android:inputType=”text”
android:inputType=”textCapCharacters” 字母大寫
android:inputType=”textCapWords” 首字母大寫
android:inputType=”textCapSentences” 僅第一個字母大寫
android:inputType=”textAutoCorrect” 自動完成
android:inputType=”textAutoComplete” 自動完成
android:inputType=”textMultiLine” 多行輸入
android:inputType=”textImeMultiLine” 輸入法多行(如果支持)
android:inputType=”textNoSuggestions” 不提示
android:inputType=”textUri” 網址
android:inputType=”textEmailAddress” 電子郵件地址
android:inputType=”textEmailSubject” 郵件主題
android:inputType=”textShortMessage” 短訊
android:inputType=”textLongMessage” 長信息
android:inputType=”textPersonName” 人名
android:inputType=”textPostalAddress” 地址
android:inputType=”textPassword” 密碼
android:inputType=”textVisiblePassword” 可見密碼
android:inputType=”textWebEditText” 作為網頁表單的文本
android:inputType=”textFilter” 文本篩選過濾
android:inputType=”textPhonetic” 拼音輸入
//數值類型
android:inputType=”number” 數字
android:inputType=”numberSigned” 帶符號數字格式
android:inputType=”numberDecimal” 帶小數點的浮點格式
android:inputType=”phone” 撥號鍵盤
android:inputType=”datetime” 時間日期
android:inputType=”date” 日期鍵盤
android:inputType=”time” 時間鍵盤
常用的基本告一段落了,有需求的話可以留言或者自行百度。
四、控件的點擊
以TextView為例:
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//這里是點擊之后,要執行的事情
//比如我要讓他彈一個吐司
Toast.makeText(Main2Activity.this,"TextView被點擊了",Toast.LENGTH_SHORT).show();
}
});
看一下效果:
image.png
本節課,結束。 有問題可以評論留言,或者以后做擴展