常用控件的使用方法
2016-05-19 20:06
295 查看
1、TextView :用於在界面上顯示一段文字。
android:id 表示為該控件定義一個唯一的標識符
android:layout_width 表示設定該控件的寬度
android:layout_height 表示設定該控件的高度
android 中所有的控件都具體這兩個屬性,屬性值包含:match_parent、wrap_content、fill_parent。其中match_parent和fill_parent意義一致,但官方更推薦使用match_parent。
match_parent 表示該控件的大小與父佈局大小一樣,也就是由父佈局決定控件大小
wrap_content 表示該控件的大小由控件本身內容決定
android:gravity 表示該控件內容的對齊方式。可選值有:top,bottom,left,right,center。
android:textSize 表示設定控件內容的大小
android:textColor 表示設定控件內容的顏色。
2、Button 用於在界面上顯示一個按鈕
每個button必須添加監聽器,否則button將失效。
3、EditText 用於在界面上顯示一個可編輯的文本框
<TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="30sp" android:textColor="#ff0000" android:text="This is a TextView" />
android:id 表示為該控件定義一個唯一的標識符
android:layout_width 表示設定該控件的寬度
android:layout_height 表示設定該控件的高度
android 中所有的控件都具體這兩個屬性,屬性值包含:match_parent、wrap_content、fill_parent。其中match_parent和fill_parent意義一致,但官方更推薦使用match_parent。
match_parent 表示該控件的大小與父佈局大小一樣,也就是由父佈局決定控件大小
wrap_content 表示該控件的大小由控件本身內容決定
android:gravity 表示該控件內容的對齊方式。可選值有:top,bottom,left,right,center。
android:textSize 表示設定控件內容的大小
android:textColor 表示設定控件內容的顏色。
2、Button 用於在界面上顯示一個按鈕
<Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" />
每個button必須添加監聽器,否則button將失效。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show(); } }); }
3、EditText 用於在界面上顯示一個可編輯的文本框
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); editText = (EditText)findViewById(R.id.edit_text); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub editData = editText.getText().toString(); Log.d("hello", "editData = " + editData); if(editData != null & !editData.equals("")){ Toast.makeText(MainActivity.this, editData, Toast.LENGTH_SHORT).show(); }else Toast.makeText(MainActivity.this, "hello1", Toast.LENGTH_SHORT).show(); } }); }
相关文章推荐
- 【数据结构】串 YTU 2296 KMP算法(2)next
- 231. Power of Two
- Uva 1225
- MTK Battery系统
- ContentProvider基础
- 绝对转载
- 映射配置文件中关联关系——一对一
- hrbust/哈理工oj 1475国王的宴会【树型dp】
- POJ1011 木棒
- 加权中位数应用:油井加权的输油管道位置选取
- MES机器数据维护
- TCP的拥塞控制
- MTK 平台memory(emmc)移植常见问题总结
- Poj 1904 King's Quest 强连通分量
- POJO百度百科
- Location服务之LocationManager
- C语言链表各种操作
- 【转载】C++基本功和 Design Pattern系列 ctor & dtor
- 【数据结构】串 YTU 2295 KMP算法(1)next
- POJ-2960-S-Nim(SG函数)