公司网站设计用什么,杭州网站建设h5,上传软件的网站,广州seo公司品牌在很多PC软件或手机软件中#xff0c;我们都会看见 “加载中...” 类似的对话框#xff0c;当然#xff0c;在android应用程序中也是如此。如果我们想在android应用程序中使用这样的效果#xff0c;那么就需要用到ProgressDialog。首先#xff0c;我们来看一下ProgressDia… 在很多PC软件或手机软件中我们都会看见 “加载中...” 类似的对话框当然在android应用程序中也是如此。如果我们想在android应用程序中使用这样的效果那么就需要用到ProgressDialog。首先我们来看一下ProgressDialog这个类。 ProgressDialog类继承自AlertDialog类同样存放在android.app包中。ProgressDialog有两种形式一种是圆圈旋转形式一种是水平进度条形式选择哪种形式可以通过以下两个属性值来设定 static intSTYLE_HORIZONTAL Creates a ProgressDialog with a horizontal progress bar.static intSTYLE_SPINNER Creates a ProgressDialog with a ciruclar, spinning progress bar.注意当设置为水平进度条形式时进度的取值范围为0—10000。 ProgressDialog的构造方法有以下两种 ProgressDialog(Context context) ProgressDialog(Context context, int theme) 除了构造方法外ProgressDialog还提供的如下的静态方法返回ProgressDialog对象 static ProgressDialogshow(Context context, CharSequence title, CharSequence message) static ProgressDialogshow(Context context, CharSequence title, CharSequence message, boolean indeterminate) static ProgressDialogshow(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable) static ProgressDialogshow(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable, DialogInterface.OnCancelListener cancelListener) 需要留意的是第一个参数必须是目前运行的Activity的Context。 android的ProgressDialog必须要在后台程序运行完毕前以dismiss()方法来关闭取得焦点的对话框否则程序就会陷入无法终止的无穷循环中。在线程中不得有任何更改Context或parent View的任何状态文字输出等时间因为线程里的Context与View并不属于parent两者之间也没有关联。 我们以下面一个简单的程序来学习ProgressDialog的应用 public class MainActivity extends Activity
{private Button buttonnull;public ProgressDialog dialognull;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_main);this.button(Button)super.findViewById(R.id.button);this.button.setOnClickListener(new OnClickListener() { Overridepublic void onClick(View v) {final CharSequence strDialogTitleMainActivity.this.getString(R.string.str_dialog_title);final CharSequence strDialogBodyMainActivity.this.getString(R.string.str_dialog_body);//显示Progress对话框dialogProgressDialog.show(MainActivity.this,strDialogTitle,strDialogBody,true);new Thread(){Overridepublic void run(){try{//表示后台运行的代码段以暂停3秒代替sleep(3000);}catch (InterruptedException e) {e.printStackTrace();}finally{//卸载dialog对象dialog.dismiss();}}}.start();}});}} 该程序布局管理器仅需一个Button组件id为button即可此处不再给出。 注意为了代码更加符合规范本程序在strings.xml中定义了如下字符串资源 ?xml version1.0 encodingutf-8?
resourcesstring nameapp_namedemo2/stringstring nameaction_settingsSettings/stringstring namehello_worldHello world!/stringstring nameexecute执行/stringstring namestr_dialog_title请稍等片刻/stringstring namestr_dialog_body正在执行.../string/resources 程序运行效果截图 转载于:https://www.cnblogs.com/dyllove98/archive/2013/06/09/3130042.html