写了几个基本控件
- Button
- ImageButton
- EditText
- CheckBox
- RadioGroup和RadioButton
- ToggleButton
下面是代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<ImageButton
android:text="ImageButton"
android:id="@+id/buttonimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon">
</ImageButton>
<EditText
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/check1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Autosave"
/>
<CheckBox
android:id="@+id/checkstar"
style="?android:attr/starStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioGroup
android:id="@+id/grou1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/radio1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="option1"
>
</RadioButton>
<RatingBar
android:id="@+id/ra1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numStars="4"
android:rating="2"
>
</RatingBar>
</RadioGroup>
<ToggleButton
android:id="@+id/toggole1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ToggleButton>
</LinearLayout>
activity使用
public class CreateUi extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baseui);
CheckBox che = (CheckBox)findViewById(R.id.check1);
che.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(((CheckBox)v).isChecked())
Toast.makeText(getBaseContext(),
" was clicked",
Toast.LENGTH_LONG).show();
else
Toast.makeText(getBaseContext(),
" was not clicked",
Toast.LENGTH_LONG).show();
}
});
RadioGroup rg = (RadioGroup)findViewById(R.id.grou1);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton radio = (RadioButton)findViewById(R.id.radio1);
if(checkedId==radio.getId()){
Toast.makeText(getBaseContext(),
" option1 was clicked",
Toast.LENGTH_LONG).show();
}
}
});
ToggleButton toggle = (ToggleButton)findViewById(R.id.toggole1);
toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(((ToggleButton)v).isChecked()){
Toast.makeText(getBaseContext(),
" toggle1 was clicked",
Toast.LENGTH_LONG).show(); }
else {
Toast.makeText(getBaseContext(),
((ToggleButton)v).getText()+ " toggle1 was clicked",
Toast.LENGTH_LONG).show();
}
}
});
}
}