Android开发之《Android应用开发揭秘》UI事件汇总
/*
* Android开发之《Android应用开发揭秘》UI事件汇总
* 北京Android俱乐部群:167839253
* Created on: 2011-12-01
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
思想跑毛是很可怕的,最近去尝试了下创业,结果碰了一鼻子灰,公司乱七八糟的项目搞的焦头烂额。各种没有心情。还好,现在沉淀下来,继续android开发的征程。
以下是Android应用开发揭秘这本书第四章的关于UI的事件处理的总结。基本上都是可以复用的代码。存着日后方便。
1.EditView控件的setOnKeyListener方法。点击上下左右键或者中间键的时候可以出发该方法。代码如下。
- 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控件的组合使用,代码如下:
- 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控件的使用,代码如下:
- 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控件的使用,代码如下:
- myButton1.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- }
- });
4.Spinner控件的使用,代码如下:
- //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的使用,代码如下:
- 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代码可以写成:
- <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代码:
- @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;
- }
- .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.弹出对话框的使用,代码如下:
这个功能为弹出对话框,点击确定后弹出登录对话框,然后点击登录后弹出多线程对话框。
- 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的使用,代码如下:
- 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的使用,拖动效果很炫,代码如下:
- 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的使用,代码如下:
- //首先需要实现接口:
- 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的使用,代码如下:
- //这个和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的使用,代码如下:
- //要垂直滚动,使用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(圆形进度条)
- 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的使用,代码如下:
- 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的使用,代码如下:
- 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的使用,代码如下:
- 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标签的容器。
- public class TabHostActivity extends TabActivity {
- TabHost myTabHost;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //获取TabHost对象
- myTabHost = getTabHost();
- //为TabHost添加标签,新建一个newTabSpec(newTabSpec)设置其标签和图标(setIndicator)设置内容(setContent)
- myTabHost.addTab(myTabHost.newTabSpec("tabHost1").setIndicator("TAB 1", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.myTabContent1));
- myTabHost.addTab(myTabHost.newTabSpec("tabHost2").setIndicator("TAB 2", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.myTabContent2));
- myTabHost.addTab(myTabHost.newTabSpec("tabHost3").setIndicator("TAB 3", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.myTabContent3));
- //设置TabHost的背景颜色
- myTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
- //设置当前显示哪个标签
- myTabHost.setCurrentTab(1);
- //标签切换事件处理setOnTabChangedListener
- myTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
- @Override
- public void onTabChanged(String tabId) {
- // TODO Auto-generated method stub
- Dialog dialog = new AlertDialog.Builder(TabHostActivity.this).setTitle("提示").setMessage("当前选中"+tabId).setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- dialog.cancel();
- }
- }).create();
- dialog.show();
- }
- });
- }
- }