Android widget 组件
2011-04-01 15:00 闫妍 阅读(345) 评论(0) 编辑 收藏 举报ScrollView
一个可以下拉滚动条的视图区域
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
Button
按钮
获取并设置事件及委托
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(button_listener);
在点击事件方法里面设置了程序标题
private Button.OnClickListener button_listener = new Button.OnClickListener() {
public void onClick(View v) {
setTitle("哎呦,button被点了一下");
}
};
在点击事件方法里面跳转到Activity 这个和之前的SetContentView(R.Layout.xxx)不一样,后者是绑定到指定模板。
private Button.OnClickListener text_view_button_listener = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ViewTextActivity.class);
startActivity(intent);
}
};
TextView
文本显示和Label类似,下面是实现
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#ffffff"
android:padding="10dip"
android:background="#cc0000"
android:text="这里是TextView,你可以在这里输入需要显示的文字信息.."
/>
java实现修改TextView 的值
TextView text_view = (TextView) findViewById(R.id.text_view);
CharSequence text_view_old = text_view.getText();
text_view.setText("修改前是:" + text_view_old
+ "\n修改为:TextView的值也是可以动态修改的.");
在AndroidManifest.xml 里面注册一个Activity
EditText
文本输入框
<EditText android:id="@+id/edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这里可以输入文字" />
CheckBox
复选框
<CheckBox android:id="@+id/plain_cb"
android:text="Plain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
获得是否选中
plain_cb = (CheckBox) findViewById(R.id.plain_cb);
if (plain_cb.isChecked()) {
r = r + "," + plain_cb.getText();
}
单选框按钮组
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/lunch"
android:id="@+id/menu">
<RadioButton
android:text="breakfast"
android:id="@+id/breakfast"
/>
<RadioButton
android:text="lunch"
android:id="@id/lunch" />
<RadioButton
android:text="dinner"
android:id="@+id/dinner" />
<RadioButton
android:text="all"
android:id="@+id/all" />
</RadioGroup>
清除选择
mRadioGroup = (RadioGroup) findViewById(R.id.menu);
Button clearButton = (Button) findViewById(R.id.clear);
clearButton.setOnClickListener(this);
public void onClick(View v) {
mRadioGroup.clearCheck();
}
Spinner
下拉框 //86页
<Spinner android:id="@+id/spinner_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
/>
private static final String[] mCountries = { "China" ,"Russia", "Germany",
"Ukraine", "Belarus", "USA" };
private void find_and_modify_view() {
//数组填充
spinner_c = (Spinner) findViewById(R.id.spinner_1);
allcountries = new ArrayList<String>();
for (int i = 0; i < mCountries.length; i++) {
allcountries.add(mCountries[i]);
}
aspnCountries = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, allcountries);
aspnCountries .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_c.setAdapter(aspnCountries);
//XML填充
spinner_2 = (Spinner) findViewById(R.id.spinner_2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.countries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_2.setAdapter(adapter);
}
//遗留问题android.R.layout.simple_spinner_item 在那里可以看到。
AutoCompleteTextView
自动完成文本框
<AutoCompleteTextView android:id="@+id/auto_complete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
将一个数组赋值给自动完成文本框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete);
setTitle("AutoCompleteTextViewActivity");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.auto_complete);
textView.setAdapter(adapter);
}
static final String[] COUNTRIES = new String[] {
"China" ,"Russia", "Germany",
"Ukraine", "Belarus", "USA" ,"China1" ,"China12", "Germany",
"Russia2", "Belarus", "USA"
};
DatePicker
日期选择控件
<DatePicker
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
初始化日期选择控件
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CheckBoxActivity");
setContentView(R.layout.date_picker);
DatePicker dp = (DatePicker)this.findViewById(R.id.date_picker);
dp.init(2009, 5, 17, null);
}
TimePicker
时间控件
<TimePicker
android:id="@+id/time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
后台设置24小时视图
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("TimePickerActivity");
setContentView(R.layout.time_picker);
TimePicker tp = (TimePicker)this.findViewById(R.id.time_picker);
tp.setIs24HourView(true);
}
ProgressBar
圆形进度条
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
水平进度条
<ProgressBar android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="75" />
</LinearLayout>
SeekBar
拖动栏目
<SeekBar
android:id="@+id/seek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:thumb="@drawable/seeker"
android:progress="50"/>
RatingBar
星评级
<RatingBar android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ratingBarStyleSmall="true" />
ImageView
图片控件,显示一个图片
<ImageView
android:id="@+id/imagebutton"
android:src="@drawable/eoe" //图片存储在资源文件夹
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
ImageButton
图片按钮
<ImageButton android:id="@+id/imagebuttonaa"
android:src="@drawable/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
ImageSwitcher 和 Gallery
展示图片
<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<Gallery android:id="@+id/gallery"
android:background="#55000000"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp" />
GridView
格子展示控件
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Tab
选项卡
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("TabDemoActivity");
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tab_demo,
tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")
.setContent(R.id.view3));
}