[android]代码库
1.MainActivity.java
package com.example.acer.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import com.samir.R;
public class MainActivity extends AppCompatActivity {
MyTask objMyTask;
private static final int SLEEP_TIME=((10*1000)/100);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view)
{
objMyTask = new MyTask();
objMyTask.execute();
}
class MyTask extends AsyncTask<Void, Integer, Void> {
Dialog dialog;
ProgressBar progressBar;
TextView tvLoading,tvPer;
Button btnCancel;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new Dialog(MainActivity.this);
dialog.setCancelable(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progressdialog);
progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1);
tvLoading = (TextView) dialog.findViewById(R.id.tv1);
tvPer = (TextView) dialog.findViewById(R.id.tvper);
btnCancel = (Button) dialog.findViewById(R.id.btncancel);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
objMyTask.cancel(true);
dialog.dismiss();
}
});
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
for (int i = 1; i <100; i++) {
if (isCancelled()) {break;}
else {publishProgress(i);
try {Thread.sleep(SLEEP_TIME);}
catch (InterruptedException e) {e.printStackTrace();}
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
tvLoading.setText("Loading... " + values[0] + " %");
tvPer.setText(values[0]+" %");
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Completed!!!");
alert.setMessage("Your Task is Completed SuccessFully!!!");
alert.setButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
}
}
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#dcdcdc"
android:orientation="vertical" >
<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="32dp"
android:text="开始"
android:onClick="click" />
</RelativeLayout>
3.progressdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:text="Loading..."
android:textAppearance="?android:attr/textAppearanceLarge" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv1"
android:layout_marginLeft="5dp"
android:layout_marginRight="25dp"
android:max="100" />
<Button
android:id="@+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Cancel" />
<TextView
android:id="@+id/tvper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
by: 发表于:2017-09-29 14:56:49 顶(0) | 踩(0) 回复
??
回复评论