一、種類
1. TextView 文本控件
2. Button 按鈕控件
3. RadioGroup 單選按鈕組控件
RadioButon 單選控件
4. CheckBox 複選框控件
5. Toast控件
6. ProgressBar控件
二、佈局文件中添加控件
<widget
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"></widget>
對於ProgressBar控件還有選項:
style="?android:attr/progressBarStyleHorizontal" //進度條的風格
android:visibility="gone"//進度條初始不顯示
三、Activity中獲取控件id
Widget widget = (Widget)findViewById(R.id.name);
四、設置監聽器
①Button
button1.setOnClickListener(new MyButtonListener()); //設置監聽事件
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) { //單擊時的動作
// TODO Auto-generated method stub
}
}
②RadioGroup
radioGroup.setOnCheckedChangeListener(new myRadioGroup());
class myRadioGroup implements RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (radioButton1.getId() == checkedId)
{
Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show();
}else if (radioButton2.getId() == checkedId)
{
Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show();
}
}
}
③CheckBox
checkBox1.setOnCheckedChangeListener(new myCheckBox1());
class myCheckBox1 implements CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked)
{
Toast.makeText(MainActivity.this, "swim", Toast.LENGTH_SHORT).show();
}
}
}
五 控件常用函數
Toast:
Toast.makeText(MainActivity.this, "swim", Toast.LENGTH_SHORT).show();
ProgressBar:
firstBar.setVisibility(View.VISIBLE); //顯示
firstBar.setMax(150);//設置進度條最大值
firstBar.setProgress(i);//設置進度條第一進度
firstBar.setSecondaryProgress(i+10);//設置進度條第二進度