Android开发之《Android应用开发揭秘》UI事件汇总
/*
*Android开发之《Android应用开发揭秘》UI事件汇总
*北京Android俱乐部群:167839253
*Createdon:2011-12-01
*Author:blueeagle
*Email:[email protected]
*/
思想跑毛是很可怕的,最近去尝试了下创业,结果碰了一鼻子灰,公司乱七八糟的项目搞的焦头烂额。各种没有心情。还好,现在沉淀下来,继续android开发的征程。
以下是Android应用开发揭秘这本书第四章的关于UI的事件处理的总结。基本上都是可以复用的代码。存着日后方便。
1.EditView控件的setOnKeyListener方法。点击上下左右键或者中间键的时候可以出发该方法。代码如下。
Java代码
1.myEditView.setOnKeyListener(newEditText.OnKeyListener(){
2.
3.@Override
4.publicbooleanonKey(Viewv,intkeyCode,KeyEventevent){
5.myTextView.setText("文本框中内容是:"+myEditView.getText().toString());
6.returnfalse;//TODOAuto-generatedmethodstub
7.}
8.});
myEditView.setOnKeyListener(newEditText.OnKeyListener(){@OverridepublicbooleanonKey(Viewv,intkeyCode,KeyEventevent){myTextView.setText("文本框中内容是:"+myEditView.getText().toString());returnfalse;//TODOAuto-generatedmethodstub}});
2.RadioButton控件和RadioGroup控件的组合使用,代码如下:
Java代码
1.myRadioGroup.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){
2.
3.@Override
4.publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
5.//TODOAuto-generatedmethodstub
6.if(checkedId==myRadioButton3.getId())
7.{
8.DisplayToast("正确答案:"+myRadioButton3.getText()+"恭喜你回答正确");
9.}
10.else
11.{
12.DisplayToast("回答错误");
13.}
14.}
15.);
myRadioGroup.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){@OverridepublicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){//TODOAuto-generatedmethodstubif(checkedId==myRadioButton3.getId()){DisplayToast("正确答案:"+myRadioButton3.getText()+"恭喜你回答正确");}else{DisplayToast("回答错误");}}});
3.CheckBox控件的使用,代码如下:
Java代码
1.myCheckBox1.setOnCheckedChangeListener(newCheckBox.OnCheckedChangeListener(){
2.@Override
3.publicvoidonCheckedChanged(CompoundButtonbuttonView,
4.booleanisChecked){
5.//TODOAuto-generatedmethodstub
6.if(myCheckBox1.isChecked()){
7.DisplayToast("你选择了:"+myCheckBox1.getText());
8.}
9.}
10.});
myCheckBox1.setOnCheckedChangeListener(newCheckBox.OnCheckedChangeListener(){@OverridepublicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){//TODOAuto-generatedmethodstubif(myCheckBox1.isChecked()){DisplayToast("你选择了:"+myCheckBox1.getText());}}});
4.Button控件的使用,代码如下:
Java代码
1.myButton1.setOnClickListener(newButton.OnClickListener(){
2.@Override
3.publicvoidonClick(Viewv){
4.}
5.});
myButton1.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){}});
4.Spinner控件的使用,代码如下:
Java代码
1.//Spinner控件需要跟ArrayAdapter绑定使用。
2.privatestaticfinalString[]m_Countries={"A型","B型","O型","AB型","未知类型"};
3.privateSpinnermySpinner;
4.privateArrayAdapter<String>myAdapter;
5.mySpinner=(Spinner)findViewById(R.id.mySpinner1);
6.myAdapter=newArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m_Countries);
7.//设置下拉列表风格
8.myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
9.//将adapter添加到mySpinner中
10.mySpinner.setAdapter(myAdapter);
11.//添加事件监听
12.mySpinner.setOnItemSelectedListener(newSpinner.OnItemSelectedListener(){
13.@Override
14.publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,
15.intarg2,longarg3){
16.//TODOAuto-generatedmethodstub
17.myTextView.setText("你的血型是"+m_Countries[arg2]);
18.arg0.setVisibility(View.VISIBLE);
19.}
20.@Override
21.publicvoidonNothingSelected(AdapterView<?>arg0){
22.//TODOAuto-generatedmethodstub
23.DisplayToast("你什么也没选!");
24.}
25.});
//Spinner控件需要跟ArrayAdapter绑定使用。privatestaticfinalString[]m_Countries={"A型","B型","O型","AB型","未知类型"};privateSpinnermySpinner;privateArrayAdapter<String>myAdapter;mySpinner=(Spinner)findViewById(R.id.mySpinner1);myAdapter=newArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m_Countries);//设置下拉列表风格myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//将adapter添加到mySpinner中mySpinner.setAdapter(myAdapter);//添加事件监听mySpinner.setOnItemSelectedListener(newSpinner.OnItemSelectedListener(){@OverridepublicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){//TODOAuto-generatedmethodstubmyTextView.setText("你的血型是"+m_Countries[arg2]);arg0.setVisibility(View.VISIBLE);}@OverridepublicvoidonNothingSelected(AdapterView<?>arg0){//TODOAuto-generatedmethodstubDisplayToast("你什么也没选!");}});
5.AutoCompleteTextView的使用,代码如下:
Java代码
1.privatestaticfinalString[]autoString={"abcd1","abcd212","32123","32open","32add","android","windows","iphone","linux","mydirve","msnbmw","wocaonimalegebi"};
2.privateArrayAdapter<String>myAdapter1;
3.privateAutoCompleteTextViewmyAutoCompleteTextView;
4.myAdapter1=newArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,autoString);
5.myAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.myAutoCompleteTextView);
6.myAutoCompleteTextView.setAdapter(myAdapter1);
7.6.DatePicker、TimePicker以及Button的使用,代码如下:
8.myCalendar1=Calendar.getInstance();
9.myTextView=(TextView)findViewById(R.id.myTextView);
10.myDatePicker=(DatePicker)findViewById(R.id.datePicker1);
11.myTimePicker=(TimePicker)findViewById(R.id.timePicker1);
12.myButton1=(Button)findViewById(R.id.button1);
13.myButton2=(Button)findViewById(R.id.button2);
14.
15.//将日历初始化为当前系统时间,并设置其事件监听
16.myDatePicker.init(myCalendar1.get(Calendar.YEAR),myCalendar1.get(Calendar.MONTH),myCalendar1.get(Calendar.DAY_OF_MONTH),newDatePicker.OnDateChangedListener(){
17.
18.@Override
19.publicvoidonDateChanged(DatePickerview,intyear,intmonthOfYear,
20.intdayOfMonth){
21.//当日期更改时,在这里处理。
22.//myCalendar1.set(year,month,day);
23.}
24.});
25.//获取TimePicker对象设置为24小时显示。
26.myTimePicker.setIs24HourView(true);
27.//监听时间改变
28.myTimePicker.setOnTimeChangedListener(newTimePicker.OnTimeChangedListener(){
29.
30.@Override
31.publicvoidonTimeChanged(TimePickerview,inthourOfDay,intminute){
32.//TODOAuto-generatedmethodstub
33.//当时间改变时,在这里处理。
34.//myCalendar1.set(year,month,day,hourOfDay,minute);
35.}
36.});
37.//-----------按钮事件---------//
38.myButton1.setOnClickListener(newButton.OnClickListener(){
39.@Override
40.publicvoidonClick(Viewv){
41.//下面的NEW是新建了一个调整日历的对话框
42.newDatePickerDialog(AndroidUnleashed001Activity.this,newDatePickerDialog.OnDateSetListener(){
43.@Override
44.publicvoidonDateSet(DatePickerview,intyear,
45.intmonthOfYear,intdayOfMonth){
46.//TODOAuto-generatedmethodstub
47.//设置日历
48.}
49.},myCalendar1.get(Calendar.YEAR),myCalendar1.get(Calendar.MONTH),myCalendar1.get(Calendar.DAY_OF_MONTH)).show();
50.}
51.});
52.myButton2.setOnClickListener(newButton.OnClickListener(){
53.@Override
54.publicvoidonClick(Viewv){
55.//下面的NEW是新建了一个调整时间的对话框
56.newTimePickerDialog(AndroidUnleashed001Activity.this,newTimePickerDialog.OnTimeSetListener(){
57.
58.@Override
59.publicvoidonTimeSet(TimePickerview,inthourOfDay,intminute){
60.//TODOAuto-generatedmethodstub
61.}
62.},myCalendar1.get(Calendar.HOUR_OF_DAY),myCalendar1.get(Calendar.MINUTE),true).show();
63.}
64.});
65.}
privatestaticfinalString[]autoString={"abcd1","abcd212","32123","32open","32add","android","windows","iphone","linux","mydirve","msnbmw","wocaonimalegebi"};privateArrayAdapter<String>myAdapter1;privateAutoCompleteTextViewmyAutoCompleteTextView;myAdapter1=newArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,autoString);myAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.myAutoCompleteTextView);myAutoCompleteTextView.setAdapter(myAdapter1);6.DatePicker、TimePicker以及Button的使用,代码如下:myCalendar1=Calendar.getInstance();myTextView=(TextView)findViewById(R.id.myTextView);myDatePicker=(DatePicker)findViewById(R.id.datePicker1);myTimePicker=(TimePicker)findViewById(R.id.timePicker1);myButton1=(Button)findViewById(R.id.button1);myButton2=(Button)findViewById(R.id.button2);//将日历初始化为当前系统时间,并设置其事件监听myDatePicker.init(myCalendar1.get(Calendar.YEAR),myCalendar1.get(Calendar.MONTH),myCalendar1.get(Calendar.DAY_OF_MONTH),newDatePicker.OnDateChangedListener(){@OverridepublicvoidonDateChanged(DatePickerview,intyear,intmonthOfYear,intdayOfMonth){//当日期更改时,在这里处理。//myCalendar1.set(year,month,day);}});//获取TimePicker对象设置为24小时显示。myTimePicker.setIs24HourView(true);//监听时间改变myTimePicker.setOnTimeChangedListener(newTimePicker.OnTimeChangedListener(){@OverridepublicvoidonTimeChanged(TimePickerview,inthourOfDay,intminute){//TODOAuto-generatedmethodstub//当时间改变时,在这里处理。//myCalendar1.set(year,month,day,hourOfDay,minute);}});//-----------按钮事件---------//myButton1.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){//下面的NEW是新建了一个调整日历的对话框newDatePickerDialog(AndroidUnleashed001Activity.this,newDatePickerDialog.OnDateSetListener(){@OverridepublicvoidonDateSet(DatePickerview,intyear,intmonthOfYear,intdayOfMonth){//TODOAuto-generatedmethodstub//设置日历}},myCalendar1.get(Calendar.YEAR),myCalendar1.get(Calendar.MONTH),myCalendar1.get(Calendar.DAY_OF_MONTH)).show();}});myButton2.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){//下面的NEW是新建了一个调整时间的对话框newTimePickerDialog(AndroidUnleashed001Activity.this,newTimePickerDialog.OnTimeSetListener(){@OverridepublicvoidonTimeSet(TimePickerview,inthourOfDay,intminute){//TODOAuto-generatedmethodstub}},myCalendar1.get(Calendar.HOUR_OF_DAY),myCalendar1.get(Calendar.MINUTE),true).show();}});}
7.Menu的使用,代码如下:
1.XML方式实现
XML代码可以写成:
Html代码
1.<menuxmlns:android="http://schemas.android.com/apk/res/android">
2.<itemandroid:id="@+id/style_submenu"android:title="笔迹样式">
3.<menu>
4.<groupandroid:id="@+id/style_group"android:checkableBehavior="single">
5.<itemandroid:id="@+id/style"android:orderInCategory="1"android:title="铅笔"/>
6.<itemandroid:id="@+id/style"android:orderInCategory="2"android:title="钢笔"/>
7.<itemandroid:id="@+id/style"android:orderInCategory="3"android:title="毛笔"/>
8.</group>
9.</menu>
10.</item>
11.<itemandroid:id="@+id/color_submenu"android:title="笔迹颜色">
12.<menu>
13.<groupandroid:id="@+id/color_group"android:checkableBehavior="single">
14.<itemandroid:id="@+id/color"android:orderInCategory="0"android:title="蓝色"/>
15.<itemandroid:id="@+id/color"android:orderInCategory="1"android:title="绿色"/>
16.<itemandroid:id="@+id/color"android:orderInCategory="2"android:title="青色"/>
17.<itemandroid:id="@+id/color"android:orderInCategory="3"android:title="红色"/>
18.</group>
19.</menu>
20.</item>
21.<itemandroid:id="@+id/width_submenu"android:title="笔迹粗细">
22.<menu>
23.<groupandroid:id="@+id/width_group"android:checkableBehavior="single">
24.<itemandroid:id="@+id/width"android:orderInCategory="1"android:title="较细"/>
25.<itemandroid:id="@+id/width"android:orderInCategory="2"android:title="细"/>
26.<itemandroid:id="@+id/width"android:orderInCategory="3"android:title="中等"/>
27.<itemandroid:id="@+id/width"android:orderInCategory="4"android:title="粗"/>
28.<itemandroid:id="@+id/width"android:orderInCategory="5"android:title="较粗"/>
29.</group>
30.</menu>
31.</item>
32.<itemandroid:id="@+id/write"android:title="书写"/>
33.<itemandroid:id="@+id/erase"android:title="清除"/>
34.<itemandroid:id="@+id/clear"android:title="清屏"/>
35.</menu>
<menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/style_submenu"android:title="笔迹样式"><menu><groupandroid:id="@+id/style_group"android:checkableBehavior="single"><itemandroid:id="@+id/style"android:orderInCategory="1"android:title="铅笔"/><itemandroid:id="@+id/style"android:orderInCategory="2"android:title="钢笔"/><itemandroid:id="@+id/style"android:orderInCategory="3"android:title="毛笔"/></group></menu></item><itemandroid:id="@+id/color_submenu"android:title="笔迹颜色"><menu><groupandroid:id="@+id/color_group"android:checkableBehavior="single"><itemandroid:id="@+id/color"android:orderInCategory="0"android:title="蓝色"/><itemandroid:id="@+id/color"android:orderInCategory="1"android:title="绿色"/><itemandroid:id="@+id/color"android:orderInCategory="2"android:title="青色"/><itemandroid:id="@+id/color"android:orderInCategory="3"android:title="红色"/></group></menu></item><itemandroid:id="@+id/width_submenu"android:title="笔迹粗细"><menu><groupandroid:id="@+id/width_group"android:checkableBehavior="single"><itemandroid:id="@+id/width"android:orderInCategory="1"android:title="较细"/><itemandroid:id="@+id/width"android:orderInCategory="2"android:title="细"/><itemandroid:id="@+id/width"android:orderInCategory="3"android:title="中等"/><itemandroid:id="@+id/width"android:orderInCategory="4"android:title="粗"/><itemandroid:id="@+id/width"android:orderInCategory="5"android:title="较粗"/></group></menu></item><itemandroid:id="@+id/write"android:title="书写"/><itemandroid:id="@+id/erase"android:title="清除"/><itemandroid:id="@+id/clear"android:title="清屏"/></menu>
JAVA代码:
Java代码
1.@Override
2.publicbooleanonCreateOptionsMenu(Menumenu){
3.MenuInflaterinflater=getMenuInflater();
4.inflater.inflate(R.menu.menu,menu);
5.returntrue;
6.}
7.@Override
8.publicbooleanonOptionsItemSelected(MenuItemitem){
9.intitem_id=item.getItemId();
10.switch(item_id)
11.{
12.caseR.id.about1:
13.Intentintent=newIntent();
14.
15.intent.setClass(AndroidUnleashed001Activity.this,Activity02.class);
16.startActivity(intent);
17.AndroidUnleashed001Activity.this.finish();
18.break;
19.caseR.id.about2:
20.AndroidUnleashed001Activity.this.finish();
21.break;
22.}
23.returntrue;
24.}
25..Menu.add方法实现
26.publicbooleanonCreateOptionsMenu(Menumenu){
27.//MenuInflaterinflater=getMenuInflater();
28.//inflater.inflate(R.menu.menu,menu);
29.menu.add(0,0,0,R.string.app_name);
30.menu.add(0,1,1,R.string.hello);
31.returntrue;
32.}
@OverridepublicbooleanonCreateOptionsMenu(Menumenu){MenuInflaterinflater=getMenuInflater();inflater.inflate(R.menu.menu,menu);returntrue;}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){intitem_id=item.getItemId();switch(item_id){caseR.id.about1:Intentintent=newIntent();intent.setClass(AndroidUnleashed001Activity.this,Activity02.class);startActivity(intent);AndroidUnleashed001Activity.this.finish();break;caseR.id.about2:AndroidUnleashed001Activity.this.finish();break;}returntrue;}1.Menu.add方法实现publicbooleanonCreateOptionsMenu(Menumenu){//MenuInflaterinflater=getMenuInflater();//inflater.inflate(R.menu.menu,menu);menu.add(0,0,0,R.string.app_name);menu.add(0,1,1,R.string.hello);returntrue;}
8.弹出对话框的使用,代码如下:
这个功能为弹出对话框,点击确定后弹出登录对话框,然后点击登录后弹出多线程对话框。
Java代码
1.Dialogdialog=newAlertDialog.Builder(AndroidUnleashed001Activity.this).setTitle("登录提示").setMessage("这里需要登录").setPositiveButton("确定",newDialogInterface.OnClickListener(){
2.@Override
3.publicvoidonClick(DialogInterfacedialog,intwhich){
4.//点击确定转向登陆框
5.LayoutInflaterfactory=LayoutInflater.from(AndroidUnleashed001Activity.this);
6.//得到自定义对话框
7.finalViewDialogView=factory.inflate(R.layout.pop,null);
8.//创建对话框
9.AlertDialogdlg=newAlertDialog.Builder(AndroidUnleashed001Activity.this).setTitle("登录框").setView(DialogView).setPositiveButton("确定",newDialogInterface.OnClickListener(){
10.privateProgressDialogmyProgressDialog;
11.@Override
12.publicvoidonClick(DialogInterfacedialog,intwhich){
13.//当输入完成后,点击确定开始登录。
14.myProgressDialog=ProgressDialog.show(AndroidUnleashed001Activity.this,"请等待","正在为您登陆",true);
15.newThread(){
16.publicvoidrun(){
17.try{
18.sleep(3000);
19.}
20.catch(Exceptione){
21.e.printStackTrace();
22.}
23.finally{
24.myProgressDialog.dismiss();
25.}
26.}
27.}.start();
28.}
29.}).setNegativeButton("取消",newDialogInterface.OnClickListener(){
30.
31.@Override
32.publicvoidonClick(DialogInterfacedialog,intwhich){
33.//TODOAuto-generatedmethodstub
34.AndroidUnleashed001Activity.this.finish();
35.}
36.}).create();
37.dlg.show();
38.}
39.}).setNegativeButton("退出",newDialogInterface.OnClickListener(){
40.
41.@Override
42.publicvoidonClick(DialogInterfacedialog,intwhich){
43.//TODOAuto-generatedmethodstub
44.AndroidUnleashed001Activity.this.finish();
45.}
46.}).create();
47.dialog.show();
Dialogdialog=newAlertDialog.Builder(AndroidUnleashed001Activity.this).setTitle("登录提示").setMessage("这里需要登录").setPositiveButton("确定",newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//点击确定转向登陆框LayoutInflaterfactory=LayoutInflater.from(AndroidUnleashed001Activity.this);//得到自定义对话框finalViewDialogView=factory.inflate(R.layout.pop,null);//创建对话框AlertDialogdlg=newAlertDialog.Builder(AndroidUnleashed001Activity.this).setTitle("登录框").setView(DialogView).setPositiveButton("确定",newDialogInterface.OnClickListener(){privateProgressDialogmyProgressDialog;@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//当输入完成后,点击确定开始登录。myProgressDialog=ProgressDialog.show(AndroidUnleashed001Activity.this,"请等待","正在为您登陆",true);newThread(){publicvoidrun(){try{sleep(3000);}catch(Exceptione){e.printStackTrace();}finally{myProgressDialog.dismiss();}}}.start();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubAndroidUnleashed001Activity.this.finish();}}).create();dlg.show();}}).setNegativeButton("退出",newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubAndroidUnleashed001Activity.this.finish();}}).create();dialog.show();
9.ImageView的使用,代码如下:
Java代码
1.myImageButton1.setImageDrawable(getResources().getDrawable(R.drawable.logo2));
2.
3.myImageButton1.setOnClickListener(newButton.OnClickListener(){
4.
5.@Override
6.publicvoidonClick(Viewv){
7.//TODOAuto-generatedmethodstub
8.Dialogmydialog=newAlertDialog.Builder(AndroidUnleashed003Activity.this).setTitle("提示")
9..setMessage("我是ImageButton1").setPositiveButton("确定",newDialogInterface.OnClickListener(){
10.
11.@Override
12.publicvoidonClick(DialogInterfacedialog,intwhich){
13.//TODOAuto-generatedmethodstub
14.
15.}
16.}).create();
17.mydialog.show();
18.}
19.});
myImageButton1.setImageDrawable(getResources().getDrawable(R.drawable.logo2));myImageButton1.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubDialogmydialog=newAlertDialog.Builder(AndroidUnleashed003Activity.this).setTitle("提示").setMessage("我是ImageButton1").setPositiveButton("确定",newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstub}}).create();mydialog.show();}});
注意:如果要去掉ImageButton后面的背景色,使得ImageButton看起来更漂亮一些而不是有个边,则需要做:在XML文件中的属性里添加:“android:background="#00000000"”
10.Gallery的使用,拖动效果很炫,代码如下:
Java代码
1.GallerymyGallery=(Gallery)findViewById(R.id.myGallery);
2.//添加ImageAdapter给Gallery对象
3.myGallery.setAdapter(newImageAdapter(this));
4.myGallery.setBackgroundResource(R.drawable.background);
5.myGallery.setOnItemClickListener(newOnItemClickListener(){
6.@Override
7.publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
8.longarg3){
9.//TODOAuto-generatedmethodstub
10.Toast.makeText(AndroidUnleashed003Activity.this,"你选择了"+(arg2+1)+"号图片",Toast.LENGTH_SHORT).show();
11.}
12.});
13.}
14.//另外,需要存放图片,存放图片的容器,需要使用一个类实现,这个类是ImageAdapter,继承于BaseAdapter。
15.publicclassImageAdapterextendsBaseAdapter{
16.privateContextmyContext;
17.privateInteger[]mImageIds={
18.R.drawable.ic_launcher,
19.R.drawable.icon,
20.R.drawable.logo,
21.R.drawable.logo2,
22.R.drawable.logo,
23.R.drawable.ic_launcher,
24.R.drawable.icon,
25.R.drawable.logo,
26.};
27.publicImageAdapter(Contextc){
28.myContext=c;
29.}
30.@Override
31.//获取图片的个数
32.publicintgetCount(){
33.//TODOAuto-generatedmethodstub
34.returnmImageIds.length;
35.}
36.@Override
37.//获取图片在库中的位置
38.publicObjectgetItem(intarg0){
39.//TODOAuto-generatedmethodstub
40.returnarg0;
41.}
42.
43.@Override
44.//获取图片在库中的位置
45.publiclonggetItemId(intarg0){
46.//TODOAuto-generatedmethodstub
47.returnarg0;
48.}
49.@Override
50.publicViewgetView(intposition,Viewarg1,ViewGrouparg2){
51.//TODOAuto-generatedmethodstub
52.//returnnull;
53.ImageViewmyImageView=newImageView(myContext);
54.//给ImageView设置资源
55.myImageView.setImageResource(mImageIds[position]);
56.//设置布局图片以120*120显示
57.//myImageView.setLayoutParams(newGallery.LayoutParams(120,120));
58.//设置显示比例类型
59.//myImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
60.returnmyImageView;
61.}
62.}
GallerymyGallery=(Gallery)findViewById(R.id.myGallery);//添加ImageAdapter给Gallery对象myGallery.setAdapter(newImageAdapter(this));myGallery.setBackgroundResource(R.drawable.background);myGallery.setOnItemClickListener(newOnItemClickListener(){@OverridepublicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){//TODOAuto-generatedmethodstubToast.makeText(AndroidUnleashed003Activity.this,"你选择了"+(arg2+1)+"号图片",Toast.LENGTH_SHORT).show();}});}//另外,需要存放图片,存放图片的容器,需要使用一个类实现,这个类是ImageAdapter,继承于BaseAdapter。publicclassImageAdapterextendsBaseAdapter{privateContextmyContext;privateInteger[]mImageIds={R.drawable.ic_launcher,R.drawable.icon,R.drawable.logo,R.drawable.logo2,R.drawable.logo,R.drawable.ic_launcher,R.drawable.icon,R.drawable.logo,};publicImageAdapter(Contextc){myContext=c;}@Override//获取图片的个数publicintgetCount(){//TODOAuto-generatedmethodstubreturnmImageIds.length;}@Override//获取图片在库中的位置publicObjectgetItem(intarg0){//TODOAuto-generatedmethodstubreturnarg0;}@Override//获取图片在库中的位置publiclonggetItemId(intarg0){//TODOAuto-generatedmethodstubreturnarg0;}@OverridepublicViewgetView(intposition,Viewarg1,ViewGrouparg2){//TODOAuto-generatedmethodstub//returnnull;ImageViewmyImageView=newImageView(myContext);//给ImageView设置资源myImageView.setImageResource(mImageIds[position]);//设置布局图片以120*120显示//myImageView.setLayoutParams(newGallery.LayoutParams(120,120));//设置显示比例类型//myImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);returnmyImageView;}}
11.ImageSwitcher的使用,代码如下:
Java代码
1.//首先需要实现接口:
2.extendsActivityimplementsOnClickListener,ViewFactory
3.//具体操作:
4.LinearLayoutmyMainView=newLinearLayout(this);
5.//创建ImageSwitcher对象
6.mySwitcher=newImageSwitcher(this);
7.myMainView.addView(mySwitcher);
8.mySwitcher.setId(BUTTON_SWITCHER_ID);
9.mySwitcher.setFactory(this);
10.mySwitcher.setImageResource(mImageIds[index]);
11.
12.setContentView(myMainView);
13.
14.Buttonnext=newButton(this);
15.next.setId(BUTTON_DOWN_ID);
16.next.setText("下一张");
17.next.setOnClickListener(this);
18.//LinearLayout.LayoutParamsparam=newLinearLayout.LayoutParams(200,200);
19.myMainView.addView(next);
20.
21.Buttonpre=newButton(this);
22.pre.setId(BUTTON_UP_ID);
23.pre.setText("上一张");
24.pre.setOnClickListener(this);
25.myMainView.addView(pre);
26.
27.}
28.
29.privateImageSwitchermySwitcher;
30.privatestaticintindex=0;
31.//上一页,下一页的按钮索引和Switcher的对象ID
32.privatestaticfinalintBUTTON_DOWN_ID=0x123456;
33.privatestaticfinalintBUTTON_UP_ID=0x123457;
34.privatestaticfinalintBUTTON_SWITCHER_ID=0x123458;
35.
36.//实现OnClickListener接口的函数
37.@Override
38.publicvoidonClick(Viewv){
39.//TODOAuto-generatedmethodstub
40.switch(v.getId()){
41.caseBUTTON_DOWN_ID:
42.index++;
43.if(index>=mImageIds.length){
44.index=0;
45.}
46.mySwitcher.setImageResource(mImageIds[index]);
47.break;
48.caseBUTTON_UP_ID:
49.index--;
50.if(index<0){
51.index=mImageIds.length-1;
52.}
53.mySwitcher.setImageResource(mImageIds[index]);
54.break;
55.default:
56.break;
57.}
58.}
59.
60.//实现View.Factory接口的函数
61.@Override
62.publicViewmakeView(){
63.//TODOAuto-generatedmethodstub
64.returnnewImageView(this);
65.}
//首先需要实现接口:extendsActivityimplementsOnClickListener,ViewFactory//具体操作:LinearLayoutmyMainView=newLinearLayout(this);//创建ImageSwitcher对象mySwitcher=newImageSwitcher(this);myMainView.addView(mySwitcher);mySwitcher.setId(BUTTON_SWITCHER_ID);mySwitcher.setFactory(this);mySwitcher.setImageResource(mImageIds[index]);setContentView(myMainView);Buttonnext=newButton(this);next.setId(BUTTON_DOWN_ID);next.setText("下一张");next.setOnClickListener(this);//LinearLayout.LayoutParamsparam=newLinearLayout.LayoutParams(200,200);myMainView.addView(next);Buttonpre=newButton(this);pre.setId(BUTTON_UP_ID);pre.setText("上一张");pre.setOnClickListener(this);myMainView.addView(pre);}privateImageSwitchermySwitcher;privatestaticintindex=0;//上一页,下一页的按钮索引和Switcher的对象IDprivatestaticfinalintBUTTON_DOWN_ID=0x123456;privatestaticfinalintBUTTON_UP_ID=0x123457;privatestaticfinalintBUTTON_SWITCHER_ID=0x123458;//实现OnClickListener接口的函数@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubswitch(v.getId()){caseBUTTON_DOWN_ID:index++;if(index>=mImageIds.length){index=0;}mySwitcher.setImageResource(mImageIds[index]);break;caseBUTTON_UP_ID:index--;if(index<0){index=mImageIds.length-1;}mySwitcher.setImageResource(mImageIds[index]);break;default:break;}}//实现View.Factory接口的函数@OverridepublicViewmakeView(){//TODOAuto-generatedmethodstubreturnnewImageView(this);}
12.GridView的使用,代码如下:
Java代码
1.//这个和Gallery的使用相似,也需要存图片在BaseAdapter中。
2.GridViewmyGridView=(GridView)findViewById(R.id.gridView1);
3.myGridView.setAdapter(newImageAdapter(this));
4.
5.myGridView.setBackgroundResource(R.drawable.background);
6.myGridView.setOnItemClickListener(newOnItemClickListener(){
7.
8.@Override
9.publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
10.longarg3){
11.//TODOAuto-generatedmethodstub
12.Toast.makeText(AndroidUnleashed003Activity.this,"你选择了"+(arg2+1)+"号图片",Toast.LENGTH_SHORT).show();
13.}
14.
15.});
16.}
//这个和Gallery的使用相似,也需要存图片在BaseAdapter中。GridViewmyGridView=(GridView)findViewById(R.id.gridView1);myGridView.setAdapter(newImageAdapter(this));myGridView.setBackgroundResource(R.drawable.background);myGridView.setOnItemClickListener(newOnItemClickListener(){@OverridepublicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){//TODOAuto-generatedmethodstubToast.makeText(AndroidUnleashed003Activity.this,"你选择了"+(arg2+1)+"号图片",Toast.LENGTH_SHORT).show();}});}
12.ScrollView的使用,代码如下:
Java代码
1.//要垂直滚动,使用ScrollView要水平滚动使用HorizontalScrollView。
2.myLayout=(LinearLayout)findViewById(R.id.myLayout);
3.myScllorView=(ScrollView)findViewById(R.id.myScrollView);
4.ButtonmyButton=(Button)findViewById(R.id.myButton);
5.
6.myButton.setOnClickListener(newButton.OnClickListener(){
7.privateintmIndex=-1;
8.@Override
9.publicvoidonClick(Viewv){
10.//TODOAuto-generatedmethodstub
11.TextViewmyTextView=newTextView(ScrollerViewActivity.this);
12.myTextView.setText("TextView"+mIndex);
13.LinearLayout.LayoutParamsp=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
14.//增加一个TextView到现行布局中
15.myLayout.addView(myTextView,p);
16.
17.ButtonmyButtonView=newButton(ScrollerViewActivity.this);
18.myButtonView.setText("Button"+mIndex++);
19.//增加一个Button到线性布局中
20.myLayout.addView(myButtonView,p);
21.//改变默认焦点切换
22.//myButtonView.setOnKeyListener(newButtonKeyListener);
23.//投递一个消息进行滚动
24.//myHandler.post(myScolltoBottom);
25.}
26.
27.});
28.}
//要垂直滚动,使用ScrollView要水平滚动使用HorizontalScrollView。myLayout=(LinearLayout)findViewById(R.id.myLayout);myScllorView=(ScrollView)findViewById(R.id.myScrollView);ButtonmyButton=(Button)findViewById(R.id.myButton);myButton.setOnClickListener(newButton.OnClickListener(){privateintmIndex=-1;@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubTextViewmyTextView=newTextView(ScrollerViewActivity.this);myTextView.setText("TextView"+mIndex);LinearLayout.LayoutParamsp=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);//增加一个TextView到现行布局中myLayout.addView(myTextView,p);ButtonmyButtonView=newButton(ScrollerViewActivity.this);myButtonView.setText("Button"+mIndex++);//增加一个Button到线性布局中myLayout.addView(myButtonView,p);//改变默认焦点切换//myButtonView.setOnKeyListener(newButtonKeyListener);//投递一个消息进行滚动//myHandler.post(myScolltoBottom);}});}
13.ProgressBar的使用,代码如下:
ProgressBarStyleHorizontal(长形进度条)
ProgressBarStyleLarge(圆形进度条)
Java代码
1.publicclassProgressBarAextendsActivity{
2.protectedstaticfinalintGUI_STOP_NOTIFIER=0x108;
3.protectedstaticfinalintGUI_THREADING_NOTIFIER=0x109;
4.privateProgressBarmyProgress1;
5.privateProgressBarmyProgress2;
6.privateButtonmyButton;
7.privateintintCounter;
8./**Calledwhentheactivityisfirstcreated.*/
9.@Override
10.publicvoidonCreate(BundlesavedInstanceState){
11.super.onCreate(savedInstanceState);
12.//设置窗口模式,因为需要显示进度条在标题栏
13.requestWindowFeature(Window.FEATURE_PROGRESS);
14.setProgressBarVisibility(true);
15.setContentView(R.layout.main);
16.//取得ProgressBar
17.myProgress1=(ProgressBar)findViewById(R.id.myProgressBar01);
18.myProgress2=(ProgressBar)findViewById(R.id.myProgressBar02);
19.myButton=(Button)findViewById(R.id.myButton);
20.
21.myProgress1.setIndeterminate(false);
22.myProgress2.setIndeterminate(false);
23.
24.myButton.setOnClickListener(newButton.OnClickListener(){
25.
26.@Override
27.publicvoidonClick(Viewv){
28.//TODOAuto-generatedmethodstub
29.myProgress1.setVisibility(View.VISIBLE);
30.myProgress2.setVisibility(View.VISIBLE);
31.
32.myProgress1.setMax(100);
33.myProgress1.setProgress(0);
34.myProgress2.setProgress(0);
35.
36.//通过线程来改变Progress的值
37.
38.newThread(newRunnable(){
39.
40.@Override
41.publicvoidrun(){
42.//TODOAuto-generatedmethodstub
43.for(inti=0;i<10;i++){
44.try{
45.intCounter=(i+1)*20;
46.Thread.sleep(1000);
47.if(i==4){
48.Messagem=newMessage();
49.m.what=ProgressBarA.GUI_STOP_NOTIFIER;
50.ProgressBarA.this.myMessageHandler.sendMessage(m);
51.break;
52.}
53.else{
54.Messagem=newMessage();
55.m.what=ProgressBarA.GUI_THREADING_NOTIFIER;
56.ProgressBarA.this.myMessageHandler.sendMessage(m);
57.}
58.}
59.catch(Exceptione){
60.e.printStackTrace();
61.}
62.}
63.}
64.
65.}).start();
66.}
67.
68.});
69.}
70.HandlermyMessageHandler=newHandler(){
71.publicvoidhandleMessage(Messagemsg){
72.switch(msg.what){
73.caseProgressBarA.GUI_STOP_NOTIFIER:
74.//myProgress1.setVisibility(View.GONE);
75.//myProgress2.setVisibility(View.GONE);
76.//Thread.currentThread().interrupt();
77.break;
78.caseProgressBarA.GUI_THREADING_NOTIFIER:
79.if(!Thread.currentThread().isInterrupted()){
80.myProgress1.setProgress(intCounter);
81.myProgress1.setProgress(intCounter);
82.setProgress(intCounter*100);//设置标题栏中前景的一个进度条进度值
83.//setSecondaryProgress(intCounter*100);//设置标题栏中后面的一个进度条进度值
84.//Thread.currentThread().interrupt();
85.}
86.break;
87.}
88.super.handleMessage(msg);
89.}
90.};
91.}
publicclassProgressBarAextendsActivity{protectedstaticfinalintGUI_STOP_NOTIFIER=0x108;protectedstaticfinalintGUI_THREADING_NOTIFIER=0x109;privateProgressBarmyProgress1;privateProgressBarmyProgress2;privateButtonmyButton;privateintintCounter;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);//设置窗口模式,因为需要显示进度条在标题栏requestWindowFeature(Window.FEATURE_PROGRESS);setProgressBarVisibility(true);setContentView(R.layout.main);//取得ProgressBarmyProgress1=(ProgressBar)findViewById(R.id.myProgressBar01);myProgress2=(ProgressBar)findViewById(R.id.myProgressBar02);myButton=(Button)findViewById(R.id.myButton);myProgress1.setIndeterminate(false);myProgress2.setIndeterminate(false);myButton.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubmyProgress1.setVisibility(View.VISIBLE);myProgress2.setVisibility(View.VISIBLE);myProgress1.setMax(100);myProgress1.setProgress(0);myProgress2.setProgress(0);//通过线程来改变Progress的值newThread(newRunnable(){@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubfor(inti=0;i<10;i++){try{intCounter=(i+1)*20;Thread.sleep(1000);if(i==4){Messagem=newMessage();m.what=ProgressBarA.GUI_STOP_NOTIFIER;ProgressBarA.this.myMessageHandler.sendMessage(m);break;}else{Messagem=newMessage();m.what=ProgressBarA.GUI_THREADING_NOTIFIER;ProgressBarA.this.myMessageHandler.sendMessage(m);}}catch(Exceptione){e.printStackTrace();}}}}).start();}});}HandlermyMessageHandler=newHandler(){publicvoidhandleMessage(Messagemsg){switch(msg.what){caseProgressBarA.GUI_STOP_NOTIFIER://myProgress1.setVisibility(View.GONE);//myProgress2.setVisibility(View.GONE);//Thread.currentThread().interrupt();break;caseProgressBarA.GUI_THREADING_NOTIFIER:if(!Thread.currentThread().isInterrupted()){myProgress1.setProgress(intCounter);myProgress1.setProgress(intCounter);setProgress(intCounter*100);//设置标题栏中前景的一个进度条进度值//setSecondaryProgress(intCounter*100);//设置标题栏中后面的一个进度条进度值//Thread.currentThread().interrupt();}break;}super.handleMessage(msg);}};}
14.SeekBar的使用,代码如下:
Java代码
1.implementsSeekBar.OnSeekBarChangeListener
2.//实现OnSeekBarChangeListener接口
3.publicvoidonProgressChanged(SeekBarseekBar,intprogress,
4.booleanfromUser){
5.//TODOAuto-generatedmethodstub
6.myProgressText.setText("当前值:"+progress);
7.}
8.@Override
9.publicvoidonStartTrackingTouch(SeekBarseekBar){
10.//TODOAuto-generatedmethodstub
11.myTrackingText.setText("正在调节");
12.}
13.@Override
14.publicvoidonStopTrackingTouch(SeekBarseekBar){
15.//TODOAuto-generatedmethodstub
16.myTrackingText.setText("停止调节");
17.}
18.}
implementsSeekBar.OnSeekBarChangeListener//实现OnSeekBarChangeListener接口publicvoidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser){//TODOAuto-generatedmethodstubmyProgressText.setText("当前值:"+progress);}@OverridepublicvoidonStartTrackingTouch(SeekBarseekBar){//TODOAuto-generatedmethodstubmyTrackingText.setText("正在调节");}@OverridepublicvoidonStopTrackingTouch(SeekBarseekBar){//TODOAuto-generatedmethodstubmyTrackingText.setText("停止调节");}}
15.Notification、NotificationManager的使用,代码如下:
Java代码
1.myIntent=newIntent(Notification01.this,Notification02.class);
2.//主要是设置点击通知时显示内容的类
3.myPendingIntent=PendingIntent.getActivity(Notification01.this,0,myIntent,0);
4.//构造Notification对象
5.myNotification=newNotification();
6.myButton1.setOnClickListener(newButton.OnClickListener(){
7.@Override
8.publicvoidonClick(Viewv){
9.//TODOAuto-generatedmethodstub
10.//设置通知在状态栏显示的图标
11.myNotification.icon=R.drawable.ic_launcher;
12.myNotification.tickerText="Button1的通知内容...";
13.//通知时发出默认的声音
14.myNotification.defaults=Notification.DEFAULT_SOUND;
15.//设置通知显示的参数
16.myNotification.setLatestEventInfo(Notification01.this,"Button1","Button1通知",myPendingIntent);
17.//可以理解为执行这个通知
18.myNotificationManager.notify(0,myNotification);
19.}
20.});
myIntent=newIntent(Notification01.this,Notification02.class);//主要是设置点击通知时显示内容的类myPendingIntent=PendingIntent.getActivity(Notification01.this,0,myIntent,0);//构造Notification对象myNotification=newNotification();myButton1.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstub//设置通知在状态栏显示的图标myNotification.icon=R.drawable.ic_launcher;myNotification.tickerText="Button1的通知内容...";//通知时发出默认的声音myNotification.defaults=Notification.DEFAULT_SOUND;//设置通知显示的参数myNotification.setLatestEventInfo(Notification01.this,"Button1","Button1通知",myPendingIntent);//可以理解为执行这个通知myNotificationManager.notify(0,myNotification);}});
16.ProgressDialog的使用,代码如下:
Java代码
1.myButton01=(Button)findViewById(R.id.Button1);
2.myButton02=(Button)findViewById(R.id.Button2);
3.myButton01.setOnClickListener(newButton.OnClickListener(){
4.@Override
5.publicvoidonClick(Viewv){
6.//TODOAuto-generatedmethodstub
7.//创建ProgressDialog对象
8.myProgressDialog=newProgressDialog(ProgressDialogActivity.this);
9.//设置进度条风格,风格为圆形的,旋转的
10.myProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
11.//设置ProgressDialog标题
12.myProgressDialog.setTitle("提示");
13.//设置ProgressDialog提示信息
14.myProgressDialog.setMessage("这是一个圆形进度条对话框");
15.//设置myProgressDialog标题图标
16.myProgressDialog.setIcon(R.drawable.ic_launcher);
17.//设置myProgressDialog的进度条是否不明确
18.myProgressDialog.setIndeterminate(false);
19.//设置myProgressDialog是否可以按退回键取消
20.myProgressDialog.setCancelable(true);
21.//设置myProgressDialog的一个Button
22.myProgressDialog.setButton("确定",newDialogInterface.OnClickListener(){
23.
24.@Override
25.publicvoidonClick(DialogInterfacedialog,intwhich){
26.//TODOAuto-generatedmethodstub
27.//点击确定取消对话框
28.dialog.cancel();
29.}
30.});
31.//让ProgressDialog显示
32.myProgressDialog.show();
33.}
34.});
35.
36.myButton02.setOnClickListener(newButton.OnClickListener(){
37.@Override
38.publicvoidonClick(Viewv){
39.//TODOAuto-generatedmethodstub
40.myCount=0;
41.//创建ProgressDialog对象
42.myProgressDialog=newProgressDialog(ProgressDialogActivity.this);
43.//设置进度条风格,风格为长形的。
44.myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
45.//设置ProgressDialog标题
46.myProgressDialog.setTitle("提示");
47.//设置ProgressDialog提示信息
48.myProgressDialog.setMessage("这是一个长形进度条对话框");
49.//设置myProgressDialog标题图标
50.myProgressDialog.setIcon(R.drawable.ic_launcher);
51.//设置myProgressDialog进度条进度
52.myProgressDialog.setProgress(100);
53.//设置myProgressDialog的进度条是否不明确
54.myProgressDialog.setIndeterminate(false);
55.//设置myProgressDialog是否可以按退回键取消
56.myProgressDialog.setCancelable(true);
57.//设置myProgressDialog的一个Button
58.myProgressDialog.setButton("确定",newDialogInterface.OnClickListener(){
59.
60.@Override
61.publicvoidonClick(DialogInterfacedialog,intwhich){
62.//TODOAuto-generatedmethodstub
63.//点击确定取消对话框
64.dialog.cancel();
65.}
66.});
67.//让ProgressDialog显示
68.myProgressDialog.show();
69.newThread(){
70.publicvoidrun(){
71.try{
72.while(myCount<=100){
73.//由线程来控制进度
74.myProgressDialog.setProgress(myCount++);
75.Thread.sleep(100);
76.}
77.myProgressDialog.cancel();
78.}
79.catch(InterruptedExceptione){
80.myProgressDialog.cancel();
81.}
82.}
83.}.start();
84.}
85.});
myButton01=(Button)findViewById(R.id.Button1);myButton02=(Button)findViewById(R.id.Button2);myButton01.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstub//创建ProgressDialog对象myProgressDialog=newProgressDialog(ProgressDialogActivity.this);//设置进度条风格,风格为圆形的,旋转的myProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置ProgressDialog标题myProgressDialog.setTitle("提示");//设置ProgressDialog提示信息myProgressDialog.setMessage("这是一个圆形进度条对话框");//设置myProgressDialog标题图标myProgressDialog.setIcon(R.drawable.ic_launcher);//设置myProgressDialog的进度条是否不明确myProgressDialog.setIndeterminate(false);//设置myProgressDialog是否可以按退回键取消myProgressDialog.setCancelable(true);//设置myProgressDialog的一个ButtonmyProgressDialog.setButton("确定",newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstub//点击确定取消对话框dialog.cancel();}});//让ProgressDialog显示myProgressDialog.show();}});myButton02.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubmyCount=0;//创建ProgressDialog对象myProgressDialog=newProgressDialog(ProgressDialogActivity.this);//设置进度条风格,风格为长形的。myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置ProgressDialog标题myProgressDialog.setTitle("提示");//设置ProgressDialog提示信息myProgressDialog.setMessage("这是一个长形进度条对话框");//设置myProgressDialog标题图标myProgressDialog.setIcon(R.drawable.ic_launcher);//设置myProgressDialog进度条进度myProgressDialog.setProgress(100);//设置myProgressDialog的进度条是否不明确myProgressDialog.setIndeterminate(false);//设置myProgressDialog是否可以按退回键取消myProgressDialog.setCancelable(true);//设置myProgressDialog的一个ButtonmyProgressDialog.setButton("确定",newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstub//点击确定取消对话框dialog.cancel();}});//让ProgressDialog显示myProgressDialog.show();newThread(){publicvoidrun(){try{while(myCount<=100){//由线程来控制进度myProgressDialog.setProgress(myCount++);Thread.sleep(100);}myProgressDialog.cancel();}catch(InterruptedExceptione){myProgressDialog.cancel();}}}.start();}});
17.TabWidget的使用,代码如下:
要使用TabWidget,首先要了解TabHost,TabHost是一个用来存放Tab标签的容器。
Java代码
1.publicclassTabHostActivityextendsTabActivity{
2.TabHostmyTabHost;
3.
4./**Calledwhentheactivityisfirstcreated.*/
5.@Override
6.publicvoidonCreate(BundlesavedInstanceState){
7.super.onCreate(savedInstanceState);
8.setContentView(R.layout.main);
9.//获取TabHost对象
10.myTabHost=getTabHost();
11.//为TabHost添加标签,新建一个newTabSpec(newTabSpec)设置其标签和图标(setIndicator)设置内容(setContent)
12.myTabHost.addTab(myTabHost.newTabSpec("tabHost1").setIndicator("TAB1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.myTabContent1));
13.myTabHost.addTab(myTabHost.newTabSpec("tabHost2").setIndicator("TAB2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.myTabContent2));
14.myTabHost.addTab(myTabHost.newTabSpec("tabHost3").setIndicator("TAB3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.myTabContent3));
15.//设置TabHost的背景颜色
16.myTabHost.setBackgroundColor(Color.argb(150,22,70,150));
17.//设置当前显示哪个标签
18.myTabHost.setCurrentTab(1);
19.//标签切换事件处理setOnTabChangedListener
20.myTabHost.setOnTabChangedListener(newTabHost.OnTabChangeListener(){
21.
22.@Override
23.publicvoidonTabChanged(StringtabId){
24.//TODOAuto-generatedmethodstub
25.Dialogdialog=newAlertDialog.Builder(TabHostActivity.this).setTitle("提示").setMessage("当前选中"+tabId).setPositiveButton("确定",newDialogInterface.OnClickListener(){
26.
27.@Override
28.publicvoidonClick(DialogInterfacedialog,intwhich){
29.//TODOAuto-generatedmethodstub
30.dialog.cancel();
31.}
32.}).create();
33.dialog.show();
34.}
35.});
36.}
37.}