ProgressDialog和AlertDialog(具有选择功能)的例子

今天看书写了这两个例子:一、对载提示框的使用,二、具有选择功能的提示框使用。

Activity例码:

package com.pdw2009;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 *
 * @author peidw 2009-08-23
 *
 */
public class ProgressDemoActivity extends Activity{
 public ProgressDialog myDialog=null;
 private TextView tv_1;
 private Button btn_1;
 private Button btn_2;
 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  this.setContentView(R.layout.progress);
  btn_1=(Button)this.findViewById(R.id.btn_progress1);
  btn_2=(Button)this.findViewById(R.id.btn_selectlert);
  tv_1=(TextView)this.findViewById(R.id.tv_1);
  
  btn_1.setOnClickListener(new OnClickListener(){
   public void onClick(View v) {
    myDialog=ProgressDialog.show(ProgressDemoActivity.this, "加载","加载中,请稍候..",true);
    tv_1.setText("加载中,请稍候….");
    new Thread(){
     public void run(){
      try{
       sleep(3000);
       
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       myDialog.dismiss();
      }
     }
    }.start();
   }
   
  });
  
  btn_2.setOnClickListener(new Button.OnClickListener(){

   public void onClick(View v) {
    new AlertDialog.Builder(ProgressDemoActivity.this)
    .setTitle("请选择")
    .setItems(R.array.sel_fruit, new DialogInterface.OnClickListener(){
     public void onClick(DialogInterface dialog, int which) {
      String ary_fruit[]=getResources().getStringArray(R.array.sel_fruit);
      String sel_value=ary_fruit[which];
      new AlertDialog.Builder(ProgressDemoActivity.this)
      .setMessage("你选择的是: "+sel_value)
      .setNegativeButton("确认", new android.content.DialogInterface.OnClickListener(){

       public void onClick(DialogInterface dialog,int which) {
        // TODO  确认选择项时要做的操作
        
       }
       
      }).show();
     }
     
    }).show();
   }
   
  });
 }
}
 

界面progress.xml代码

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_progress" android:layout_y="78dip" android:layout_x="82dip" android:id="@+id/btn_progress1">
</Button>
<TextView android:id="@+id/tv_1" android:layout_y="171dip" android:layout_width="wrap_content" android:text="TextView01" android:layout_height="wrap_content" android:layout_x="0px" android:hint="测试" android:height="20px"></TextView>
<Button android:layout_y="97dip" android:id="@+id/btn_selectlert" android:layout_width="wrap_content" android:layout_x="171dip" android:text="@string/select_alertdialog" android:layout_height="wrap_content"></Button>
</AbsoluteLayout>
 

string.xml代码

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ExampleActivity!</string>
    <string name="app_name">Example</string>
    <string name="quit">退出</string>
     <string name="app_about">请确认</string>
    <string name="btn_ok">确定</string>
    <string name="btn_progress">加载测试</string>
    <string name="btn_alert">AlertDiadlog窗口</string>
    <string name="select_alertdialog">具有选择功能的AD</string>
    <string-array name="sel_fruit">
     <item>苹果</item>
     <item>龙眼</item>
     <item>桃子</item>
     <item>荔枝</item>
    </string-array>
</resources>
 

相关图片

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值