public class CustomDialog extends AlertDialog {
public CustomDialog(Context context) {
super(context);
}
private TextView tv_loading;
private ProgressBar progressBar;
private Timer timer;
private int count = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_progress);
tv_loading = (TextView) findViewById(R.id.tv_loading);
progressBar = (ProgressBar) findViewById(R.id.pb);
// 设置Dialog显示的宽度,
Display d = getWindow().getWindowManager().getDefaultDisplay();
WindowManager.LayoutParams lp = getWindow().getAttributes();
//这里设置为屏幕宽度的百分之八十
lp.width = (int) (d.getWidth() * 0.8);
getWindow().setAttributes(lp);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
}, 300, 300);
setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (timer != null) {
timer.cancel();
}
}
});
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
count++;
if (count > 3) {
count = 1;
}
switch (count) {
case 1:
tv_loading.setText("加载中.");
break;
case 2:
tv_loading.setText("加载中..");
break;
case 3:
tv_loading.setText("加载中...");
break;
}
}
};
public void setProgress(int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
this.dismiss();
}
}
}
<?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="match_parent" android:background="@drawable/shape_dialog_bg" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/tv_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="加载中..." android:textSize="16sp" /> <ProgressBar android:id="@+id/pb" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="10dp" android:max="100" android:progressDrawable="@drawable/layer_list_progress_drawable" /> </LinearLayout>
public class MainActivity extends FragmentActivity {
private CustomDialog customDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customDialog = new CustomDialog(this);
}
private int count = 0;
public void tvClick(View view) {
customDialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
count += 10;
runOnUiThread(new Runnable() {
@Override
public void run() {
if (customDialog != null && customDialog.isShowing()) {
customDialog.setProgress(count);
}
}
});
if (count >= 100) {
timer.cancel();
}
}
}, 0, 500);
customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (timer != null) timer.cancel();
count = 0;
}
});
}
}
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:clickable="true" android:onClick="tvClick" android:padding="10dp" android:text="点击弹框" />
private String[] scoreText = {". ", ".. ", "..."};
ValueAnimator valueAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_progress);
tv_loading = (TextView) findViewById(R.id.tv_loading);
progressBar = (ProgressBar) findViewById(R.id.pb);
// 设置Dialog显示的宽度,
Display d = getWindow().getWindowManager().getDefaultDisplay();
WindowManager.LayoutParams lp = getWindow().getAttributes();
//这里设置为屏幕宽度的百分之八十
lp.width = (int) (d.getWidth() * 0.8);
getWindow().setAttributes(lp);
if (valueAnimator == null) {
valueAnimator = ValueAnimator.ofInt(0, 3).setDuration(1000);
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int i = (int) animation.getAnimatedValue();
tv_loading.setText("加载中" + scoreText[i % scoreText.length]);
}
});
}
valueAnimator.start();
}
//代码省略...
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有