一、文本控件TextView
1.布局文件
<TextViewandroid:id="@+id/tv_show"
android:text="@string/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/green"
android:textSize="@dimen/title"
android:lines="1"
android:maxWidth="40dp"
android:ellipsize="middle"
android:focusable="true"
android:focusableInTouchMode="true"/>
2.控件屬性
android:id 控件唯一標識
android:text 顯示的文本信息
android:layout_width 控件寬度
android:layout_height 控件高度
android:textSize 字體大小
android:textColor 字體顏色
android:lines 文本顯示行數
android:maxWidth 最大顯示寬度
android:ellipsize 設置當文本過長時如何顯示文本內容
start:省略號顯示在開頭
middle:省略號顯示在中間
end:省略號顯示在結尾
marquee:以跑馬燈方式顯示
android:focusable 是否獲得焦點
android:
focusableInTouchMode 觸摸模式后是否可獲得焦點
3.對象獲取
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取文本對象
TextView tv_show = (TextView) findViewById(R.id.tv_show);
//獲取android:text屬性值
String text = tv_show.getText().toString();
//后臺日志輸出
Log.i("wl",text);
//設置android:text
tv_show.setText("Hello Man");
//通過getResources()獲得資源常量 tv_show.setTextColor(getResources().getColor(R.color.colorPrimary));
//吐司 在app中輸出
Toast.makeText(this,text,Toast.LENGTH_LONG).show();
}
二、按鈕控件Button
1.布局文件
<Buttonandroid:id="@+id/btn_show"
android:text="按鈕"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnClick"/>
2.注冊監聽
(1)匿名內部類
//獲取按鈕對象
Button btn_show = (Button) findViewById(R.id.btn_show);
//注冊點擊監聽
btn_show.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Toast.makeText(MainActivity.this,"點擊按鈕",Toast.LENGTH_LONG).show();
}
});
(2)接口實現
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//聲明控件對象 Button btn_show ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取按鈕對象
btn_show = (Button) findViewById(R.id.btn_show);
//注冊點擊監聽
btn_show.setOnClickListener(this);
}
//實現接口類 @Override
public void onClick(View v) {
Toast.makeText(this,"點擊按鈕",Toast.LENGTH_LONG).show();
}
}
(3)設置onclick屬性
public void btnClick(View v){
Toast.makeText(this,"點擊按鈕",Toast.LENGTH_LONG).show();
}
3.按鈕背景圖片設置及點擊效果
(1)在res/drawable下創建btn_selector.xml,選擇選中和沒選中時的背景圖片
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_bg2" android:state_pressed="false"/>
<item android:drawable="@drawable/btn_bg_p" android:state_pressed="true"/></selector>
(2)按鈕布局文件中背景圖片使用btn_selector.xml
<Buttonandroid:text="卸載"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/btn_selector"
android:textColor="#fff"
android:textSize="18sp"/>
三、圖片控件ImageView
1.布局文件
<ImageViewandroid:src="@drawable/danger"
android:background="@drawable/danger"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
2.控件屬性
android:src
設置ImageView中顯示的圖片
– 是前景,顯示在前面
– 可根據寬高縮放,但是保持圖片原有比例
android:background
設置ImageView控件的背景
– 是背景,顯示在后面
– 可根據寬高縮放,但是不保持圖片原有比例
– 除了圖片以外,背景還可以是顏色
3.圖片資源
(1)注意命名中不得含有中文或大寫字母
(2)首字母必須以字母開頭
(3)格式png,jpg
四、輸入控件EditText
1.布局文件
<EditTextandroid:hint="請輸入"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
用戶注冊界面開發
任務目標:
1.掌握RelativeLayout布局的使用方法
2.掌握RadioButton和RadioGroup控件的使用方法
3.掌握CheckBox控件的使用方法
任務陳述:
使用RelativeLayout布局管理器和CheckBox控件及RadioGroup控件實現用戶注冊界面開發。該界面用于輸入用戶的注冊信息,包括用戶名,郵箱,密碼及確認密碼的輸入,并進行性別的選擇,進行是否顯示密碼和同意協議的多選,最后點擊注冊,實現注冊功能。
任務學習:
5.1 RelativeLayout布局的使用
在Android中,相對布局是指按照組件之間的相對位置進行布局,這種方式允許子元素指定它們相對于其他元素或父元素的位置(通過ID指定)。
在開發中,可以在XML布局文件定義相對布局管理器,其基本語法如下:
5.2掌握RadioButton和RadioGroup控件的使用方法
在Android中,單選按鈕(RadioButton)繼承了普通按鈕。因此,它們都可以直接使用普通按鈕支持的的各種屬性和方法。
單選按鈕(RadioButton),可以通過在XML布局文件中使用<RadioButton>標記添加,其基本語法格式如下:
RadioButton常見的XML屬性列表
若要RadioButton組件與RadioGroup組件一起使用時,即構成一個單選按鈕組。在XML布局文件中,添加RadioGroup組件的基本語法格式如下:
5.3 CheckBox控件的使用
在Android中,復選框用CheckBox表示,,而CheckBox類是Button類的子類,所以可以直接使用Button支持的各種屬性??梢酝ㄟ^在XML布局文件中使用<CheckBox>標記添加,其語法格式如下:
在Eclipse下創建一個Android項目,命名為AndroidDemo2.5,實現用戶注冊界面開發。
1.修改res/layout目錄下的布局文件,首先添加一個相對布局管理器,在該布局管理器中嵌套添加一個線性布局管理器,并在其在內部添加四個線性布局管理器,在第一個線性布局管理器中,添加一個TextView控件和一個EditText控件,實現用戶名的輸入。
2.在第二個線性布局管理器中,繼續添加一個TextView控件和一個EditText控件,用于密碼的輸入,并添加一個CheckBox決定是否顯示密碼。
3.在第三個線性布局管理器中,繼續添加一個TextView控件和一個EditText控件,用于確認密碼的輸入。
4.在第四個線性布局管理器中,繼續添加一個RadioGroup控件,在RadioGroup控件中,添加兩個RadioButton按鈕,用于性別的選擇。
5.在外層的線性布局管理器中,添加一個Button按鈕,點擊按鈕,實現注冊功能。
6.界面設計呈現的效果如下圖所示