- 浏览: 9164 次
- 性别:
- 来自: 广州
文章分类
最新评论
/*
* Android开发之《Android应用开发揭秘》UI事件汇总
* 北京Android俱乐部群:167839253
* Created on: 2011-12-01
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
思想跑毛是很可怕的,最近去尝试了下创业,结果碰了一鼻子灰,公司乱七八糟的项目搞的焦头烂额。各种没有心情。还好,现在沉淀下来,继续 android开发的征程。
以下是Android应用开发揭秘这本书第四章的关于UI的事件处理的总结。基本上都是可以复用的代码。存着日后方便。
1.EditView控件的setOnKeyListener方法。点击上下左右键或者中间键的时候可以出发该方法。代码如下。
Java代码
1. myEditView.setOnKeyListener(new EditText.OnKeyListener(){
2.
3. @Override
4. public boolean onKey(View v, int keyCode, KeyEvent event) {
5. myTextView.setText("文本框中内容是:"+myEditView.getText().toString());
6. return false;// TODO Auto-generated method stub
7. }
8. });
myEditView.setOnKeyListener(new EditText.OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { myTextView.setText("文本框中内容是:"+myEditView.getText().toString()); return false;// TODO Auto-generated method stub } });
2.RadioButton控件和RadioGroup控件的组合使用,代码如下:
Java代码
1. myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
2.
3. @Override
4. public void onCheckedChanged(RadioGroup group, int checkedId) {
5. // TODO Auto-generated method stub
6. if(checkedId == myRadioButton3.getId())
7. {
8. DisplayToast("正确答案:"+myRadioButton3.getText()+"恭喜你回答正确");
9. }
10. else
11. {
12. DisplayToast("回答错误");
13. }
14. }
15. );
myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(checkedId == myRadioButton3.getId()) { DisplayToast("正确答案:"+myRadioButton3.getText()+"恭喜你回答正确"); } else { DisplayToast("回答错误"); } } });
3.CheckBox控件的使用,代码如下:
Java代码
1. myCheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
2. @Override
3. public void onCheckedChanged(CompoundButton buttonView,
4. boolean isChecked) {
5. // TODO Auto-generated method stub
6. if(myCheckBox1.isChecked()){
7. DisplayToast("你选择了:"+myCheckBox1.getText());
8. }
9. }
10. });
myCheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(myCheckBox1.isChecked()){ DisplayToast("你选择了:"+myCheckBox1.getText()); } } });
4.Button控件的使用,代码如下:
Java代码
1. myButton1.setOnClickListener(new Button.OnClickListener(){
2. @Override
3. public void onClick(View v) {
4. }
5. });
myButton1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { } });
4.Spinner控件的使用,代码如下:
Java代码
1. //Spinner控件需要跟ArrayAdapter绑定使用。
2. private static final String[] m_Countries = {"A型","B型","O型","AB型","未知类型"};
3. private Spinner mySpinner;
4. private ArrayAdapter<String> myAdapter;
5. mySpinner = (Spinner)findViewById(R.id.mySpinner1);
6. myAdapter = new ArrayAdapter<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(new Spinner.OnItemSelectedListener(){
13. @Override
14. public void onItemSelected(AdapterView<?> arg0, View arg1,
15. int arg2, long arg3) {
16. // TODO Auto-generated method stub
17. myTextView.setText("你的血型是"+ m_Countries[arg2]);
18. arg0.setVisibility(View.VISIBLE);
19. }
20. @Override
21. public void onNothingSelected(AdapterView<?> arg0) {
22. // TODO Auto-generated method stub
23. DisplayToast("你什么也没选!");
24. }
25. });
//Spinner 控件需要跟ArrayAdapter绑定使用。 private static final String[] m_Countries = {"A型","B型","O型","AB型","未知类型"}; private Spinner mySpinner; private ArrayAdapter<String> myAdapter; mySpinner = (Spinner)findViewById(R.id.mySpinner1); myAdapter = new ArrayAdapter<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(new Spinner.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub myTextView.setText("你的血型是"+ m_Countries[arg2]); arg0.setVisibility(View.VISIBLE); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub DisplayToast("你什么也没选!"); } });
5.AutoCompleteTextView的使用,代码如下:
Java代码
1. private static final String[] autoString = {"abcd1","abcd212","32123","32open","32add","android","windows","iphone","linux","mydirve","msnbmw","wocaonimalegebi"};
2. private ArrayAdapter<String> myAdapter1;
3. private AutoCompleteTextView myAutoCompleteTextView;
4. myAdapter1 = new ArrayAdapter<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), new DatePicker.OnDateChangedListener() {
17.
18. @Override
19. public void onDateChanged(DatePicker view, int year, int monthOfYear,
20. int dayOfMonth) {
21. // 当日期更改时,在这里处理。
22. // myCalendar1.set(year, month, day);
23. }
24. });
25. //获取TimePicker对象设置为24小时显示。
26. myTimePicker.setIs24HourView(true);
27. //监听时间改变
28. myTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
29.
30. @Override
31. public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
32. // TODO Auto-generated method stub
33. // 当时间改变时,在这里处理。
34. //myCalendar1.set(year, month, day, hourOfDay, minute);
35. }
36. });
37. //-----------按钮事件---------//
38. myButton1.setOnClickListener(new Button.OnClickListener(){
39. @Override
40. public void onClick(View v) {
41. // 下面的NEW 是新建了一个调整日历的对话框
42. new DatePickerDialog(AndroidUnleashed001Activity.this,new DatePickerDialog.OnDateSetListener(){
43. @Override
44. public void onDateSet(DatePicker view, int year,
45. int monthOfYear, int dayOfMonth) {
46. // TODO Auto-generated method stub
47. //设置日历
48. }
49. },myCalendar1.get(Calendar.YEAR),myCalendar1.get(Calendar.MONTH), myCalendar1.get(Calendar.DAY_OF_MONTH)).show();
50. }
51. });
52. myButton2.setOnClickListener(new Button.OnClickListener(){
53. @Override
54. public void onClick(View v) {
55. //下面的NEW 是新建了一个调整时间的对话框
56. new TimePickerDialog(AndroidUnleashed001Activity.this, new TimePickerDialog.OnTimeSetListener() {
57.
58. @Override
59. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
60. // TODO Auto-generated method stub
61. }
62. }, myCalendar1.get(Calendar.HOUR_OF_DAY), myCalendar1.get(Calendar.MINUTE),true).show();
63. }
64. });
65. }
private static final String[] autoString = {"abcd1","abcd212","32123","32open","32add","android","windows","iphone","linux","mydirve","msnbmw","wocaonimalegebi"}; private ArrayAdapter<String> myAdapter1; private AutoCompleteTextView myAutoCompleteTextView; myAdapter1 = new ArrayAdapter<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), new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // 当日期更改时,在这里处理。 // myCalendar1.set(year, month, day); } }); //获取TimePicker对象设置为24小时显示。 myTimePicker.setIs24HourView(true); //监听时间改变 myTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub // 当时间改变时,在这里处理。 //myCalendar1.set(year, month, day, hourOfDay, minute); } }); //-----------按钮事件---------// myButton1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // 下面的NEW 是新建了一个调整日历的对话框 new DatePickerDialog(AndroidUnleashed001Activity.this,new DatePickerDialog.OnDateSetListener(){ @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub //设置日历 } },myCalendar1.get(Calendar.YEAR),myCalendar1.get(Calendar.MONTH), myCalendar1.get(Calendar.DAY_OF_MONTH)).show(); } }); myButton2.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { //下面的NEW 是新建了一个调整时间的对话框 new TimePickerDialog(AndroidUnleashed001Activity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub } }, myCalendar1.get(Calendar.HOUR_OF_DAY), myCalendar1.get(Calendar.MINUTE),true).show(); } }); }
7.Menu的使用,代码如下:
1.XML方式实现
XML代码可以写成:
Html代码
1. <menu xmlns:android="http://schemas.android.com/apk/res/android">
2. <item android:id="@+id/style_submenu" android:title="笔迹样式">
3. <menu>
4. <group android:id="@+id/style_group" android:checkableBehavior="single">
5. <item android:id="@+id/style" android:orderInCategory="1" android:title="铅笔" />
6. <item android:id="@+id/style" android:orderInCategory="2" android:title="钢笔" />
7. <item android:id="@+id/style" android:orderInCategory="3" android:title="毛笔" />
8. </group>
9. </menu>
10. </item>
11. <item android:id="@+id/color_submenu" android:title="笔迹颜色">
12. <menu>
13. <group android:id="@+id/color_group" android:checkableBehavior="single">
14. <item android:id="@+id/color" android:orderInCategory="0" android:title="蓝色" />
15. <item android:id="@+id/color" android:orderInCategory="1" android:title="绿色" />
16. <item android:id="@+id/color" android:orderInCategory="2" android:title="青色" />
17. <item android:id="@+id/color" android:orderInCategory="3" android:title="红色" />
18. </group>
19. </menu>
20. </item>
21. <item android:id="@+id/width_submenu" android:title="笔迹粗细">
22. <menu>
23. <group android:id="@+id/width_group" android:checkableBehavior="single">
24. <item android:id="@+id/width" android:orderInCategory="1" android:title="较细" />
25. <item android:id="@+id/width" android:orderInCategory="2" android:title="细" />
26. <item android:id="@+id/width" android:orderInCategory="3" android:title="中等" />
27. <item android:id="@+id/width" android:orderInCategory="4" android:title="粗" />
28. <item android:id="@+id/width" android:orderInCategory="5" android:title="较粗" />
29. </group>
30. </menu>
31. </item>
32. <item android:id="@+id/write" android:title="书写" />
33. <item android:id="@+id/erase" android:title="清除" />
34. <item android:id="@+id/clear" android:title="清屏" />
35. </menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/style_submenu" android:title="笔迹样式"> <menu> <group android:id="@+id/style_group" android:checkableBehavior="single"> <item android:id="@+id/style" android:orderInCategory="1" android:title="铅笔" /> <item android:id="@+id/style" android:orderInCategory="2" android:title="钢笔" /> <item android:id="@+id/style" android:orderInCategory="3" android:title="毛笔" /> </group> </menu> </item> <item android:id="@+id/color_submenu" android:title="笔迹颜色"> <menu> <group android:id="@+id/color_group" android:checkableBehavior="single"> <item android:id="@+id/color" android:orderInCategory="0" android:title="蓝色" /> <item android:id="@+id/color" android:orderInCategory="1" android:title="绿色" /> <item android:id="@+id/color" android:orderInCategory="2" android:title="青色" /> <item android:id="@+id/color" android:orderInCategory="3" android:title="红色" /> </group> </menu> </item> <item android:id="@+id/width_submenu" android:title="笔迹粗细"> <menu> <group android:id="@+id/width_group" android:checkableBehavior="single"> <item android:id="@+id/width" android:orderInCategory="1" android:title="较细" /> <item android:id="@+id/width" android:orderInCategory="2" android:title="细" /> <item android:id="@+id/width" android:orderInCategory="3" android:title="中等" /> <item android:id="@+id/width" android:orderInCategory="4" android:title="粗" /> <item android:id="@+id/width" android:orderInCategory="5" android:title="较粗" /> </group> </menu> </item> <item android:id="@+id/write" android:title="书写" /> <item android:id="@+id/erase" android:title="清除" /> <item android:id="@+id/clear" android:title="清屏" /> </menu>
JAVA代码:
Java代码
1. @Override
2. public boolean onCreateOptionsMenu(Menu menu){
3. MenuInflater inflater = getMenuInflater();
4. inflater.inflate(R.menu.menu, menu);
5. return true;
6. }
7. @Override
8. public boolean onOptionsItemSelected(MenuItem item){
9. int item_id = item.getItemId();
10. switch (item_id)
11. {
12. case R.id.about1:
13. Intent intent = new Intent();
14.
15. intent.setClass(AndroidUnleashed001Activity.this, Activity02.class);
16. startActivity(intent);
17. AndroidUnleashed001Activity.this.finish();
18. break;
19. case R.id.about2:
20. AndroidUnleashed001Activity.this.finish();
21. break;
22. }
23. return true;
24. }
25. .Menu.add 方法实现
26. public boolean onCreateOptionsMenu(Menu menu){
27. //MenuInflater inflater = 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. return true;
32. }
@Override public boolean onCreateOptionsMenu(Menu menu){ MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ int item_id = item.getItemId(); switch (item_id) { case R.id.about1: Intent intent = new Intent(); intent.setClass(AndroidUnleashed001Activity.this, Activity02.class); startActivity(intent); AndroidUnleashed001Activity.this.finish(); break; case R.id.about2: AndroidUnleashed001Activity.this.finish(); break; } return true; } 1.Menu.add方法实现 public boolean onCreateOptionsMenu(Menu menu){ //MenuInflater inflater = getMenuInflater(); //inflater.inflate(R.menu.menu, menu); menu.add(0,0,0,R.string.app_name); menu.add(0,1,1,R.string.hello); return true; }
8.弹出对话框的使用,代码如下:
这个功能为弹出对话框,点击确定后弹出登录对话框,然后点击登录后弹出多线程对话框。
Java代码
1. Dialog dialog = new AlertDialog.Builder(AndroidUnleashed001Activity.this).setTitle(" 登录提示").setMessage("这里需要登录").setPositiveButton("确定", new DialogInterface.OnClickListener() {
2. @Override
3. public void onClick(DialogInterface dialog, int which) {
4. // 点击确定转向登陆框
5. LayoutInflater factory = LayoutInflater.from(AndroidUnleashed001Activity.this);
6. //得到自定义对话框
7. final View DialogView = factory.inflate(R.layout.pop, null);
8. //创建对话框
9. AlertDialog dlg = new AlertDialog.Builder(AndroidUnleashed001Activity.this).setTitle(" 登录框").setView(DialogView).setPositiveButton("确定", new DialogInterface.OnClickListener() {
10. private ProgressDialog myProgressDialog;
11. @Override
12. public void onClick(DialogInterface dialog, int which) {
13. // 当输入完成后,点击确定开始登录。
14. myProgressDialog = ProgressDialog.show(AndroidUnleashed001Activity.this, "请等待", "正在为您登陆",true);
15. new Thread(){
16. public void run(){
17. try{
18. sleep(3000);
19. }
20. catch(Exception e){
21. e.printStackTrace();
22. }
23. finally{
24. myProgressDialog.dismiss();
25. }
26. }
27. }.start();
28. }
29. }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
30.
31. @Override
32. public void onClick(DialogInterface dialog, int which) {
33. // TODO Auto-generated method stub
34. AndroidUnleashed001Activity.this.finish();
35. }
36. }).create();
37. dlg.show();
38. }
39. }).setNegativeButton("退出", new DialogInterface.OnClickListener() {
40.
41. @Override
42. public void onClick(DialogInterface dialog, int which) {
43. // TODO Auto-generated method stub
44. AndroidUnleashed001Activity.this.finish();
45. }
46. }).create();
47. dialog.show();
Dialog dialog = new AlertDialog.Builder(AndroidUnleashed001Activity.this).setTitle("登录提示").setMessage("这里需要登录").setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击确定转向登陆框 LayoutInflater factory = LayoutInflater.from(AndroidUnleashed001Activity.this); //得到自定义对话框 final View DialogView = factory.inflate(R.layout.pop, null); //创建对话框 AlertDialog dlg = new AlertDialog.Builder(AndroidUnleashed001Activity.this).setTitle("登录框").setView(DialogView).setPositiveButton("确定", new DialogInterface.OnClickListener() { private ProgressDialog myProgressDialog; @Override public void onClick(DialogInterface dialog, int which) { // 当输入完成后,点击确定开始登录。 myProgressDialog = ProgressDialog.show(AndroidUnleashed001Activity.this, "请等待", "正在为您登陆",true); new Thread(){ public void run(){ try{ sleep(3000); } catch(Exception e){ e.printStackTrace(); } finally{ myProgressDialog.dismiss(); } } }.start(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub AndroidUnleashed001Activity.this.finish(); } }).create(); dlg.show(); } }).setNegativeButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub AndroidUnleashed001Activity.this.finish(); } }).create(); dialog.show();
9.ImageView的使用,代码如下:
Java代码
1. myImageButton1.setImageDrawable(getResources().getDrawable(R.drawable.logo2));
2.
3. myImageButton1.setOnClickListener(new Button.OnClickListener(){
4.
5. @Override
6. public void onClick(View v) {
7. // TODO Auto-generated method stub
8. Dialog mydialog = new AlertDialog.Builder(AndroidUnleashed003Activity.this).setTitle(" 提示")
9. .setMessage("我是ImageButton1").setPositiveButton("确定",new DialogInterface.OnClickListener() {
10.
11. @Override
12. public void onClick(DialogInterface dialog, int which) {
13. // TODO Auto-generated method stub
14.
15. }
16. }).create();
17. mydialog.show();
18. }
19. });
myImageButton1.setImageDrawable(getResources().getDrawable(R.drawable.logo2)); myImageButton1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Dialog mydialog = new AlertDialog.Builder(AndroidUnleashed003Activity.this).setTitle("提示") .setMessage("我是ImageButton1").setPositiveButton("确定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).create(); mydialog.show(); } });
注意:如果要去掉ImageButton后面的背景色,使得ImageButton看起来更漂亮一些而不是有个边,则需要做:在XML文件中的属性里添加:“android:background="#00000000"”
10.Gallery的使用,拖动效果很炫,代码如下:
Java代码
1. Gallery myGallery = (Gallery)findViewById(R.id.myGallery);
2. //添加ImageAdapter给Gallery对象
3. myGallery.setAdapter(new ImageAdapter(this));
4. myGallery.setBackgroundResource(R.drawable.background);
5. myGallery.setOnItemClickListener(new OnItemClickListener(){
6. @Override
7. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
8. long arg3) {
9. // TODO Auto-generated method stub
10. Toast.makeText(AndroidUnleashed003Activity.this, "你选择了"+(arg2+1)+" 号图片 ", Toast.LENGTH_SHORT).show();
11. }
12. });
13. }
14. //另外,需要存放图片,存放图片的容器,需要使用一个类实现,这个类是ImageAdapter,继承于 BaseAdapter。
15. public class ImageAdapter extends BaseAdapter{
16. private Context myContext;
17. private Integer[] 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. public ImageAdapter(Context c){
28. myContext = c;
29. }
30. @Override
31. //获取图片的个数
32. public int getCount() {
33. // TODO Auto-generated method stub
34. return mImageIds.length;
35. }
36. @Override
37. //获取图片在库中的位置
38. public Object getItem(int arg0) {
39. // TODO Auto-generated method stub
40. return arg0;
41. }
42.
43. @Override
44. //获取图片在库中的位置
45. public long getItemId(int arg0) {
46. // TODO Auto-generated method stub
47. return arg0;
48. }
49. @Override
50. public View getView(int position, View arg1, ViewGroup arg2) {
51. // TODO Auto-generated method stub
52. //return null;
53. ImageView myImageView = new ImageView(myContext);
54. //给ImageView设置资源
55. myImageView.setImageResource(mImageIds[position]);
56. //设置布局图片以120*120显示
57. //myImageView.setLayoutParams(new Gallery.LayoutParams(120,120));
58. //设置显示比例类型
59. //myImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
60. return myImageView;
61. }
62. }
Gallery myGallery = (Gallery)findViewById(R.id.myGallery); //添加ImageAdapter给Gallery对象 myGallery.setAdapter(new ImageAdapter(this)); myGallery.setBackgroundResource(R.drawable.background); myGallery.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(AndroidUnleashed003Activity.this, "你选择了"+(arg2+1)+" 号图片 ", Toast.LENGTH_SHORT).show(); } }); } //另外,需要存放图片,存放图片的容器,需要使用一个类实现,这个类是ImageAdapter,继承于BaseAdapter。 public class ImageAdapter extends BaseAdapter{ private Context myContext; private Integer[] 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, }; public ImageAdapter(Context c){ myContext = c; } @Override //获取图片的个数 public int getCount() { // TODO Auto-generated method stub return mImageIds.length; } @Override //获取图片在库中的位置 public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override //获取图片在库中的位置 public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub //return null; ImageView myImageView = new ImageView(myContext); //给ImageView设置资源 myImageView.setImageResource(mImageIds[position]); //设置布局图片以120*120显示 //myImageView.setLayoutParams(new Gallery.LayoutParams(120,120)); //设置显示比例类型 //myImageView.setScaleType(ImageView.ScaleType.FIT_CENTER); return myImageView; } }
11.ImageSwitcher的使用,代码如下:
Java代码
1. //首先需要实现接口:
2. extends Activity implements OnClickListener,ViewFactory
3. //具体操作:
4. LinearLayout myMainView = new LinearLayout(this);
5. //创建ImageSwitcher对象
6. mySwitcher = new ImageSwitcher(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. Button next = new Button(this);
15. next.setId(BUTTON_DOWN_ID);
16. next.setText("下一张");
17. next.setOnClickListener(this);
18. //LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(200,200);
19. myMainView.addView(next);
20.
21. Button pre = new Button(this);
22. pre.setId(BUTTON_UP_ID);
23. pre.setText("上一张");
24. pre.setOnClickListener(this);
25. myMainView.addView(pre);
26.
27. }
28.
29. private ImageSwitcher mySwitcher;
30. private static int index = 0;
31. //上一页,下一页的按钮索引和Switcher的对象ID
32. private static final int BUTTON_DOWN_ID = 0x123456;
33. private static final int BUTTON_UP_ID = 0x123457;
34. private static final int BUTTON_SWITCHER_ID = 0x123458;
35.
36. //实现OnClickListener接口的函数
37. @Override
38. public void onClick(View v) {
39. // TODO Auto-generated method stub
40. switch(v.getId()){
41. case BUTTON_DOWN_ID:
42. index++;
43. if(index>= mImageIds.length){
44. index = 0;
45. }
46. mySwitcher.setImageResource(mImageIds[index]);
47. break;
48. case BUTTON_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. public View makeView() {
63. // TODO Auto-generated method stub
64. return new ImageView(this);
65. }
//首先需要实现接口: extends Activity implements OnClickListener,ViewFactory //具体操作: LinearLayout myMainView = new LinearLayout(this); //创建ImageSwitcher对象 mySwitcher = new ImageSwitcher(this); myMainView.addView(mySwitcher); mySwitcher.setId(BUTTON_SWITCHER_ID); mySwitcher.setFactory(this); mySwitcher.setImageResource(mImageIds[index]); setContentView(myMainView); Button next = new Button(this); next.setId(BUTTON_DOWN_ID); next.setText("下一张"); next.setOnClickListener(this); //LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(200,200); myMainView.addView(next); Button pre = new Button(this); pre.setId(BUTTON_UP_ID); pre.setText("上一张"); pre.setOnClickListener(this); myMainView.addView(pre); } private ImageSwitcher mySwitcher; private static int index = 0; //上一页,下一页的按钮索引和Switcher的对象ID private static final int BUTTON_DOWN_ID = 0x123456; private static final int BUTTON_UP_ID = 0x123457; private static final int BUTTON_SWITCHER_ID = 0x123458; //实现OnClickListener接口的函数 @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case BUTTON_DOWN_ID: index++; if(index>= mImageIds.length){ index = 0; } mySwitcher.setImageResource(mImageIds[index]); break; case BUTTON_UP_ID: index--; if(index<0){ index = mImageIds.length-1; } mySwitcher.setImageResource(mImageIds[index]); break; default: break; } } //实现View.Factory接口的函数 @Override public View makeView() { // TODO Auto-generated method stub return new ImageView(this); }
12.GridView的使用,代码如下:
Java代码
1. //这个和Gallery的使用相似,也需要存图片在BaseAdapter中。
2. GridView myGridView = (GridView)findViewById(R.id.gridView1);
3. myGridView.setAdapter(new ImageAdapter(this));
4.
5. myGridView.setBackgroundResource(R.drawable.background);
6. myGridView.setOnItemClickListener(new OnItemClickListener(){
7.
8. @Override
9. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
10. long arg3) {
11. // TODO Auto-generated method stub
12. Toast.makeText(AndroidUnleashed003Activity.this, "你选择了"+(arg2+1)+" 号图片 ", Toast.LENGTH_SHORT).show();
13. }
14.
15. });
16. }
//这个和Gallery的使用相似,也需要存图片在BaseAdapter中。 GridView myGridView = (GridView)findViewById(R.id.gridView1); myGridView.setAdapter(new ImageAdapter(this)); myGridView.setBackgroundResource(R.drawable.background); myGridView.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.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. Button myButton = (Button)findViewById(R.id.myButton);
5.
6. myButton.setOnClickListener(new Button.OnClickListener(){
7. private int mIndex = -1;
8. @Override
9. public void onClick(View v) {
10. // TODO Auto-generated method stub
11. TextView myTextView = new TextView(ScrollerViewActivity.this);
12. myTextView.setText("Text View"+mIndex);
13. LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
14. //增加一个TextView到现行布局中
15. myLayout.addView(myTextView,p);
16.
17. Button myButtonView = new Button(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); Button myButton = (Button)findViewById(R.id.myButton); myButton.setOnClickListener(new Button.OnClickListener(){ private int mIndex = -1; @Override public void onClick(View v) { // TODO Auto-generated method stub TextView myTextView = new TextView(ScrollerViewActivity.this); myTextView.setText("Text View"+mIndex); LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); //增加一个TextView到现行布局中 myLayout.addView(myTextView,p); Button myButtonView = new Button(ScrollerViewActivity.this); myButtonView.setText("Button "+mIndex++); //增加一个Button到线性布局中 myLayout.addView(myButtonView,p); //改变默认焦点切换 //myButtonView.setOnKeyListener(newButtonKeyListener); //投递一个消息进行滚动 //myHandler.post(myScolltoBottom); } }); }
13.ProgressBar的使用,代码如下:
ProgressBarStyleHorizontal(长形进度条)
ProgressBarStyleLarge(圆形进度条)
Java代码
1. public class ProgressBarA extends Activity {
2. protected static final int GUI_STOP_NOTIFIER = 0x108;
3. protected static final int GUI_THREADING_NOTIFIER = 0x109;
4. private ProgressBar myProgress1;
5. private ProgressBar myProgress2;
6. private Button myButton;
7. private int intCounter;
8. /** Called when the activity is first created. */
9. @Override
10. public void onCreate(Bundle savedInstanceState) {
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(new Button.OnClickListener(){
25.
26. @Override
27. public void onClick(View v) {
28. // TODO Auto-generated method stub
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. new Thread(new Runnable(){
39.
40. @Override
41. public void run() {
42. // TODO Auto-generated method stub
43. for(int i=0;i<10;i++){
44. try{
45. intCounter=(i+1)*20;
46. Thread.sleep(1000);
47. if(i==4){
48. Message m = new Message();
49. m.what = ProgressBarA.GUI_STOP_NOTIFIER;
50. ProgressBarA.this.myMessageHandler.sendMessage(m);
51. break;
52. }
53. else{
54. Message m = new Message();
55. m.what = ProgressBarA.GUI_THREADING_NOTIFIER;
56. ProgressBarA.this.myMessageHandler.sendMessage(m);
57. }
58. }
59. catch(Exception e){
60. e.printStackTrace();
61. }
62. }
63. }
64.
65. }).start();
66. }
67.
68. });
69. }
70. Handler myMessageHandler = new Handler(){
71. public void handleMessage(Message msg){
72. switch(msg.what){
73. case ProgressBarA.GUI_STOP_NOTIFIER:
74. //myProgress1.setVisibility(View.GONE);
75. //myProgress2.setVisibility(View.GONE);
76. //Thread.currentThread().interrupt();
77. break;
78. case ProgressBarA.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. }
public class ProgressBarA extends Activity { protected static final int GUI_STOP_NOTIFIER = 0x108; protected static final int GUI_THREADING_NOTIFIER = 0x109; private ProgressBar myProgress1; private ProgressBar myProgress2; private Button myButton; private int intCounter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置窗口模式,因为需要显示进度条在标题栏 requestWindowFeature(Window.FEATURE_PROGRESS); setProgressBarVisibility(true); setContentView(R.layout.main); //取得ProgressBar myProgress1 = (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(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub myProgress1.setVisibility(View.VISIBLE); myProgress2.setVisibility(View.VISIBLE); myProgress1.setMax(100); myProgress1.setProgress(0); myProgress2.setProgress(0); //通过线程来改变Progress的值 new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<10;i++){ try{ intCounter=(i+1)*20; Thread.sleep(1000); if(i==4){ Message m = new Message(); m.what = ProgressBarA.GUI_STOP_NOTIFIER; ProgressBarA.this.myMessageHandler.sendMessage(m); break; } else{ Message m = new Message(); m.what = ProgressBarA.GUI_THREADING_NOTIFIER; ProgressBarA.this.myMessageHandler.sendMessage(m); } } catch(Exception e){ e.printStackTrace(); } } } }).start(); } }); } Handler myMessageHandler = new Handler(){ public void handleMessage(Message msg){ switch(msg.what){ case ProgressBarA.GUI_STOP_NOTIFIER: //myProgress1.setVisibility(View.GONE); //myProgress2.setVisibility(View.GONE); //Thread.currentThread().interrupt(); break; case ProgressBarA.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. implements SeekBar.OnSeekBarChangeListener
2. //实现OnSeekBarChangeListener接口
3. public void onProgressChanged(SeekBar seekBar, int progress,
4. boolean fromUser) {
5. // TODO Auto-generated method stub
6. myProgressText.setText("当前值:"+ progress);
7. }
8. @Override
9. public void onStartTrackingTouch(SeekBar seekBar) {
10. // TODO Auto-generated method stub
11. myTrackingText.setText("正在调节");
12. }
13. @Override
14. public void onStopTrackingTouch(SeekBar seekBar) {
15. // TODO Auto-generated method stub
16. myTrackingText.setText("停止调节");
17. }
18. }
implements SeekBar.OnSeekBarChangeListener //实现OnSeekBarChangeListener接口 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub myProgressText.setText("当前值:"+ progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub myTrackingText.setText("正在调节"); } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub myTrackingText.setText("停止调节"); } }
15.Notification、NotificationManager的使用,代码如下:
Java代码
1. myIntent = new Intent(Notification01.this,Notification02.class);
2. //主要是设置点击通知时显示内容的类
3. myPendingIntent = PendingIntent.getActivity(Notification01.this, 0, myIntent, 0);
4. //构造Notification对象
5. myNotification = new Notification();
6. myButton1.setOnClickListener(new Button.OnClickListener(){
7. @Override
8. public void onClick(View v) {
9. // TODO Auto-generated method stub
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 = new Intent(Notification01.this,Notification02.class); //主要是设置点击通知时显示内容的类 myPendingIntent = PendingIntent.getActivity(Notification01.this, 0, myIntent, 0); //构造Notification对象 myNotification = new Notification(); myButton1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub //设置通知在状态栏显示的图标 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(new Button.OnClickListener(){
4. @Override
5. public void onClick(View v) {
6. // TODO Auto-generated method stub
7. //创建ProgressDialog对象
8. myProgressDialog = new ProgressDialog(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("确定", new DialogInterface.OnClickListener() {
23.
24. @Override
25. public void onClick(DialogInterface dialog, int which) {
26. // TODO Auto-generated method stub
27. //点击确定取消对话框
28. dialog.cancel();
29. }
30. });
31. //让ProgressDialog显示
32. myProgressDialog.show();
33. }
34. });
35.
36. myButton02.setOnClickListener(new Button.OnClickListener(){
37. @Override
38. public void onClick(View v) {
39. // TODO Auto-generated method stub
40. myCount = 0;
41. //创建ProgressDialog对象
42. myProgressDialog = new ProgressDialog(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("确定", new DialogInterface.OnClickListener() {
59.
60. @Override
61. public void onClick(DialogInterface dialog, int which) {
62. // TODO Auto-generated method stub
63. //点击确定取消对话框
64. dialog.cancel();
65. }
66. });
67. //让ProgressDialog显示
68. myProgressDialog.show();
69. new Thread(){
70. public void run(){
71. try{
72. while(myCount <= 100){
73. //由线程来控制进度
74. myProgressDialog.setProgress(myCount++);
75. Thread.sleep(100);
76. }
77. myProgressDialog.cancel();
78. }
79. catch(InterruptedException e){
80. myProgressDialog.cancel();
81. }
82. }
83. }.start();
84. }
85. });
myButton01 = (Button)findViewById(R.id.Button1); myButton02 = (Button)findViewById(R.id.Button2); myButton01.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub //创建ProgressDialog对象 myProgressDialog = new ProgressDialog(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的一个Button myProgressDialog.setButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //点击确定取消对话框 dialog.cancel(); } }); //让ProgressDialog显示 myProgressDialog.show(); } }); myButton02.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub myCount = 0; //创建ProgressDialog对象 myProgressDialog = new ProgressDialog(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的一个Button myProgressDialog.setButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //点击确定取消对话框 dialog.cancel(); } }); //让ProgressDialog显示 myProgressDialog.show(); new Thread(){ public void run(){ try{ while(myCount <= 100){ //由线程来控制进度 myProgressDialog.setProgress(myCount++); Thread.sleep(100); } myProgressDialog.cancel(); } catch(InterruptedException e){ myProgressDialog.cancel(); } } }.start(); } });
17.TabWidget的使用,代码如下:
要使用TabWidget,首先要了解TabHost,TabHost是一个用来存放Tab标签的容器。
Java代码
1. public class TabHostActivity extends TabActivity {
2. TabHost myTabHost;
3.
4. /** Called when the activity is first created. */
5. @Override
6. public void onCreate(Bundle savedInstanceState) {
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("TAB 1", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.myTabContent1));
13. myTabHost.addTab(myTabHost.newTabSpec("tabHost2").setIndicator("TAB 2", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.myTabContent2));
14. myTabHost.addTab(myTabHost.newTabSpec("tabHost3").setIndicator("TAB 3", 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(new TabHost.OnTabChangeListener() {
21.
22. @Override
23. public void onTabChanged(String tabId) {
24. // TODO Auto-generated method stub
25. Dialog dialog = new AlertDialog.Builder(TabHostActivity.this).setTitle(" 提示").setMessage("当前选中"+tabId).setPositiveButton("确定", new DialogInterface.OnClickListener() {
26.
27. @Override
28. public void onClick(DialogInterface dialog, int which) {
29. // TODO Auto-generated method stub
30. dialog.cancel();
31. }
32. }).create();
33. dialog.show();
34. }
35. });
36. }
37. }
相关推荐
根据提供的信息,我们可以推断出《Android应用开发揭秘》这本书主要涵盖了Android应用程序的开发流程及相关技术,适合初学者作为入门指南。尽管具体内容未给出详细章节或摘要,但基于标题、描述及常见Android开发...
《Android应用开发揭秘》是一本深入探讨Android应用开发的专业书籍,涵盖了从基础知识到高级技术的全方位解析。书中通过源码分析和实例讲解,帮助开发者理解Android系统的内部运作机制,提升应用设计与开发的能力。 ...
《Android应用程序开发教程(第2版)》教学课件02Android UI设计.pdf《Android应用程序开发教程(第2版)》教学课件02Android UI设计.pdf《Android应用程序开发教程(第2版)》教学课件02Android UI设计.pdf《Android应用...
《Android应用开发揭秘高清版》是一本深入探讨Android应用程序开发的专业书籍,对于想要在移动平台领域一展拳脚的开发者来说,它无疑是一份宝贵的参考资料。该资源包含一个PDF文件,大小为47.2 MB,确保了内容的清晰...
《Android应用开发揭秘》全部实例源代码,配合《Android应用开发揭秘》使用 前言 第一部分 准备篇 第1章 Android开发简介 1.1 Android基本概念 1.1.1 Android简介 1.1.2 Android的系统构架 1.1.3 ...
《Android Framework 开发揭秘》这本书深入剖析了Android系统的内部运作机制,是Android开发者提升技术深度的宝贵资料。它涵盖了从系统启动到服务框架的构建,再到关键组件的工作原理,旨在帮助读者理解和掌握...
本书《Android应用开发揭秘》深入探讨了Android系统在移动互联网时代的地位和其对PC时代的影响力,强调了其作为移动设备操作系统的优势,包括开放性和免费的特性,以及它在全球开发者社区和一流手机厂商中的广泛支持...
《Android Framework 开发揭秘》是腾讯技术团队倾力打造的一部深入探讨Android系统框架底层原理的专业书籍。本书针对Android开发者,尤其是对系统级开发有深厚兴趣的工程师们,旨在揭示Android系统的神秘面纱,帮助...
杨丰盛,Android应用开发先驱,对Android有深入研究,实战经验极其丰富。精通Java、C、C++等语言,专注于移动通信软件开发,在机顶盒软件开发和MTK平台软件开发方面有非常深厚的积累。2007年获得中国软件行业协会...
"Android应用开发揭秘(源码)" 提供了深入洞察Android应用构建过程的机会,这对于初学者和有经验的开发者来说都是一个不可多得的学习资料。这份源码涵盖了多个关键知识点,下面我们将逐一探讨。 1. **Activity生命...
《Android应用开发揭秘》这本书是Android开发者们的重要参考资料,它深入浅出地讲解了Android应用开发的各个环节。源代码作为书中的实践部分,是理论知识的直观体现,可以帮助读者更好地理解和掌握Android编程技术。...
《Android应用开发揭秘》是一本深入探讨Android应用开发的专业书籍,其随书附带的源代码为读者提供了实践和学习的宝贵资源。本书主要涵盖了Android平台的各个方面,旨在帮助开发者从基础到高级深入理解Android应用的...
第2篇为应用开发篇,通过实例介绍了Android UI布局、Android人机界面、手机硬件设备的使用、Android本地存储系统、Android中的数据库、多线程设计、Android传感器、Android游戏开发基础、Android与Internet,以及...
《Android应用开发揭秘》这本书是Android开发者的重要参考资料,它深入探讨了Android平台的应用开发技术,涵盖了从基础到高级的各种主题。以下将详细阐述书中的关键知识点: 1. **Android系统架构**:Android系统由...
《Android应用开发揭秘》内容全面,不仅详细讲解了Android框架、Android组件、用户界面开发、游戏开发、数据存储、多媒体开发和网络开发等基础知识,而且还深入阐述了传感器、语音识别、桌面组件开发、Android游戏...
《Android应用开发揭秘》是一本深入探讨Android应用开发的专业书籍,涵盖了从基础知识到高级技术的全方位内容。这本书的PDF版本提供了详细的理论讲解和实例代码,是初学者和进阶开发者的重要参考资料。配合提供的...
在Android应用开发领域,这本书《Android应用开发揭秘》是一本极具价值的资源,它深入浅出地探讨了Android开发与传统Java开发的区别,并且针对个人和小团队开发者提供了实用的指导。书中通过精选的实例,讲解了从...
《Android应用开发揭秘》这本书是Android开发者们的重要参考资料,它深入浅出地讲解了Android应用开发的各个环节。源代码作为书中的实践部分,是理论知识的直观体现,可以帮助读者更好地理解和掌握Android开发技术。...
- **内容覆盖**:本书内容全面,涵盖了从Android框架基础到高级应用开发的各个层面,不仅适合初学者入门,也适用于有一定经验的开发者进阶学习。 #### 二、Android基础知识 - **Android系统架构**:首先介绍了...