您的位置:首页 > 其它

常用控件的使用方法

2016-05-19 20:06 295 查看
1、TextView :用於在界面上顯示一段文字。

<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();
}
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: