Android学习第十一天----AlertDialog_3
项目需求:通过点击按钮,显示出来一个对话框,选择其中一个选项之后出来一个进度对话框,一段时间之后自动消失。
xml文件中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="160dp" android:text="点我" /> </RelativeLayout>
MainActivity中:
package com.example.alertdialog_3; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private String[] info = {"北京","上海","天津"}; private Button mButton; private AlertDialog.Builder mBuilder ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button)findViewById(R.id.button1); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mBuilder = new AlertDialog.Builder(MainActivity.this); mBuilder.setTitle("你好!"); mBuilder.setIcon(R.drawable.ic_launcher); mBuilder.setItems(info, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(which == 0) { final ProgressDialog mProgressDialog = new ProgressDialog(MainActivity.this); mProgressDialog.setTitle("hello"); mProgressDialog.setMessage("heihe"); mProgressDialog.show(); new Thread() { public void run() { try { Thread.sleep(5* 1000); mProgressDialog.dismiss(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } } }); mBuilder.create(); mBuilder.show(); } }); } }
其中的进度对话框显示时间,用的是线程的方式让它睡眠5秒钟再起来;
android里面的线程是不可以直接对组件进行操作的;