一.等待窗口实现:
记录一下,有时候我们下载数据的时候为了提高用户的体验,加一个等待窗口还是有必要的,有两种等待窗口:
第一种旋转的ProgressBar:
主界面:
public class MainActivity extends Activity {
private Button button1;
private TextView textview1;
public AlertDialog selfdialog;
private MainActivity main;
public Handler mhandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
//显示TextView
case 1:
Toast.makeText(main, "成功", Toast.LENGTH_SHORT).show();
textview1.setVisibility(View.VISIBLE);
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = this;
button1 = (Button) findViewById(R.id.button1);
textview1 = (TextView) findViewById(R.id.textview1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
showDialogLoading();
}
});
}
//等待dialog
private void showDialogLoading() {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(this.LAYOUT_INFLATER_SERVICE);
//view
View view = inflater.inflate(R.layout.footer, null);
AlertDialog.Builder ad = new AlertDialog.Builder(this);
selfdialog = ad.create();
selfdialog.setView(view);
//屏蔽掉点击
selfdialog.setCancelable(false);
selfdialog.show();
new Thread() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = new Message();
message.what = 1;
main.mhandler.sendMessage(message);
//关闭dialog
selfdialog.dismiss();
}
}.start();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击我"
/>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载完成后显示我"
android:visibility="gone"
/>
</LinearLayout>
footer.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击我"
/>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载完成后显示我"
android:visibility="gone"
/>
</LinearLayout>
这样第一种的等待窗就实现了。
第二种进度条式的等待窗:
主界面:
public class MainActivity extends Activity {
private Button button1;
private TextView textview1;
private TextView textview2;
public AlertDialog selfdialog;
private MainActivity main;
public Handler mhandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
//显示TextView
case 1:
textview2.setText("正在下载("+msg.obj.toString()+"/10)");
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = this;
button1 = (Button) findViewById(R.id.button1);
textview1 = (TextView) findViewById(R.id.textview1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
showDialogLoading();
}
});
}
//等待dialog
private void showDialogLoading() {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(this.LAYOUT_INFLATER_SERVICE);
//view
View view = inflater.inflate(R.layout.footer, null);
final ProgressBar progressbar1 = (ProgressBar)view.findViewById(R.id.progressbar1);
textview2 = (TextView)view.findViewById(R.id.textview2);
AlertDialog.Builder ad = new AlertDialog.Builder(this);
selfdialog = ad.create();
selfdialog.setView(view);
//屏蔽掉点击
selfdialog.setCancelable(false);
selfdialog.show();
new Thread() {
@Override
public void run() {
//getProgress()获得当前的进度值
//incrementProgressBy()每次增加的量
//getMax()获得这个进度条的范围的上限
int max = progressbar1.getMax();
int i = 0;
while(true){
if(i > max){
break;
}
try {
Thread.sleep(1000);
progressbar1.incrementProgressBy(1);
Message message = new Message();
message.what = 1;
message.obj = progressbar1.getProgress();
main.mhandler.sendMessage(message);
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//关闭dialog
selfdialog.dismiss();
}
}.start();
}
}
footer.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center" >
<ProgressBar
android:id="@+id/progressbar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:max="10"
/>
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在下载(0/10)"
android:textColor="@android:color/black"
android:textSize="9sp" />
</LinearLayout>
activity_main与上个例子布局一样。